github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/ccv3/event_test.go (about) 1 package ccv3_test 2 3 import ( 4 "fmt" 5 "net/http" 6 "time" 7 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 9 10 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 . "github.com/onsi/gomega/ghttp" 14 ) 15 16 var _ = Describe("Event", func() { 17 var client *Client 18 19 BeforeEach(func() { 20 client, _ = NewTestClient() 21 }) 22 23 Describe("GetEvents", func() { 24 var ( 25 events []Event 26 warnings Warnings 27 executeErr error 28 ) 29 30 JustBeforeEach(func() { 31 events, warnings, executeErr = client.GetEvents( 32 Query{Key: TargetGUIDFilter, Values: []string{"some-target-guid"}}, 33 Query{Key: OrderBy, Values: []string{"-created_at"}}, 34 Query{Key: PerPage, Values: []string{"1"}}, 35 Query{Key: Page, Values: []string{"1"}}) 36 }) 37 38 var response string 39 BeforeEach(func() { 40 response = fmt.Sprintf(`{ 41 "pagination": { 42 "total_results": 3, 43 "total_pages": 2, 44 "next": { 45 "href": "%s/v3/audit_events?page=2&per_page=2" 46 }, 47 "previous": null 48 }, 49 "resources": [ 50 { 51 "guid": "some-event-guid", 52 "created_at": "2016-06-08T16:41:23Z", 53 "updated_at": "2016-06-08T16:41:26Z", 54 "type": "audit.app.update", 55 "actor": { 56 "guid": "d144abe3-3d7b-40d4-b63f-2584798d3ee5", 57 "type": "user", 58 "name": "admin" 59 }, 60 "target": { 61 "guid": "2e3151ba-9a63-4345-9c5b-6d8c238f4e55", 62 "type": "app", 63 "name": "my-app" 64 }, 65 "data": { 66 "request": { 67 "recursive": true 68 } 69 }, 70 "space": { 71 "guid": "cb97dd25-d4f7-4185-9e6f-ad6e585c207c" 72 }, 73 "organization": { 74 "guid": "d9be96f5-ea8f-4549-923f-bec882e32e3c" 75 }, 76 "links": { 77 "self": { 78 "href": "https://api.example.org/v3/audit_events/a595fe2f-01ff-4965-a50c-290258ab8582" 79 } 80 } 81 } 82 ] 83 }`, server.URL()) 84 }) 85 86 Context("when the event exists", func() { 87 BeforeEach(func() { 88 server.AppendHandlers( 89 CombineHandlers( 90 VerifyRequest(http.MethodGet, "/v3/audit_events", "target_guids=some-target-guid&order_by=-created_at&per_page=1&page=1"), 91 RespondWith(http.StatusAccepted, response, http.Header{"X-Cf-Warnings": {"warning"}}), 92 ), 93 ) 94 }) 95 96 It("returns the event guid of the most recent event", func() { 97 timestamp, err := time.Parse(time.RFC3339, "2016-06-08T16:41:23Z") 98 Expect(err).ToNot(HaveOccurred()) 99 Expect(executeErr).ToNot(HaveOccurred()) 100 Expect(warnings).To(ConsistOf("warning")) 101 Expect(events).To(ConsistOf( 102 Event{ 103 GUID: "some-event-guid", 104 CreatedAt: timestamp, 105 Type: "audit.app.update", 106 ActorName: "admin", 107 Data: map[string]interface{}{ 108 "request": map[string]interface{}{ 109 "recursive": true, 110 }, 111 }, 112 }, 113 )) 114 }) 115 }) 116 117 Context("when the request fails", func() { 118 BeforeEach(func() { 119 response := `{ 120 "errors": [ 121 { 122 "code": 10008, 123 "detail": "The request is semantically invalid: command presence", 124 "title": "CF-UnprocessableEntity" 125 }, 126 { 127 "code": 10010, 128 "detail": "App not found", 129 "title": "CF-ResourceNotFound" 130 } 131 ] 132 }` 133 134 server.AppendHandlers( 135 CombineHandlers( 136 VerifyRequest(http.MethodGet, "/v3/audit_events", "target_guids=some-target-guid&order_by=-created_at&per_page=1&page=1"), 137 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning"}}), 138 ), 139 ) 140 }) 141 142 It("returns CC warnings and error", func() { 143 Expect(executeErr).To(MatchError(ccerror.MultiError{ 144 ResponseCode: http.StatusTeapot, 145 Errors: []ccerror.V3Error{ 146 { 147 Code: 10008, 148 Detail: "The request is semantically invalid: command presence", 149 Title: "CF-UnprocessableEntity", 150 }, 151 { 152 Code: 10010, 153 Detail: "App not found", 154 Title: "CF-ResourceNotFound", 155 }, 156 }, 157 })) 158 Expect(warnings).To(ConsistOf("warning")) 159 }) 160 }) 161 }) 162 })