How to use Metadata method of dummy Package

Best Testkube code snippet using dummy.Metadata

get_test.go

Source:get_test.go Github

copy

Full Screen

...12 "code.cloudfoundry.org/credhub-cli/credhub/credentials/values"13 . "github.com/onsi/ginkgo/extensions/table"14)15var _ = Describe("Get", func() {16 var nilMetadata credentials.Metadata = nil17 Describe("GetLatestVersion()", func() {18 It("requests the credential by name using the 'current' query parameter", func() {19 dummyAuth := &DummyAuth{Response: &http.Response{20 Body: ioutil.NopCloser(bytes.NewBufferString("")),21 }}22 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))23 ch.GetLatestVersion("/example-password")24 url := dummyAuth.Request.URL.String()25 Expect(url).To(Equal("https://example.com/api/v1/data?current=true&name=%2Fexample-password"))26 Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))27 })28 Context("when successful", func() {29 It("returns a credential by name", func() {30 responseString := `{31 "data": [32 {33 "id": "some-id",34 "name": "/example-password",35 "type": "password",36 "value": "some-password",37 "metadata": {"some":"metadata"},38 "version_created_at": "2017-01-05T01:01:01Z"39 }40 ]}`41 dummyAuth := &DummyAuth{Response: &http.Response{42 StatusCode: http.StatusOK,43 Body: ioutil.NopCloser(bytes.NewBufferString(responseString)),44 }}45 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))46 cred, err := ch.GetLatestVersion("/example-password")47 Expect(err).To(BeNil())48 Expect(cred.Id).To(Equal("some-id"))49 Expect(cred.Name).To(Equal("/example-password"))50 Expect(cred.Type).To(Equal("password"))51 Expect(cred.Value.(string)).To(Equal("some-password"))52 Expect(cred.Metadata).To(Equal(credentials.Metadata{"some": "metadata"}))53 Expect(cred.VersionCreatedAt).To(Equal("2017-01-05T01:01:01Z"))54 })55 })56 Context("when the response body contains an empty list", func() {57 It("returns an error", func() {58 dummyAuth := &DummyAuth{Response: &http.Response{59 StatusCode: http.StatusOK,60 Body: ioutil.NopCloser(bytes.NewBufferString(`{"data":[]}`)),61 }}62 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))63 _, err := ch.GetLatestVersion("/example-password")64 Expect(err).To(MatchError("response did not contain any credentials"))65 })66 })67 })68 Describe("GetById()", func() {69 It("requests the credential by id", func() {70 dummyAuth := &DummyAuth{Response: &http.Response{71 Body: ioutil.NopCloser(bytes.NewBufferString("")),72 }}73 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))74 ch.GetById("0239482304958")75 url := dummyAuth.Request.URL.String()76 Expect(url).To(Equal("https://example.com/api/v1/data/0239482304958"))77 Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))78 })79 Context("when successful", func() {80 It("returns a credential by name", func() {81 responseString := `{82 "id": "0239482304958",83 "name": "/reasonable-password",84 "type": "password",85 "value": "some-password",86 "metadata": {"some":"metadata"},87 "version_created_at": "2017-01-05T01:01:01Z"88 }`89 dummyAuth := &DummyAuth{Response: &http.Response{90 StatusCode: http.StatusOK,91 Body: ioutil.NopCloser(bytes.NewBufferString(responseString)),92 }}93 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))94 cred, err := ch.GetById("0239482304958")95 Expect(err).To(BeNil())96 Expect(cred.Id).To(Equal("0239482304958"))97 Expect(cred.Name).To(Equal("/reasonable-password"))98 Expect(cred.Type).To(Equal("password"))99 Expect(cred.Value.(string)).To(Equal("some-password"))100 Expect(cred.Metadata).To(Equal(credentials.Metadata{"some": "metadata"}))101 Expect(cred.VersionCreatedAt).To(Equal("2017-01-05T01:01:01Z"))102 })103 })104 })105 Describe("GetAllVersions()", func() {106 It("makes a request for all versions of a credential", func() {107 dummyAuth := &DummyAuth{Response: &http.Response{108 Body: ioutil.NopCloser(bytes.NewBufferString("")),109 }}110 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))111 ch.GetAllVersions("/example-password")112 url := dummyAuth.Request.URL.String()113 Expect(url).To(Equal("https://example.com/api/v1/data?name=%2Fexample-password"))114 Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))115 })116 Context("when successful", func() {117 It("returns a list of all passwords", func() {118 responseString := `{119 "data": [120 {121 "id": "some-id",122 "name": "/example-password",123 "type": "password",124 "value": "some-password",125 "metadata": {"some":"metadata"},126 "version_created_at": "2017-01-05T01:01:01Z"127 },128 {129 "id": "some-id",130 "name": "/example-password",131 "type": "password",132 "value": "some-other-password",133 "metadata": null,134 "version_created_at": "2017-01-05T01:01:01Z"135 }136 ]}`137 dummyAuth := &DummyAuth{Response: &http.Response{138 StatusCode: http.StatusOK,139 Body: ioutil.NopCloser(bytes.NewBufferString(responseString)),140 }}141 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))142 creds, err := ch.GetAllVersions("/example-password")143 Expect(err).To(BeNil())144 Expect(creds[0].Id).To(Equal("some-id"))145 Expect(creds[0].Name).To(Equal("/example-password"))146 Expect(creds[0].Type).To(Equal("password"))147 Expect(creds[0].Value.(string)).To(Equal("some-password"))148 Expect(creds[0].Metadata).To(Equal(credentials.Metadata{"some": "metadata"}))149 Expect(creds[0].VersionCreatedAt).To(Equal("2017-01-05T01:01:01Z"))150 Expect(creds[1].Id).To(Equal("some-id"))151 Expect(creds[1].Name).To(Equal("/example-password"))152 Expect(creds[1].Type).To(Equal("password"))153 Expect(creds[1].Value.(string)).To(Equal("some-other-password"))154 Expect(creds[1].Metadata).To(Equal(nilMetadata))155 Expect(creds[1].VersionCreatedAt).To(Equal("2017-01-05T01:01:01Z"))156 })157 It("returns a list of all users", func() {158 responseString := `{159 "data": [160 {161 "id": "some-id",162 "name": "/example-user",163 "type": "user",164 "value": {165 "username": "first-username",166 "password": "dummy_password",167 "password_hash": "$6$kjhlkjh$lkjhasdflkjhasdflkjh"168 },169 "metadata": null,170 "version_created_at": "2017-01-05T01:01:01Z"171 },172 {173 "id": "some-id",174 "name": "/example-user",175 "type": "user",176 "value": {177 "username": "second-username",178 "password": "another_random_dummy_password",179 "password_hash": "$6$kjhlkjh$lkjhasdflkjhasdflkjh"180 },181 "metadata": {"some":"metadata"},182 "version_created_at": "2017-01-05T01:01:01Z"183 }184 ]}`185 dummyAuth := &DummyAuth{Response: &http.Response{186 StatusCode: http.StatusOK,187 Body: ioutil.NopCloser(bytes.NewBufferString(responseString)),188 }}189 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))190 creds, err := ch.GetAllVersions("/example-user")191 Expect(err).To(BeNil())192 Expect(creds[0].Id).To(Equal("some-id"))193 Expect(creds[0].Name).To(Equal("/example-user"))194 Expect(creds[0].Type).To(Equal("user"))195 firstCredValue := creds[0].Value.(map[string]interface{})196 Expect(firstCredValue["username"]).To(Equal("first-username"))197 Expect(firstCredValue["password"]).To(Equal("dummy_password"))198 Expect(firstCredValue["password_hash"]).To(Equal("$6$kjhlkjh$lkjhasdflkjhasdflkjh"))199 Expect(creds[0].Metadata).To(Equal(nilMetadata))200 Expect(creds[0].VersionCreatedAt).To(Equal("2017-01-05T01:01:01Z"))201 Expect(creds[1].Id).To(Equal("some-id"))202 Expect(creds[1].Name).To(Equal("/example-user"))203 Expect(creds[1].Type).To(Equal("user"))204 secondCredValue := creds[1].Value.(map[string]interface{})205 Expect(secondCredValue["username"]).To(Equal("second-username"))206 Expect(secondCredValue["password"]).To(Equal("another_random_dummy_password"))207 Expect(secondCredValue["password_hash"]).To(Equal("$6$kjhlkjh$lkjhasdflkjhasdflkjh"))208 Expect(creds[1].Metadata).To(Equal(credentials.Metadata{"some": "metadata"}))209 Expect(creds[1].VersionCreatedAt).To(Equal("2017-01-05T01:01:01Z"))210 })211 })212 Context("when the response body contains an empty list", func() {213 It("returns an error", func() {214 dummyAuth := &DummyAuth{Response: &http.Response{215 StatusCode: http.StatusOK,216 Body: ioutil.NopCloser(bytes.NewBufferString(`{"data":[]}`)),217 }}218 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))219 _, err := ch.GetAllVersions("/example-password")220 Expect(err).To(MatchError("response did not contain any credentials"))221 })222 })223 })224 Describe("GetNVersions()", func() {225 It("makes a request for N versions of a credential", func() {226 dummyAuth := &DummyAuth{Response: &http.Response{227 Body: ioutil.NopCloser(bytes.NewBufferString("")),228 }}229 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))230 ch.GetNVersions("/example-password", 3)231 url := dummyAuth.Request.URL.String()232 Expect(url).To(Equal("https://example.com/api/v1/data?name=%2Fexample-password&versions=3"))233 Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))234 })235 Context("when successful", func() {236 It("returns a list of N passwords", func() {237 responseString := `{238 "data": [239 {240 "id": "some-id",241 "name": "/example-password",242 "type": "password",243 "value": "some-password",244 "metadata": {"some":"metadata"},245 "version_created_at": "2017-01-05T01:01:01Z"246 },247 {248 "id": "some-id",249 "name": "/example-password",250 "type": "password",251 "value": "some-other-password",252 "metadata": null,253 "version_created_at": "2017-01-05T01:01:01Z"254 }255 ]}`256 dummyAuth := &DummyAuth{Response: &http.Response{257 StatusCode: http.StatusOK,258 Body: ioutil.NopCloser(bytes.NewBufferString(responseString)),259 }}260 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))261 creds, err := ch.GetNVersions("/example-password", 2)262 Expect(err).To(BeNil())263 Expect(creds[0].Id).To(Equal("some-id"))264 Expect(creds[0].Name).To(Equal("/example-password"))265 Expect(creds[0].Type).To(Equal("password"))266 Expect(creds[0].Value.(string)).To(Equal("some-password"))267 Expect(creds[0].Metadata).To(Equal(credentials.Metadata{"some": "metadata"}))268 Expect(creds[0].VersionCreatedAt).To(Equal("2017-01-05T01:01:01Z"))269 Expect(creds[1].Id).To(Equal("some-id"))270 Expect(creds[1].Name).To(Equal("/example-password"))271 Expect(creds[1].Type).To(Equal("password"))272 Expect(creds[1].Value.(string)).To(Equal("some-other-password"))273 Expect(creds[1].Metadata).To(Equal(nilMetadata))274 Expect(creds[1].VersionCreatedAt).To(Equal("2017-01-05T01:01:01Z"))275 })276 It("returns a list of N users", func() {277 responseString := `{278 "data": [279 {280 "id": "some-id",281 "name": "/example-user",282 "type": "user",283 "value": {284 "username": "first-username",285 "password": "dummy_password",286 "password_hash": "$6$kjhlkjh$lkjhasdflkjhasdflkjh"287 },288 "metadata": null,289 "version_created_at": "2017-01-05T01:01:01Z"290 },291 {292 "id": "some-id",293 "name": "/example-user",294 "type": "user",295 "value": {296 "username": "second-username",297 "password": "another_random_dummy_password",298 "password_hash": "$6$kjhlkjh$lkjhasdflkjhasdflkjh"299 },300 "metadata": {"some":"metadata"},301 "version_created_at": "2017-01-05T01:01:01Z"302 }303 ]}`304 dummyAuth := &DummyAuth{Response: &http.Response{305 StatusCode: http.StatusOK,306 Body: ioutil.NopCloser(bytes.NewBufferString(responseString)),307 }}308 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))309 creds, err := ch.GetNVersions("/example-user", 2)310 Expect(err).To(BeNil())311 Expect(creds[0].Id).To(Equal("some-id"))312 Expect(creds[0].Name).To(Equal("/example-user"))313 Expect(creds[0].Type).To(Equal("user"))314 firstCredValue := creds[0].Value.(map[string]interface{})315 Expect(firstCredValue["username"]).To(Equal("first-username"))316 Expect(firstCredValue["password"]).To(Equal("dummy_password"))317 Expect(firstCredValue["password_hash"]).To(Equal("$6$kjhlkjh$lkjhasdflkjhasdflkjh"))318 Expect(creds[0].Metadata).To(Equal(nilMetadata))319 Expect(creds[0].VersionCreatedAt).To(Equal("2017-01-05T01:01:01Z"))320 Expect(creds[1].Id).To(Equal("some-id"))321 Expect(creds[1].Name).To(Equal("/example-user"))322 Expect(creds[1].Type).To(Equal("user"))323 secondCredValue := creds[1].Value.(map[string]interface{})324 Expect(secondCredValue["username"]).To(Equal("second-username"))325 Expect(secondCredValue["password"]).To(Equal("another_random_dummy_password"))326 Expect(secondCredValue["password_hash"]).To(Equal("$6$kjhlkjh$lkjhasdflkjhasdflkjh"))327 Expect(creds[1].Metadata).To(Equal(credentials.Metadata{"some": "metadata"}))328 Expect(creds[1].VersionCreatedAt).To(Equal("2017-01-05T01:01:01Z"))329 })330 })331 Context("when the response body contains an empty list", func() {332 It("returns an error", func() {333 dummyAuth := &DummyAuth{Response: &http.Response{334 StatusCode: http.StatusOK,335 Body: ioutil.NopCloser(bytes.NewBufferString(`{"data":[]}`)),336 }}337 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))338 _, err := ch.GetNVersions("/example-password", 2)339 Expect(err).To(MatchError("response did not contain any credentials"))340 })341 })342 Context("when the server returns a 200 but the cli can not parse the response", func() {343 It("returns an appropriate error", func() {344 responseString := `some-invalid-json`345 dummyAuth := &DummyAuth{Response: &http.Response{346 StatusCode: http.StatusOK,347 Body: ioutil.NopCloser(bytes.NewBufferString(responseString)),348 }}349 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))350 _, err := ch.GetNVersions("/example-password", 2)351 Expect(err.Error()).To(ContainSubstring("The response body could not be decoded:"))352 })353 })354 })355 Describe("GetLatestPassword()", func() {356 It("requests the credential by name", func() {357 dummyAuth := &DummyAuth{Response: &http.Response{358 Body: ioutil.NopCloser(bytes.NewBufferString("")),359 }}360 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))361 ch.GetLatestPassword("/example-password")362 url := dummyAuth.Request.URL363 Expect(url.String()).To(ContainSubstring("https://example.com/api/v1/data"))364 Expect(url.String()).To(ContainSubstring("name=%2Fexample-password"))365 Expect(url.String()).To(ContainSubstring("current=true"))366 Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))367 })368 Context("when successful", func() {369 It("returns a password credential", func() {370 responseString := `{371 "data": [372 {373 "id": "some-id",374 "name": "/example-password",375 "type": "password",376 "value": "some-password",377 "metadata": {"some":"metadata"},378 "version_created_at": "2017-01-05T01:01:01Z"379 }]}`380 dummyAuth := &DummyAuth{Response: &http.Response{381 StatusCode: http.StatusOK,382 Body: ioutil.NopCloser(bytes.NewBufferString(responseString)),383 }}384 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))385 cred, err := ch.GetLatestPassword("/example-password")386 Expect(err).ToNot(HaveOccurred())387 Expect(cred.Id).To(Equal("some-id"))388 Expect(cred.Name).To(Equal("/example-password"))389 Expect(cred.Type).To(Equal("password"))390 Expect(cred.Metadata).To(Equal(credentials.Metadata{"some": "metadata"}))391 Expect(cred.Value).To(BeEquivalentTo("some-password"))392 Expect(cred.VersionCreatedAt).To(Equal("2017-01-05T01:01:01Z"))393 })394 })395 })396 Describe("GetLatestCertificate()", func() {397 It("requests the credential by name", func() {398 dummyAuth := &DummyAuth{Response: &http.Response{399 Body: ioutil.NopCloser(bytes.NewBufferString("")),400 }}401 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))402 ch.GetLatestCertificate("/example-certificate")403 url := dummyAuth.Request.URL404 Expect(url.String()).To(ContainSubstring("https://example.com/api/v1/data"))405 Expect(url.String()).To(ContainSubstring("name=%2Fexample-certificate"))406 Expect(url.String()).To(ContainSubstring("current=true"))407 Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))408 })409 Context("when successful", func() {410 It("returns a certificate credential", func() {411 responseString := `{412 "data": [{413 "id": "some-id",414 "name": "/example-certificate",415 "type": "certificate",416 "value": {417 "ca": "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----",418 "certificate": "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----",419 "private_key": "-----BEGIN RSA PRIVATE KEY-----\n...\n-----END RSA PRIVATE KEY-----"420 },421 "metadata": {"some":"metadata"},422 "version_created_at": "2017-01-01T04:07:18Z"423}]}`424 dummyAuth := &DummyAuth{Response: &http.Response{425 StatusCode: http.StatusOK,426 Body: ioutil.NopCloser(bytes.NewBufferString(responseString)),427 }}428 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))429 cred, err := ch.GetLatestCertificate("/example-certificate")430 Expect(err).ToNot(HaveOccurred())431 Expect(cred.Id).To(Equal("some-id"))432 Expect(cred.Name).To(Equal("/example-certificate"))433 Expect(cred.Type).To(Equal("certificate"))434 Expect(cred.Value.Ca).To(Equal("-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----"))435 Expect(cred.Value.Certificate).To(Equal("-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----"))436 Expect(cred.Value.PrivateKey).To(Equal("-----BEGIN RSA PRIVATE KEY-----\n...\n-----END RSA PRIVATE KEY-----"))437 Expect(cred.Metadata).To(Equal(credentials.Metadata{"some": "metadata"}))438 Expect(cred.VersionCreatedAt).To(Equal("2017-01-01T04:07:18Z"))439 })440 })441 })442 Describe("GetLatestUser()", func() {443 It("requests the credential by name", func() {444 dummyAuth := &DummyAuth{Response: &http.Response{445 Body: ioutil.NopCloser(bytes.NewBufferString("")),446 }}447 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))448 ch.GetLatestUser("/example-user")449 url := dummyAuth.Request.URL450 Expect(url.String()).To(ContainSubstring("https://example.com/api/v1/data"))451 Expect(url.String()).To(ContainSubstring("name=%2Fexample-user"))452 Expect(url.String()).To(ContainSubstring("current=true"))453 Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))454 })455 Context("when successful", func() {456 It("returns a user credential", func() {457 responseString := `{458 "data": [459 {460 "id": "some-id",461 "name": "/example-user",462 "type": "user",463 "value": {464 "username": "some-username",465 "password": "some-password",466 "password_hash": "some-hash"467 },468 "metadata": {"some":"metadata"},469 "version_created_at": "2017-01-05T01:01:01Z"470 }471 ]472 }`473 dummyAuth := &DummyAuth{Response: &http.Response{474 StatusCode: http.StatusOK,475 Body: ioutil.NopCloser(bytes.NewBufferString(responseString)),476 }}477 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))478 cred, err := ch.GetLatestUser("/example-user")479 Expect(err).ToNot(HaveOccurred())480 Expect(cred.Id).To(Equal("some-id"))481 Expect(cred.Name).To(Equal("/example-user"))482 Expect(cred.Type).To(Equal("user"))483 Expect(cred.Value.PasswordHash).To(Equal("some-hash"))484 Expect(cred.Value.User).To(Equal(values.User{485 Username: "some-username",486 Password: "some-password",487 }))488 Expect(cred.Metadata).To(Equal(credentials.Metadata{"some": "metadata"}))489 Expect(cred.VersionCreatedAt).To(Equal("2017-01-05T01:01:01Z"))490 })491 })492 })493 Describe("GetLatestRSA()", func() {494 It("requests the credential by name", func() {495 dummyAuth := &DummyAuth{Response: &http.Response{496 Body: ioutil.NopCloser(bytes.NewBufferString("")),497 }}498 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))499 ch.GetLatestRSA("/example-rsa")500 url := dummyAuth.Request.URL501 Expect(url.String()).To(ContainSubstring("https://example.com/api/v1/data"))502 Expect(url.String()).To(ContainSubstring("name=%2Fexample-rsa"))503 Expect(url.String()).To(ContainSubstring("current=true"))504 Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))505 })506 Context("when successful", func() {507 It("returns a rsa credential", func() {508 responseString := `{509 "data": [510 {511 "id": "some-id",512 "name": "/example-rsa",513 "type": "rsa",514 "value": {515 "public_key": "public-key",516 "private_key": "private-key"517 },518 "metadata": {"some":"metadata"},519 "version_created_at": "2017-01-01T04:07:18Z"520 }521 ]522 }`523 dummyAuth := &DummyAuth{Response: &http.Response{524 StatusCode: http.StatusOK,525 Body: ioutil.NopCloser(bytes.NewBufferString(responseString)),526 }}527 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))528 cred, err := ch.GetLatestRSA("/example-rsa")529 Expect(err).ToNot(HaveOccurred())530 Expect(cred.Id).To(Equal("some-id"))531 Expect(cred.Name).To(Equal("/example-rsa"))532 Expect(cred.Type).To(Equal("rsa"))533 Expect(cred.Value).To(Equal(values.RSA{534 PublicKey: "public-key",535 PrivateKey: "private-key",536 }))537 Expect(cred.Metadata).To(Equal(credentials.Metadata{"some": "metadata"}))538 Expect(cred.VersionCreatedAt).To(Equal("2017-01-01T04:07:18Z"))539 })540 })541 })542 Describe("GetLatestSSH()", func() {543 It("requests the credential by name", func() {544 dummyAuth := &DummyAuth{Response: &http.Response{545 Body: ioutil.NopCloser(bytes.NewBufferString("")),546 }}547 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))548 ch.GetLatestSSH("/example-ssh")549 url := dummyAuth.Request.URL550 Expect(url.String()).To(ContainSubstring("https://example.com/api/v1/data"))551 Expect(url.String()).To(ContainSubstring("name=%2Fexample-ssh"))552 Expect(url.String()).To(ContainSubstring("current=true"))553 Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))554 })555 Context("when successful", func() {556 It("returns a ssh credential", func() {557 responseString := `{558 "data": [559 {560 "id": "some-id",561 "name": "/example-ssh",562 "type": "ssh",563 "value": {564 "public_key": "public-key",565 "private_key": "private-key",566 "public_key_fingerprint": "public-key-fingerprint"567 },568 "metadata": {"some":"metadata"},569 "version_created_at": "2017-01-01T04:07:18Z"570 }571 ]572 }`573 dummyAuth := &DummyAuth{Response: &http.Response{574 StatusCode: http.StatusOK,575 Body: ioutil.NopCloser(bytes.NewBufferString(responseString)),576 }}577 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))578 cred, err := ch.GetLatestSSH("/example-ssh")579 Expect(err).ToNot(HaveOccurred())580 Expect(cred.Id).To(Equal("some-id"))581 Expect(cred.Name).To(Equal("/example-ssh"))582 Expect(cred.Type).To(Equal("ssh"))583 Expect(cred.Value.PublicKeyFingerprint).To(Equal("public-key-fingerprint"))584 Expect(cred.Value.SSH).To(Equal(values.SSH{585 PublicKey: "public-key",586 PrivateKey: "private-key",587 }))588 Expect(cred.Metadata).To(Equal(credentials.Metadata{"some": "metadata"}))589 Expect(cred.VersionCreatedAt).To(Equal("2017-01-01T04:07:18Z"))590 })591 })592 })593 Describe("GetLatestJSON()", func() {594 It("requests the credential by name", func() {595 dummyAuth := &DummyAuth{Response: &http.Response{596 Body: ioutil.NopCloser(bytes.NewBufferString("")),597 }}598 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))599 ch.GetLatestJSON("/example-json")600 url := dummyAuth.Request.URL601 Expect(url.String()).To(ContainSubstring("https://example.com/api/v1/data"))602 Expect(url.String()).To(ContainSubstring("name=%2Fexample-json"))603 Expect(url.String()).To(ContainSubstring("current=true"))604 Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))605 })606 Context("when successful", func() {607 It("returns a json credential", func() {608 responseString := `{609 "data": [610 {611 "id": "some-id",612 "name": "/example-json",613 "type": "json",614 "value": {615 "key": 123,616 "key_list": [617 "val1",618 "val2"619 ],620 "is_true": true621 },622 "metadata": {"some":"metadata"},623 "version_created_at": "2017-01-01T04:07:18Z"624 }625 ]626 }`627 dummyAuth := &DummyAuth{Response: &http.Response{628 StatusCode: http.StatusOK,629 Body: ioutil.NopCloser(bytes.NewBufferString(responseString)),630 }}631 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))632 cred, err := ch.GetLatestJSON("/example-json")633 JSONResult := `{634 "key": 123,635 "key_list": [636 "val1",637 "val2"638 ],639 "is_true": true640 }`641 var unmarshalled values.JSON642 json.Unmarshal([]byte(JSONResult), &unmarshalled)643 Expect(err).ToNot(HaveOccurred())644 Expect(cred.Id).To(Equal("some-id"))645 Expect(cred.Name).To(Equal("/example-json"))646 Expect(cred.Type).To(Equal("json"))647 Expect(cred.Value).To(Equal(unmarshalled))648 Expect(cred.Metadata).To(Equal(credentials.Metadata{"some": "metadata"}))649 Expect(cred.VersionCreatedAt).To(Equal("2017-01-01T04:07:18Z"))650 })651 })652 })653 Describe("GetLatestValue()", func() {654 It("requests the credential by name", func() {655 dummyAuth := &DummyAuth{Response: &http.Response{656 Body: ioutil.NopCloser(bytes.NewBufferString("")),657 }}658 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))659 ch.GetLatestValue("/example-value")660 url := dummyAuth.Request.URL661 Expect(url.String()).To(ContainSubstring("https://example.com/api/v1/data"))662 Expect(url.String()).To(ContainSubstring("name=%2Fexample-value"))663 Expect(url.String()).To(ContainSubstring("current=true"))664 Expect(dummyAuth.Request.Method).To(Equal(http.MethodGet))665 })666 Context("when successful", func() {667 It("returns a value credential", func() {668 responseString := `{669 "data": [670 {671 "id": "some-id",672 "name": "/example-value",673 "type": "value",674 "value": "some-value",675 "metadata": {"some":"metadata"},676 "version_created_at": "2017-01-05T01:01:01Z"677 }]}`678 dummyAuth := &DummyAuth{Response: &http.Response{679 StatusCode: http.StatusOK,680 Body: ioutil.NopCloser(bytes.NewBufferString(responseString)),681 }}682 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))683 cred, err := ch.GetLatestValue("/example-value")684 Expect(err).ToNot(HaveOccurred())685 Expect(cred.Id).To(Equal("some-id"))686 Expect(cred.Name).To(Equal("/example-value"))687 Expect(cred.Type).To(Equal("value"))688 Expect(cred.Value).To(Equal(values.Value("some-value")))689 Expect(cred.Metadata).To(Equal(credentials.Metadata{"some": "metadata"}))690 Expect(cred.VersionCreatedAt).To(Equal("2017-01-05T01:01:01Z"))691 })692 })693 })694 var listOfActions = []TableEntry{695 Entry("GetAllVersions", func(ch *CredHub) error {696 _, err := ch.GetAllVersions("/example-password")697 return err698 }),699 Entry("GetNVersions", func(ch *CredHub) error {700 _, err := ch.GetNVersions("/example-password", 47)701 return err702 }),703 Entry("GetLatestVersion", func(ch *CredHub) error {...

Full Screen

Full Screen

set_test.go

Source:set_test.go Github

copy

Full Screen

...15 . "code.cloudfoundry.org/credhub-cli/credhub"16 "code.cloudfoundry.org/credhub-cli/credhub/credentials/values"17)18var _ = Describe("Set", func() {19 withMetadata := func(metadata credentials.Metadata) func(s *SetOptions) error {20 return func(s *SetOptions) error {21 s.Metadata = metadata22 return nil23 }24 }25 Describe("SetCredential()", func() {26 It("returns the credential that has been set", func() {27 metadataStr := `{"some":{"json":"metadata"}}`28 var metadata credentials.Metadata29 Expect(json.Unmarshal([]byte(metadataStr), &metadata)).To(Succeed())30 dummy := &DummyAuth{Response: &http.Response{31 StatusCode: http.StatusOK,32 Body: ioutil.NopCloser(bytes.NewBufferString(`{33 "id": "some-credential",34 "name": "some-credential",35 "type": "some-type",36 "value": "some-value",37 "metadata": {"some":{"json":"metadata"}},38 "version_created_at": "2017-01-01T04:07:18Z"39 }`)),40 }}41 ch, err := New("https://example.com", Auth(dummy.Builder()), ServerVersion("2.6.0"))42 Expect(err).ToNot(HaveOccurred())43 cred, err := ch.SetCredential("some-credential", "some-type", "some-value", withMetadata(metadata))44 Expect(err).ToNot(HaveOccurred())45 requestBody := getBody(dummy.Request.Body)46 Expect(requestBody["name"]).To(Equal("some-credential"))47 Expect(requestBody["type"]).To(Equal("some-type"))48 Expect(requestBody["value"]).To(BeEquivalentTo("some-value"))49 Expect(requestBody["metadata"]).To(BeEquivalentTo(metadata))50 Expect(cred.Name).To(Equal("some-credential"))51 Expect(cred.Type).To(Equal("some-type"))52 Expect(cred.Value).To(BeAssignableToTypeOf(""))53 Expect(cred.Value).To(BeEquivalentTo("some-value"))54 Expect(cred.Metadata).To(Equal(metadata))55 })56 It("sends a request for server version when server version is not provided", func() {57 server := ghttp.NewServer()58 server.AppendHandlers(59 ghttp.CombineHandlers(60 ghttp.VerifyRequest("GET", "/info"),61 ghttp.RespondWith(http.StatusOK, `{}`),62 ),63 ghttp.CombineHandlers(64 ghttp.VerifyRequest("GET", "/version"),65 ghttp.RespondWith(http.StatusOK, `{"version": "1.9.0"}`),66 ),67 ghttp.CombineHandlers(68 ghttp.VerifyRequest("PUT", "/api/v1/data"),69 ghttp.RespondWith(http.StatusOK, `{}`),70 ),71 )72 ch, err := New(server.URL())73 Expect(err).ToNot(HaveOccurred())74 _, err = ch.SetCredential("some-credential", "some-type", "some-value")75 Expect(err).NotTo(HaveOccurred())76 })77 It("sets overwrite mode when server version is older than 2.x", func() {78 dummy := &DummyAuth{Response: &http.Response{79 StatusCode: http.StatusOK,80 Body: ioutil.NopCloser(bytes.NewBufferString("{}")),81 },82 Error: nil,83 }84 version := fmt.Sprintf("1.%d.0", rand.Intn(10))85 ch, err := New("https://example.com", Auth(dummy.Builder()), ServerVersion(version))86 Expect(err).ToNot(HaveOccurred())87 _, err = ch.SetCredential("some-credential", "some-type", "some-value")88 Expect(err).NotTo(HaveOccurred())89 var requestBody map[string]interface{}90 body, err := ioutil.ReadAll(dummy.Request.Body)91 Expect(json.Unmarshal(body, &requestBody)).To(Succeed())92 Expect(requestBody["mode"]).To(Equal("overwrite"))93 })94 It("returns an error when server version is invalid", func() {95 ch, err := New("https://example.com", ServerVersion("invalid-version"))96 Expect(err).ToNot(HaveOccurred())97 _, err = ch.SetCredential("some-credential", "some-type", "some-value")98 Expect(err).To(MatchError("Malformed version: invalid-version"))99 })100 It("returns an error when request fails", func() {101 dummy := &DummyAuth{Error: errors.New("network error occurred")}102 ch, err := New("https://example.com", Auth(dummy.Builder()), ServerVersion("2.6.0"))103 Expect(err).ToNot(HaveOccurred())104 _, err = ch.SetCredential("some-credential", "some-type", "some-value")105 Expect(err).To(MatchError("network error occurred"))106 })107 It("returns an error when response body cannot be unmarshalled", func() {108 dummy := &DummyAuth{Response: &http.Response{109 Body: ioutil.NopCloser(bytes.NewBufferString("something-invalid")),110 }}111 ch, err := New("https://example.com", Auth(dummy.Builder()), ServerVersion("2.6.0"))112 Expect(err).ToNot(HaveOccurred())113 _, err = ch.SetCredential("some-credential", "some-type", "some-value")114 Expect(err).To(MatchError(ContainSubstring("invalid character 's'")))115 })116 })117 Describe("SetCertificate()", func() {118 It("returns the credential that has been set", func() {119 metadataStr := `{"some":{"json":"metadata"}}`120 var metadata credentials.Metadata121 Expect(json.Unmarshal([]byte(metadataStr), &metadata)).To(Succeed())122 dummy := &DummyAuth{Response: &http.Response{123 StatusCode: http.StatusOK,124 Body: ioutil.NopCloser(bytes.NewBufferString(`{125 "id": "some-id",126 "name": "/example-certificate",127 "type": "certificate",128 "value": {129 "ca": "some-ca",130 "certificate": "some-certificate",131 "private_key": "some-private-key"132 },133 "metadata": {"some":{"json":"metadata"}},134 "version_created_at": "2017-01-01T04:07:18Z"135 }`)),136 }}137 ch, err := New("https://example.com", Auth(dummy.Builder()), ServerVersion("2.6.0"))138 Expect(err).NotTo(HaveOccurred())139 certificate := values.Certificate{140 Certificate: "some-certificate",141 }142 cred, err := ch.SetCertificate("/example-certificate", certificate, withMetadata(metadata))143 Expect(err).NotTo(HaveOccurred())144 requestBody := getBody(dummy.Request.Body)145 Expect(requestBody["name"]).To(Equal("/example-certificate"))146 Expect(requestBody["type"]).To(Equal("certificate"))147 Expect(requestBody["value"]).To(HaveKeyWithValue("ca", ""))148 Expect(requestBody["value"]).To(HaveKeyWithValue("certificate", "some-certificate"))149 Expect(requestBody["value"]).To(HaveKeyWithValue("private_key", ""))150 Expect(requestBody["metadata"]).To(BeEquivalentTo(metadata))151 Expect(cred.Name).To(Equal("/example-certificate"))152 Expect(cred.Type).To(Equal("certificate"))153 Expect(cred.Value.Ca).To(Equal("some-ca"))154 Expect(cred.Value.Certificate).To(Equal("some-certificate"))155 Expect(cred.Value.PrivateKey).To(Equal("some-private-key"))156 Expect(cred.Metadata).To(Equal(metadata))157 })158 It("returns an error when request fails", func() {159 dummy := &DummyAuth{Error: errors.New("network error occurred")}160 ch, err := New("https://example.com", Auth(dummy.Builder()), ServerVersion("2.6.0"))161 Expect(err).NotTo(HaveOccurred())162 certificate := values.Certificate{163 Ca: "some-ca",164 }165 _, err = ch.SetCertificate("/example-certificate", certificate)166 Expect(err).To(MatchError("network error occurred"))167 })168 })169 Describe("SetPassword()", func() {170 It("returns the credential that has been set", func() {171 metadataStr := `{"some":{"json":"metadata"}}`172 var metadata credentials.Metadata173 Expect(json.Unmarshal([]byte(metadataStr), &metadata)).To(Succeed())174 dummy := &DummyAuth{Response: &http.Response{175 StatusCode: http.StatusOK,176 Body: ioutil.NopCloser(bytes.NewBufferString(`{177 "id": "some-id",178 "name": "/example-password",179 "type": "password",180 "value": "some-password",181 "metadata": {"some":{"json":"metadata"}},182 "version_created_at": "2017-01-01T04:07:18Z"183 }`)),184 }}185 ch, err := New("https://example.com", Auth(dummy.Builder()), ServerVersion("2.6.0"))186 Expect(err).NotTo(HaveOccurred())187 password := values.Password("some-password")188 cred, err := ch.SetPassword("/example-password", password, withMetadata(metadata))189 Expect(err).NotTo(HaveOccurred())190 requestBody := getBody(dummy.Request.Body)191 Expect(requestBody["name"]).To(Equal("/example-password"))192 Expect(requestBody["type"]).To(Equal("password"))193 Expect(requestBody["value"]).To(Equal("some-password"))194 Expect(requestBody["metadata"]).To(BeEquivalentTo(metadata))195 Expect(cred.Name).To(Equal("/example-password"))196 Expect(cred.Type).To(Equal("password"))197 Expect(cred.Value).To(BeEquivalentTo("some-password"))198 Expect(cred.Metadata).To(Equal(metadata))199 })200 It("returns an error when request fails", func() {201 dummy := &DummyAuth{Error: errors.New("network error occurred")}202 ch, err := New("https://example.com", Auth(dummy.Builder()), ServerVersion("2.6.0"))203 Expect(err).NotTo(HaveOccurred())204 password := values.Password("some-password")205 _, err = ch.SetPassword("/example-password", password)206 Expect(err).To(MatchError("network error occurred"))207 })208 })209 Describe("SetUser()", func() {210 It("returns the credential that has been set", func() {211 metadataStr := `{"some":{"json":"metadata"}}`212 var metadata credentials.Metadata213 Expect(json.Unmarshal([]byte(metadataStr), &metadata)).To(Succeed())214 dummy := &DummyAuth{Response: &http.Response{215 StatusCode: http.StatusOK,216 Body: ioutil.NopCloser(bytes.NewBufferString(`217 {218 "id": "67fc3def-bbfb-4953-83f8-4ab0682ad675",219 "name": "/example-user",220 "type": "user",221 "value": {222 "username": "FQnwWoxgSrDuqDLmeLpU",223 "password": "6mRPZB3bAfb8lRpacnXsHfDhlPqFcjH2h9YDvLpL",224 "password_hash": "some-hash"225 },226 "metadata": {"some":{"json":"metadata"}},227 "version_created_at": "2017-01-05T01:01:01Z"228 }`)),229 }}230 ch, err := New("https://example.com", Auth(dummy.Builder()), ServerVersion("2.6.0"))231 Expect(err).NotTo(HaveOccurred())232 user := values.User{Username: "some-username", Password: "some-password"}233 cred, err := ch.SetUser("/example-user", user, withMetadata(metadata))234 Expect(err).NotTo(HaveOccurred())235 requestBody := getBody(dummy.Request.Body)236 Expect(requestBody["name"]).To(Equal("/example-user"))237 Expect(requestBody["type"]).To(Equal("user"))238 Expect(requestBody["value"]).To(HaveKeyWithValue("username", "some-username"))239 Expect(requestBody["value"]).To(HaveKeyWithValue("password", "some-password"))240 Expect(requestBody["metadata"]).To(BeEquivalentTo(metadata))241 Expect(cred.Name).To(Equal("/example-user"))242 Expect(cred.Type).To(Equal("user"))243 alternateUsername := "FQnwWoxgSrDuqDLmeLpU"244 Expect(cred.Value.User).To(Equal(values.User{245 Username: alternateUsername,246 Password: "6mRPZB3bAfb8lRpacnXsHfDhlPqFcjH2h9YDvLpL",247 }))248 Expect(cred.Value.PasswordHash).To(Equal("some-hash"))249 Expect(cred.Metadata).To(Equal(metadata))250 })251 It("returns an error", func() {252 dummy := &DummyAuth{Error: errors.New("network error occurred")}253 ch, err := New("https://example.com", Auth(dummy.Builder()), ServerVersion("2.6.0"))254 Expect(err).NotTo(HaveOccurred())255 user := values.User{Username: "username", Password: "some-password"}256 _, err = ch.SetUser("/example-user", user)257 Expect(err).To(MatchError("network error occurred"))258 })259 })260 Describe("SetRSA()", func() {261 It("returns the credential that has been set", func() {262 metadataStr := `{"some":{"json":"metadata"}}`263 var metadata credentials.Metadata264 Expect(json.Unmarshal([]byte(metadataStr), &metadata)).To(Succeed())265 dummy := &DummyAuth{Response: &http.Response{266 StatusCode: http.StatusOK,267 Body: ioutil.NopCloser(bytes.NewBufferString(`268 {269 "id": "67fc3def-bbfb-4953-83f8-4ab0682ad676",270 "name": "/example-rsa",271 "type": "rsa",272 "value": {273 "public_key": "public-key",274 "private_key": "private-key"275 },276 "metadata": {"some":{"json":"metadata"}},277 "version_created_at": "2017-01-01T04:07:18Z"278 }`)),279 }}280 ch, err := New("https://example.com", Auth(dummy.Builder()), ServerVersion("2.6.0"))281 Expect(err).NotTo(HaveOccurred())282 cred, err := ch.SetRSA("/example-rsa", values.RSA{}, withMetadata(metadata))283 Expect(err).NotTo(HaveOccurred())284 requestBody := getBody(dummy.Request.Body)285 Expect(requestBody["name"]).To(Equal("/example-rsa"))286 Expect(requestBody["type"]).To(Equal("rsa"))287 Expect(requestBody["value"]).To(HaveKeyWithValue("private_key", ""))288 Expect(requestBody["value"]).To(HaveKeyWithValue("public_key", ""))289 Expect(requestBody["metadata"]).To(BeEquivalentTo(metadata))290 Expect(cred.Name).To(Equal("/example-rsa"))291 Expect(cred.Type).To(Equal("rsa"))292 Expect(cred.Value).To(Equal(values.RSA{293 PrivateKey: "private-key",294 PublicKey: "public-key",295 }))296 Expect(cred.Metadata).To(Equal(metadata))297 })298 It("returns an error", func() {299 dummy := &DummyAuth{Error: errors.New("network error occurred")}300 ch, err := New("https://example.com", Auth(dummy.Builder()), ServerVersion("2.6.0"))301 Expect(err).NotTo(HaveOccurred())302 _, err = ch.SetRSA("/example-rsa", values.RSA{})303 Expect(err).To(MatchError("network error occurred"))304 })305 })306 Describe("SetSSH()", func() {307 It("returns the credential that has been set", func() {308 metadataStr := `{"some":{"json":"metadata"}}`309 var metadata credentials.Metadata310 Expect(json.Unmarshal([]byte(metadataStr), &metadata)).To(Succeed())311 dummy := &DummyAuth{Response: &http.Response{312 StatusCode: http.StatusOK,313 Body: ioutil.NopCloser(bytes.NewBufferString(`314 {315 "id": "67fc3def-bbfb-4953-83f8-4ab0682ad676",316 "name": "/example-ssh",317 "type": "ssh",318 "value": {319 "public_key": "public-key",320 "private_key": "private-key"321 },322 "metadata": {"some":{"json":"metadata"}},323 "version_created_at": "2017-01-01T04:07:18Z"324 }`)),325 }}326 ch, err := New("https://example.com", Auth(dummy.Builder()), ServerVersion("2.6.0"))327 Expect(err).NotTo(HaveOccurred())328 cred, err := ch.SetSSH("/example-ssh", values.SSH{}, withMetadata(metadata))329 Expect(err).NotTo(HaveOccurred())330 requestBody := getBody(dummy.Request.Body)331 Expect(requestBody["name"]).To(Equal("/example-ssh"))332 Expect(requestBody["type"]).To(Equal("ssh"))333 Expect(requestBody["value"]).To(HaveKeyWithValue("private_key", ""))334 Expect(requestBody["value"]).To(HaveKeyWithValue("public_key", ""))335 Expect(requestBody["metadata"]).To(BeEquivalentTo(metadata))336 Expect(cred.Name).To(Equal("/example-ssh"))337 Expect(cred.Type).To(Equal("ssh"))338 Expect(cred.Value.SSH).To(Equal(values.SSH{339 PrivateKey: "private-key",340 PublicKey: "public-key",341 }))342 Expect(cred.Metadata).To(Equal(metadata))343 })344 It("returns an error", func() {345 dummy := &DummyAuth{Error: errors.New("network error occurred")}346 ch, err := New("https://example.com", Auth(dummy.Builder()), ServerVersion("2.6.0"))347 Expect(err).NotTo(HaveOccurred())348 _, err = ch.SetSSH("/example-ssh", values.SSH{})349 Expect(err).To(MatchError("network error occurred"))350 })351 })352 Describe("SetJSON()", func() {353 It("returns the credential that has been set", func() {354 metadataStr := `{"some":{"json":"metadata"}}`355 var metadata credentials.Metadata356 Expect(json.Unmarshal([]byte(metadataStr), &metadata)).To(Succeed())357 JSONValue := `{358 "key": 123,359 "key_list": [360 "val1",361 "val2"362 ],363 "is_true": true364 }`365 var unmarshalledJSONValue values.JSON366 Expect(json.Unmarshal([]byte(JSONValue), &unmarshalledJSONValue)).To(Succeed())367 dummy := &DummyAuth{Response: &http.Response{368 StatusCode: http.StatusOK,369 Body: ioutil.NopCloser(bytes.NewBufferString(fmt.Sprintf(`370 {371 "id": "some-id",372 "name": "/example-json",373 "type": "json",374 "value": %s,375 "metadata": {"some":{"json":"metadata"}},376 "version_created_at": "2017-01-01T04:07:18Z"377 }`, JSONValue))),378 }}379 ch, err := New("https://example.com", Auth(dummy.Builder()), ServerVersion("2.6.0"))380 Expect(err).NotTo(HaveOccurred())381 cred, err := ch.SetJSON("/example-json", unmarshalledJSONValue, withMetadata(metadata))382 Expect(err).NotTo(HaveOccurred())383 requestBody := getBody(dummy.Request.Body)384 Expect(requestBody["name"]).To(Equal("/example-json"))385 Expect(requestBody["type"]).To(Equal("json"))386 Expect(requestBody["value"]).To(BeEquivalentTo(unmarshalledJSONValue))387 Expect(requestBody["metadata"]).To(BeEquivalentTo(metadata))388 Expect(cred.Name).To(Equal("/example-json"))389 Expect(cred.Type).To(Equal("json"))390 Expect(cred.Value).To(Equal(unmarshalledJSONValue))391 Expect(cred.Metadata).To(Equal(metadata))392 })393 It("returns an error when request fails", func() {394 dummy := &DummyAuth{Error: errors.New("network error occurred")}395 ch, err := New("https://example.com", Auth(dummy.Builder()), ServerVersion("2.6.0"))396 Expect(err).NotTo(HaveOccurred())397 _, err = ch.SetJSON("/example-json", nil)398 Expect(err).To(MatchError("network error occurred"))399 })400 })401 Describe("SetValue()", func() {402 It("returns the credential that has been set", func() {403 metadataStr := `{"some":{"json":"metadata"}}`404 var metadata credentials.Metadata405 Expect(json.Unmarshal([]byte(metadataStr), &metadata)).To(Succeed())406 dummy := &DummyAuth{Response: &http.Response{407 StatusCode: http.StatusOK,408 Body: ioutil.NopCloser(bytes.NewBufferString(`409 {410 "id": "some-id",411 "name": "/example-value",412 "type": "value",413 "value": "some string value",414 "metadata": {"some":{"json":"metadata"}},415 "version_created_at": "2017-01-01T04:07:18Z"416 }`)),417 }}418 ch, err := New("https://example.com", Auth(dummy.Builder()), ServerVersion("2.6.0"))419 Expect(err).NotTo(HaveOccurred())420 cred, err := ch.SetValue("/example-value", values.Value("some string value"), withMetadata(metadata))421 Expect(err).NotTo(HaveOccurred())422 requestBody := getBody(dummy.Request.Body)423 Expect(requestBody["name"]).To(Equal("/example-value"))424 Expect(requestBody["type"]).To(Equal("value"))425 Expect(requestBody["value"]).To(BeEquivalentTo("some string value"))426 Expect(requestBody["metadata"]).To(BeEquivalentTo(metadata))427 Expect(cred.Name).To(Equal("/example-value"))428 Expect(cred.Type).To(Equal("value"))429 Expect(cred.Value).To(Equal(values.Value("some string value")))430 Expect(cred.Metadata).To(Equal(metadata))431 })432 It("returns an error when request fails", func() {433 dummy := &DummyAuth{Error: errors.New("network error occurred")}434 ch, err := New("https://example.com", Auth(dummy.Builder()), ServerVersion("2.6.0"))435 Expect(err).NotTo(HaveOccurred())436 _, err = ch.SetValue("/example-value", values.Value(""))437 Expect(err).To(MatchError("network error occurred"))438 })439 })440})441func getBody(body io.ReadCloser) map[string]interface{} {442 var requestBody map[string]interface{}443 bodyBytes, err := ioutil.ReadAll(body)444 Expect(err).ToNot(HaveOccurred())...

Full Screen

Full Screen

certificates_test.go

Source:certificates_test.go Github

copy

Full Screen

...13 StatusCode: http.StatusOK,14 Body: ioutil.NopCloser(bytes.NewBufferString("")),15 }}16 ch, _ := New("https://example.com", Auth(dummy.Builder()))17 _, _ = ch.GetAllCertificatesMetadata()18 url := dummy.Request.URL.String()19 Expect(url).To(Equal("https://example.com/api/v1/certificates/"))20 Expect(dummy.Request.Method).To(Equal(http.MethodGet))21 })22 It("requests the certificates by name", func() {23 dummy := &DummyAuth{Response: &http.Response{24 StatusCode: http.StatusOK,25 Body: ioutil.NopCloser(bytes.NewBufferString("")),26 }}27 ch, _ := New("https://example.com", Auth(dummy.Builder()))28 _, _ = ch.GetCertificateMetadataByName("/example-certificate")29 url := dummy.Request.URL.String()30 Expect(url).To(Equal("https://example.com/api/v1/certificates/?name=%2Fexample-certificate"))31 Expect(dummy.Request.Method).To(Equal(http.MethodGet))32 })33 Context("getting certificate metadata", func() {34 Describe("when there is data returned", func() {35 It("marshals it properly", func() {36 responseString :=37 `{"certificates": [38 {39 "id": "some-id",40 "name": "/some-cert",41 "signed_by": "/some-cert",42 "signs": ["/another-cert"],43 "versions": [44 {45 "expiry_date": "2020-05-29T12:33:50Z",46 "id": "some-version-id",47 "transitional": false48 },49 {50 "expiry_date": "2020-05-29T12:33:50Z",51 "id": "some-other-version-id",52 "transitional": true53 }54 ]55 },56 {57 "id": "some-other-id",58 "name": "/some-other-cert",59 "signed_by": "/some-cert",60 "signs": [],61 "versions": [62 {63 "expiry_date": "2020-05-29T12:33:50Z",64 "id": "some-other-other-version-id",65 "transitional": false66 }67 ]68 }69 ]}`70 dummyAuth := &DummyAuth{Response: &http.Response{71 StatusCode: http.StatusOK,72 Body: ioutil.NopCloser(bytes.NewBufferString(responseString)),73 }}74 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))75 metadata, err := ch.GetAllCertificatesMetadata()76 Expect(err).To(BeNil())77 Expect(metadata[0].Id).To(Equal("some-id"))78 Expect(metadata[0].Name).To(Equal("/some-cert"))79 Expect(metadata[0].SignedBy).To(Equal("/some-cert"))80 Expect(metadata[0].Signs[0]).To(Equal("/another-cert"))81 Expect(metadata[0].Versions[0].Id).To(Equal("some-version-id"))82 Expect(metadata[0].Versions[0].ExpiryDate).To(Equal("2020-05-29T12:33:50Z"))83 Expect(metadata[0].Versions[0].Transitional).To(BeFalse())84 Expect(metadata[0].Versions[1].Id).To(Equal("some-other-version-id"))85 Expect(metadata[0].Versions[1].ExpiryDate).To(Equal("2020-05-29T12:33:50Z"))86 Expect(metadata[0].Versions[1].Transitional).To(BeTrue())87 Expect(metadata[1].Id).To(Equal("some-other-id"))88 Expect(metadata[1].Name).To(Equal("/some-other-cert"))89 Expect(metadata[1].SignedBy).To(Equal("/some-cert"))90 Expect(metadata[1].Signs).To(Equal([]string{}))91 Expect(metadata[1].Versions[0].Id).To(Equal("some-other-other-version-id"))92 Expect(metadata[1].Versions[0].ExpiryDate).To(Equal("2020-05-29T12:33:50Z"))93 Expect(metadata[1].Versions[0].Transitional).To(BeFalse())94 })95 })96 Describe("when no certificates are returned", func() {97 It("returns empty array", func() {98 responseString :=99 `{"certificates": []}`100 dummyAuth := &DummyAuth{Response: &http.Response{101 StatusCode: http.StatusOK,102 Body: ioutil.NopCloser(bytes.NewBufferString(responseString)),103 }}104 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))105 metadata, err := ch.GetAllCertificatesMetadata()106 Expect(err).To(BeNil())107 Expect(len(metadata)).To(Equal(0))108 })109 })110 Describe("when certificates key is missing", func() {111 It("returns empty array", func() {112 responseString :=113 `{}`114 dummyAuth := &DummyAuth{Response: &http.Response{115 StatusCode: http.StatusOK,116 Body: ioutil.NopCloser(bytes.NewBufferString(responseString)),117 }}118 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))119 metadata, err := ch.GetAllCertificatesMetadata()120 Expect(err).To(BeNil())121 Expect(len(metadata)).To(Equal(0))122 })123 })124 Describe("when response is empty", func() {125 It("returns error", func() {126 responseString := ``127 dummyAuth := &DummyAuth{Response: &http.Response{128 StatusCode: http.StatusOK,129 Body: ioutil.NopCloser(bytes.NewBufferString(responseString)),130 }}131 ch, _ := New("https://example.com", Auth(dummyAuth.Builder()))132 metadata, err := ch.GetAllCertificatesMetadata()133 Expect(err).ToNot(BeNil())134 Expect(err.Error()).To(ContainSubstring("The response body could not be decoded"))135 Expect(metadata).To(BeNil())136 })137 })138 })139})...

Full Screen

Full Screen

Metadata

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 d := dummy{}4 fmt.Println(d.Metadata())5}6import "fmt"7func main() {8 d := dummy{}9 fmt.Println(d.Metadata())10}11import "fmt"12func main() {13 d := dummy{}14 fmt.Println(d.Metadata())15}16import "fmt"17func main() {18 d := dummy{}19 fmt.Println(d.Metadata())20}21import "fmt"22func main() {23 d := dummy{}24 fmt.Println(d.Metadata())25}26import "fmt"27func main() {28 d := dummy{}29 fmt.Println(d.Metadata())30}31import "fmt"32func main() {33 d := dummy{}34 fmt.Println(d.Metadata())35}36import "fmt"37func main() {38 d := dummy{}39 fmt.Println(d.Metadata())40}41import "fmt"42func main() {43 d := dummy{}44 fmt.Println(d.Metadata())45}46import "fmt"47func main() {48 d := dummy{}49 fmt.Println(d.Metadata())50}51import "fmt"52func main() {53 d := dummy{}54 fmt.Println(d.Metadata())55}56import "fmt"57func main() {58 d := dummy{}59 fmt.Println(d.Metadata())60}61import "fmt"62func main() {63 d := dummy{}

Full Screen

Full Screen

Metadata

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 gollog.ConfigLog()4 dummy := golhttp.NewDummy()5 dummy.Metadata()6 dummy.Get()7 fmt.Println(goljson.JsonPrettify(dummy.Response))8}9import (

Full Screen

Full Screen

Metadata

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 d := dummy{}4 t := reflect.TypeOf(d)5 fmt.Println(t.Method(0))6}7{Metadata func() string 0 [0] false}8import (9func main() {10 d := dummy{}11 t := reflect.TypeOf(d)12 fmt.Println(t.Method(0).Name)13 fmt.Println(t.Method(0).Type)14}15func() string

Full Screen

Full Screen

Metadata

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 d := gol.Dummy{}4 fmt.Println(d.Metadata())5}6import (7func main() {8 d := gol.Dummy{}9 fmt.Println(d.Metadata())10}11./3.go:5: imported and not used: "github.com/abhishekkr/gol/gol"12./3.go:6: imported and not used: "github.com/abhishekkr/gol/gol"13./6.go:5: imported and not used: "github.com/abhishekkr/gol/gol"14./6.go:6: imported and not used: "github.com/abhishekkr/gol/gol"15The above code is not working because the gol is imported twice. The first gol import is not used, so it is not a problem, but the second gol is imported and used. So, it is a problem. To fix this, we can use the following code:16import (17func main() {18 d := gol.Dummy{}19 fmt.Println(d.Metadata())20}

Full Screen

Full Screen

Metadata

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Inside main function")4 dummy := dummy.Dummy{}5 fmt.Println("Dummy class object created")6 fmt.Println(dummy.Metadata())7}

Full Screen

Full Screen

Metadata

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 p := &dummy{"foo", 42}4 fmt.Println(pretty.Sprint(p))5}6&main.dummy{7}8import (9func main() {10 p := &dummy{"foo", 42}11 fmt.Println(pretty.Sprint(p))12}13&main.dummy{14}15import (16func main() {17 p := &dummy{"foo", 42}18 fmt.Println(pretty.Sprint(p))19}20&main.dummy{21}22import (23func main() {24 p := &dummy{"foo", 42}25 fmt.Println(pretty.Sprint(p))26}27&main.dummy{28}29import (30func main() {31 p := &dummy{"foo", 42}32 fmt.Println(pretty.Sprint(p))33}34&main.dummy{35}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testkube automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful