github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/api/wall_test.go (about) 1 package api_test 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "io/ioutil" 7 "net/http" 8 "time" 9 10 "github.com/pf-qiu/concourse/v6/atc" 11 . "github.com/pf-qiu/concourse/v6/atc/testhelpers" 12 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 ) 16 17 var _ = Describe("Wall API", func() { 18 var response *http.Response 19 Context("Gets a wall message", func() { 20 BeforeEach(func() { 21 dbWall.GetWallReturns(atc.Wall{Message: "test message"}, nil) 22 }) 23 24 JustBeforeEach(func() { 25 req, err := http.NewRequest("GET", server.URL+"/api/v1/wall", nil) 26 Expect(err).NotTo(HaveOccurred()) 27 28 response, err = client.Do(req) 29 Expect(err).NotTo(HaveOccurred()) 30 }) 31 32 It("returns 200", func() { 33 Expect(response.StatusCode).To(Equal(http.StatusOK)) 34 }) 35 36 It("returns Content-Type 'application/json'", func() { 37 expectedHeaderEntries := map[string]string{ 38 "Content-Type": "application/json", 39 } 40 Expect(response).Should(IncludeHeaderEntries(expectedHeaderEntries)) 41 }) 42 43 Context("the message does not expire", func() { 44 45 It("returns only message", func() { 46 Expect(dbWall.GetWallCallCount()).To(Equal(1)) 47 Expect(ioutil.ReadAll(response.Body)).To(MatchJSON(`{"message":"test message"}`)) 48 }) 49 }) 50 51 Context("and the message does expire", func() { 52 var ( 53 expectedDuration time.Duration 54 ) 55 BeforeEach(func() { 56 expiresAt := time.Now().Add(time.Minute) 57 expectedDuration = time.Until(expiresAt) 58 dbWall.GetWallReturns(atc.Wall{Message: "test message", TTL: expectedDuration}, nil) 59 }) 60 61 It("returns the expiration with the message", func() { 62 Expect(dbWall.GetWallCallCount()).To(Equal(1)) 63 64 var msg atc.Wall 65 err := json.NewDecoder(response.Body).Decode(&msg) 66 Expect(err).ToNot(HaveOccurred()) 67 Expect(msg).To(Equal(atc.Wall{ 68 Message: "test message", 69 TTL: expectedDuration, 70 })) 71 }) 72 }) 73 }) 74 75 Context("Sets a wall message", func() { 76 var expectedWall atc.Wall 77 BeforeEach(func() { 78 expectedWall = atc.Wall{ 79 Message: "test message", 80 TTL: time.Minute, 81 } 82 83 dbWall.SetWallReturns(nil) 84 }) 85 86 JustBeforeEach(func() { 87 payload, err := json.Marshal(expectedWall) 88 Expect(err).NotTo(HaveOccurred()) 89 90 req, err := http.NewRequest("PUT", server.URL+"/api/v1/wall", 91 ioutil.NopCloser(bytes.NewBuffer(payload))) 92 Expect(err).NotTo(HaveOccurred()) 93 94 response, err = client.Do(req) 95 Expect(err).NotTo(HaveOccurred()) 96 }) 97 98 Context("when authenticated", func() { 99 BeforeEach(func() { 100 fakeAccess.IsAuthenticatedReturns(true) 101 }) 102 103 Context("and is admin", func() { 104 BeforeEach(func() { 105 fakeAccess.IsAdminReturns(true) 106 }) 107 108 It("returns 200", func() { 109 Expect(response.StatusCode).To(Equal(http.StatusOK)) 110 }) 111 112 It("sets the message and expiration", func() { 113 Expect(dbWall.SetWallCallCount()).To(Equal(1)) 114 Expect(dbWall.SetWallArgsForCall(0)).To(Equal(expectedWall)) 115 }) 116 }) 117 118 Context("and is not admin", func() { 119 BeforeEach(func() { 120 fakeAccess.IsAdminReturns(false) 121 }) 122 123 It("returns 403", func() { 124 Expect(response.StatusCode).To(Equal(http.StatusForbidden)) 125 }) 126 }) 127 }) 128 129 Context("when not authenticated", func() { 130 BeforeEach(func() { 131 fakeAccess.IsAuthenticatedReturns(false) 132 }) 133 134 It("returns 401", func() { 135 Expect(response.StatusCode).To(Equal(http.StatusUnauthorized)) 136 }) 137 }) 138 }) 139 140 Context("Clears the wall message", func() { 141 JustBeforeEach(func() { 142 req, err := http.NewRequest("DELETE", server.URL+"/api/v1/wall", nil) 143 Expect(err).NotTo(HaveOccurred()) 144 145 response, err = client.Do(req) 146 Expect(err).NotTo(HaveOccurred()) 147 }) 148 149 Context("when authenticated", func() { 150 BeforeEach(func() { 151 fakeAccess.IsAuthenticatedReturns(true) 152 }) 153 154 Context("is an admin", func() { 155 BeforeEach(func() { 156 fakeAccess.IsAdminReturns(true) 157 }) 158 159 It("returns 200", func() { 160 Expect(response.StatusCode).To(Equal(http.StatusOK)) 161 }) 162 163 It("makes the Clear database call", func() { 164 Expect(dbWall.ClearCallCount()).To(Equal(1)) 165 }) 166 }) 167 Context("is not an admin", func() { 168 BeforeEach(func() { 169 fakeAccess.IsAdminReturns(false) 170 }) 171 172 It("returns 403", func() { 173 Expect(response.StatusCode).To(Equal(http.StatusForbidden)) 174 }) 175 }) 176 }) 177 Context("when not authenticated", func() { 178 BeforeEach(func() { 179 fakeAccess.IsAuthenticatedReturns(false) 180 }) 181 182 It("returns 401", func() { 183 Expect(response.StatusCode).To(Equal(http.StatusUnauthorized)) 184 }) 185 186 }) 187 188 }) 189 })