github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/cf/api/appevents/app_events_test.go (about) 1 package appevents_test 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 7 "time" 8 9 . "code.cloudfoundry.org/cli/cf/api/appevents" 10 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 11 "code.cloudfoundry.org/cli/cf/models" 12 "code.cloudfoundry.org/cli/cf/net" 13 "code.cloudfoundry.org/cli/cf/terminal/terminalfakes" 14 "code.cloudfoundry.org/cli/cf/trace/tracefakes" 15 testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration" 16 testnet "code.cloudfoundry.org/cli/util/testhelpers/net" 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 ) 20 21 var _ = Describe("App Events Repo", func() { 22 var ( 23 server *httptest.Server 24 handler *testnet.TestHandler 25 config coreconfig.ReadWriter 26 repo Repository 27 ) 28 29 BeforeEach(func() { 30 config = testconfig.NewRepository() 31 config.SetAccessToken("BEARER my_access_token") 32 config.SetAPIVersion("2.2.0") 33 }) 34 35 JustBeforeEach(func() { 36 gateway := net.NewCloudControllerGateway(config, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "") 37 repo = NewCloudControllerAppEventsRepository(config, gateway) 38 }) 39 40 AfterEach(func() { 41 server.Close() 42 }) 43 44 setupTestServer := func(requests ...testnet.TestRequest) { 45 server, handler = testnet.NewServer(requests) 46 config.SetAPIEndpoint(server.URL) 47 } 48 49 Describe("list recent events", func() { 50 It("returns the most recent events", func() { 51 setupTestServer(eventsRequest) 52 53 list, err := repo.RecentEvents("my-app-guid", 2) 54 Expect(err).ToNot(HaveOccurred()) 55 timestamp, err := time.Parse(eventTimestampFormat, "2014-01-21T00:20:11+00:00") 56 Expect(err).ToNot(HaveOccurred()) 57 58 Expect(list).To(ConsistOf([]models.EventFields{ 59 { 60 GUID: "event-1-guid", 61 Name: "audit.app.update", 62 Timestamp: timestamp, 63 Description: "instances: 1, memory: 256, command: PRIVATE DATA HIDDEN, environment_json: PRIVATE DATA HIDDEN", 64 Actor: "cf-1-client", 65 ActorName: "somebody@pivotallabs.com", 66 }, 67 { 68 GUID: "event-2-guid", 69 Name: "audit.app.update", 70 Timestamp: timestamp, 71 Description: "instances: 1, memory: 256, command: PRIVATE DATA HIDDEN, environment_json: PRIVATE DATA HIDDEN", 72 Actor: "cf-2-client", 73 ActorName: "nobody@pivotallabs.com", 74 }, 75 })) 76 }) 77 }) 78 }) 79 80 const eventTimestampFormat = "2006-01-02T15:04:05-07:00" 81 82 var eventsRequest = testnet.TestRequest{ 83 Method: "GET", 84 Path: "/v2/events?q=actee%3Amy-app-guid&order-direction=desc&results-per-page=2", 85 Response: testnet.TestResponse{ 86 Status: http.StatusOK, 87 Body: `{ 88 "total_results": 1, 89 "total_pages": 1, 90 "prev_url": null, 91 "next_url": "/v2/events?q=actee%3Amy-app-guid&page=2", 92 "resources": [ 93 { 94 "metadata": { 95 "guid": "event-1-guid" 96 }, 97 "entity": { 98 "type": "audit.app.update", 99 "timestamp": "2014-01-21T00:20:11+00:00", 100 "actor": "cf-1-client", 101 "actor_name": "somebody@pivotallabs.com", 102 "metadata": { 103 "request": { 104 "command": "PRIVATE DATA HIDDEN", 105 "instances": 1, 106 "memory": 256, 107 "name": "dora", 108 "environment_json": "PRIVATE DATA HIDDEN" 109 } 110 } 111 } 112 }, 113 { 114 "metadata": { 115 "guid": "event-2-guid" 116 }, 117 "entity": { 118 "type": "audit.app.update", 119 "actor": "cf-2-client", 120 "actor_name": "nobody@pivotallabs.com", 121 "timestamp": "2014-01-21T00:20:11+00:00", 122 "metadata": { 123 "request": { 124 "command": "PRIVATE DATA HIDDEN", 125 "instances": 1, 126 "memory": 256, 127 "name": "dora", 128 "environment_json": "PRIVATE DATA HIDDEN" 129 } 130 } 131 } 132 } 133 ] 134 }`}}