github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/api/log_level_test.go (about)

     1  package api_test
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"net/http"
     7  
     8  	"code.cloudfoundry.org/lager"
     9  	"github.com/pf-qiu/concourse/v6/atc"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var _ = Describe("Log Level API", func() {
    15  	Describe("PUT /api/v1/log-level", func() {
    16  		var (
    17  			logLevelPayload string
    18  
    19  			response *http.Response
    20  		)
    21  
    22  		BeforeEach(func() {
    23  			logLevelPayload = ""
    24  		})
    25  
    26  		JustBeforeEach(func() {
    27  			req, err := http.NewRequest("PUT", server.URL+"/api/v1/log-level", bytes.NewBufferString(logLevelPayload))
    28  			Expect(err).NotTo(HaveOccurred())
    29  
    30  			response, err = client.Do(req)
    31  			Expect(err).NotTo(HaveOccurred())
    32  		})
    33  
    34  		Context("when authenticated", func() {
    35  			BeforeEach(func() {
    36  				fakeAccess.IsAuthenticatedReturns(true)
    37  			})
    38  
    39  			Context("is admin", func() {
    40  				BeforeEach(func() {
    41  					fakeAccess.IsAdminReturns(true)
    42  				})
    43  
    44  				for x, y := range map[atc.LogLevel]lager.LogLevel{
    45  					atc.LogLevelDebug: lager.DEBUG,
    46  				} {
    47  					atcLevel := x
    48  					lagerLevel := y
    49  
    50  					Context("when the level is "+string(atcLevel), func() {
    51  						BeforeEach(func() {
    52  							logLevelPayload = string(atcLevel)
    53  						})
    54  
    55  						It("sets the level to "+string(atcLevel), func() {
    56  							Expect(sink.GetMinLevel()).To(Equal(lagerLevel))
    57  						})
    58  
    59  						Describe("GET /api/v1/log-level", func() {
    60  							var (
    61  								getResponse *http.Response
    62  							)
    63  
    64  							JustBeforeEach(func() {
    65  								req, err := http.NewRequest("GET", server.URL+"/api/v1/log-level", nil)
    66  								Expect(err).NotTo(HaveOccurred())
    67  
    68  								getResponse, err = client.Do(req)
    69  								Expect(err).NotTo(HaveOccurred())
    70  							})
    71  
    72  							It("returns 200", func() {
    73  								Expect(getResponse.StatusCode).To(Equal(http.StatusOK))
    74  							})
    75  
    76  							It("returns the current log level", func() {
    77  								Expect(ioutil.ReadAll(getResponse.Body)).To(Equal([]byte(atcLevel)))
    78  							})
    79  						})
    80  					})
    81  				}
    82  
    83  				Context("when the level is bogus", func() {
    84  					BeforeEach(func() {
    85  						logLevelPayload = "bogus"
    86  					})
    87  
    88  					It("returns Bad Request", func() {
    89  						Expect(response.StatusCode).To(Equal(http.StatusBadRequest))
    90  					})
    91  				})
    92  			})
    93  
    94  			Context("is not admin", func() {
    95  				It("return 403 Forbidden", func() {
    96  					Expect(response.StatusCode).To(Equal(http.StatusForbidden))
    97  				})
    98  			})
    99  
   100  		})
   101  
   102  		Context("when not authenticated", func() {
   103  			BeforeEach(func() {
   104  				fakeAccess.IsAuthenticatedReturns(false)
   105  			})
   106  
   107  			It("returns 401", func() {
   108  				Expect(response.StatusCode).To(Equal(http.StatusUnauthorized))
   109  			})
   110  		})
   111  	})
   112  })