1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
package kong
import (
"testing"
uuid "github.com/satori/go.uuid"
"github.com/stretchr/testify/assert"
)
const (
caCert1 = `-----BEGIN CERTIFICATE-----
MIIEvjCCAqagAwIBAgIJALabx/Nup200MA0GCSqGSIb3DQEBCwUAMBMxETAPBgNV
BAMMCFlvbG80Mi4xMCAXDTE5MDkxNTE2Mjc1M1oYDzIxMTkwODIyMTYyNzUzWjAT
MREwDwYDVQQDDAhZb2xvNDIuMTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoC
ggIBANIW67Ay0AtTeBY2mORaGet/VPL5jnBRz0zkZ4Jt7fEq3lbxYaJBnFI8wtz3
bHLtLsxkvOFujEMY7HVd+iTqbJ7hLBtK0AdgXDjf+HMmoWM7x0PkZO+3XSqyRBbI
YNoEaQvYBNIXrKKJbXIU6higQaXYszeN8r3+RIbcTIlZxy28msivEGfGTrNujQFc
r/eyf+TLHbRqh0yg4Dy/U/T6fqamGhFrjupRmOMugwF/BHMH2JHhBYk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ehxbwqCmRvNToAfp+GTwrhoIDt
YP97asx55JPxsXniM+L8mSsRvS+k11A8fdq5E3luKJ0Pyfct2AzUoy5OzcUWotv1
M5l19WkET0gOKRTzJzUpZiFQ3S6HIk//kT08VD++7UegLDOn2s0TIPLBpU6uaihm
6VSxYPyCcVX+4FeMGU5T47xnT5ClyhjSgUiY50FVuXuyA2zrsdZj4O5KDktK9hGh
Vlz21lz4fvd3gtPgtVXLmaBKlGQFWNLxARBTQFxJJ7JvYu6FnhzhWt6YO49vAnUG
R+pHRocvtyc8EnkuMw6+jGHr
-----END CERTIFICATE-----`
)
func TestCACertificatesService(T *testing.T) {
runWhenKong(T, ">=1.3.0")
assert := assert.New(T)
client, err := NewTestClient(nil, nil)
assert.Nil(err)
assert.NotNil(client)
certificate := &CACertificate{
Cert: String("bar"),
}
createdCertificate, err := client.CACertificates.Create(defaultCtx,
certificate)
assert.NotNil(err) // invalid cert and key
assert.Nil(createdCertificate)
certificate.Cert = String(caCert1)
createdCertificate, err = client.CACertificates.Create(defaultCtx,
certificate)
assert.Nil(err)
assert.NotNil(createdCertificate)
certificate, err = client.CACertificates.Get(defaultCtx,
createdCertificate.ID)
assert.Nil(err)
assert.NotNil(certificate)
certificate.Cert = String(caCert2)
certificate, err = client.CACertificates.Update(defaultCtx, certificate)
assert.Nil(err)
assert.NotNil(certificate)
err = client.CACertificates.Delete(defaultCtx, createdCertificate.ID)
assert.Nil(err)
// ID can be specified
id := uuid.NewV4().String()
certificate = &CACertificate{
Cert: String(caCert3),
ID: String(id),
}
createdCertificate, err = client.CACertificates.Create(defaultCtx,
certificate)
assert.Nil(err)
assert.NotNil(createdCertificate)
assert.Equal(id, *createdCertificate.ID)
err = client.CACertificates.Delete(defaultCtx, createdCertificate.ID)
assert.Nil(err)
}
func TestCACertificateWithTags(T *testing.T) {
runWhenKong(T, ">=1.3.0")
assert := assert.New(T)
client, err := NewTestClient(nil, nil)
assert.Nil(err)
assert.NotNil(client)
certificate := &CACertificate{
Cert: String(caCert3),
Tags: StringSlice("tag1", "tag2"),
}
createdCertificate, err := client.CACertificates.Create(defaultCtx,
certificate)
assert.Nil(err)
assert.NotNil(createdCertificate)
assert.Equal(StringSlice("tag1", "tag2"), createdCertificate.Tags)
err = client.CACertificates.Delete(defaultCtx, createdCertificate.ID)
assert.Nil(err)
}
func TestCACertificateListEndpoint(T *testing.T) {
runWhenKong(T, ">=1.3.0")
assert := assert.New(T)
client, err := NewTestClient(nil, nil)
assert.Nil(err)
assert.NotNil(client)
// fixtures
certificates := []*CACertificate{
{
Cert: String(caCert1),
},
{
Cert: String(caCert2),
},
{
Cert: String(caCert3),
},
}
// create fixturs
for i := 0; i < len(certificates); i++ {
certificate, err := client.CACertificates.Create(defaultCtx,
certificates[i])
assert.Nil(err)
assert.NotNil(certificate)
certificates[i] = certificate
}
certificatesFromKong, next, err :=
client.CACertificates.List(defaultCtx, nil)
assert.Nil(err)
assert.Nil(next)
assert.NotNil(certificatesFromKong)
assert.Equal(3, len(certificatesFromKong))
// check if we see all certificates
assert.True(compareCACertificates(certificates, certificatesFromKong))
// Test pagination
certificatesFromKong = []*CACertificate{}
// first page
page1, next, err := client.CACertificates.List(defaultCtx, &ListOpt{
Size: 1,
})
assert.Nil(err)
assert.NotNil(next)
assert.NotNil(page1)
assert.Equal(1, len(page1))
certificatesFromKong = append(certificatesFromKong, page1...)
// last page
next.Size = 2
page2, next, err := client.CACertificates.List(defaultCtx, next)
assert.Nil(err)
assert.Nil(next)
assert.NotNil(page2)
assert.Equal(2, len(page2))
certificatesFromKong = append(certificatesFromKong, page2...)
assert.True(compareCACertificates(certificates, certificatesFromKong))
certificates, err = client.CACertificates.ListAll(defaultCtx)
assert.Nil(err)
assert.NotNil(certificates)
assert.Equal(3, len(certificates))
for i := 0; i < len(certificates); i++ {
assert.Nil(client.CACertificates.Delete(defaultCtx, certificates[i].ID))
}
}
func compareCACertificates(expected, actual []*CACertificate) bool {
var expectedUsernames, actualUsernames []string
for _, certificate := range expected {
expectedUsernames = append(expectedUsernames, *certificate.Cert)
}
for _, certificate := range actual {
actualUsernames = append(actualUsernames, *certificate.Cert)
}
return (compareSlices(expectedUsernames, actualUsernames))
}
|