github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/api/app_events/app_events_test.go (about) 1 package app_events_test 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "time" 7 8 . "github.com/cloudfoundry/cli/cf/api/app_events" 9 "github.com/cloudfoundry/cli/cf/api/strategy" 10 "github.com/cloudfoundry/cli/cf/configuration/core_config" 11 "github.com/cloudfoundry/cli/cf/models" 12 "github.com/cloudfoundry/cli/cf/net" 13 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 14 testnet "github.com/cloudfoundry/cli/testhelpers/net" 15 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 16 testtime "github.com/cloudfoundry/cli/testhelpers/time" 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 core_config.ReadWriter 26 repo AppEventsRepository 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 strategy := strategy.NewEndpointStrategy(config.ApiVersion()) 37 gateway := net.NewCloudControllerGateway(config, time.Now, &testterm.FakeUI{}) 38 repo = NewCloudControllerAppEventsRepository(config, gateway, strategy) 39 }) 40 41 AfterEach(func() { 42 server.Close() 43 }) 44 45 setupTestServer := func(requests ...testnet.TestRequest) { 46 server, handler = testnet.NewServer(requests) 47 config.SetApiEndpoint(server.URL) 48 } 49 50 Describe("list recent events", func() { 51 It("returns the most recent events", func() { 52 setupTestServer(eventsRequest) 53 54 list, err := repo.RecentEvents("my-app-guid", 2) 55 Expect(err).ToNot(HaveOccurred()) 56 57 Expect(list).To(ConsistOf([]models.EventFields{ 58 models.EventFields{ 59 Guid: "event-1-guid", 60 Name: "audit.app.update", 61 Timestamp: testtime.MustParse(eventTimestampFormat, "2014-01-21T00:20:11+00:00"), 62 Description: "instances: 1, memory: 256, command: PRIVATE DATA HIDDEN, environment_json: PRIVATE DATA HIDDEN", 63 ActorName: "somebody@pivotallabs.com", 64 }, 65 models.EventFields{ 66 Guid: "event-2-guid", 67 Name: "audit.app.update", 68 Timestamp: testtime.MustParse(eventTimestampFormat, "2014-01-21T00:20:11+00:00"), 69 Description: "instances: 1, memory: 256, command: PRIVATE DATA HIDDEN, environment_json: PRIVATE DATA HIDDEN", 70 ActorName: "nobody@pivotallabs.com", 71 }, 72 })) 73 }) 74 }) 75 }) 76 77 const eventTimestampFormat = "2006-01-02T15:04:05-07:00" 78 79 var eventsRequest = testnet.TestRequest{ 80 Method: "GET", 81 Path: "/v2/events?q=actee%3Amy-app-guid&order-direction=desc&results-per-page=2", 82 Response: testnet.TestResponse{ 83 Status: http.StatusOK, 84 Body: `{ 85 "total_results": 1, 86 "total_pages": 1, 87 "prev_url": null, 88 "next_url": "/v2/events?q=actee%3Amy-app-guid&page=2", 89 "resources": [ 90 { 91 "metadata": { 92 "guid": "event-1-guid" 93 }, 94 "entity": { 95 "type": "audit.app.update", 96 "timestamp": "2014-01-21T00:20:11+00:00", 97 "actor_name": "somebody@pivotallabs.com", 98 "metadata": { 99 "request": { 100 "command": "PRIVATE DATA HIDDEN", 101 "instances": 1, 102 "memory": 256, 103 "name": "dora", 104 "environment_json": "PRIVATE DATA HIDDEN" 105 } 106 } 107 } 108 }, 109 { 110 "metadata": { 111 "guid": "event-2-guid" 112 }, 113 "entity": { 114 "type": "audit.app.update", 115 "actor_name": "nobody@pivotallabs.com", 116 "timestamp": "2014-01-21T00:20:11+00:00", 117 "metadata": { 118 "request": { 119 "command": "PRIVATE DATA HIDDEN", 120 "instances": 1, 121 "memory": 256, 122 "name": "dora", 123 "environment_json": "PRIVATE DATA HIDDEN" 124 } 125 } 126 } 127 } 128 ] 129 }`}}