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