github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/uaa/error_converter_test.go (about)

     1  package uaa_test
     2  
     3  import (
     4  	"net/http"
     5  
     6  	. "code.cloudfoundry.org/cli/api/uaa"
     7  	"code.cloudfoundry.org/cli/api/uaa/uaafakes"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  )
    11  
    12  var _ = Describe("Error Wrapper", func() {
    13  	var (
    14  		fakeConnection    *uaafakes.FakeConnection
    15  		wrapper           Connection
    16  		request           *http.Request
    17  		response          *Response
    18  		makeErr           error
    19  		fakeConnectionErr RawHTTPStatusError
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		fakeConnection = new(uaafakes.FakeConnection)
    24  		wrapper = NewErrorWrapper().Wrap(fakeConnection)
    25  		request = &http.Request{}
    26  		response = &Response{}
    27  		fakeConnectionErr = RawHTTPStatusError{}
    28  	})
    29  
    30  	JustBeforeEach(func() {
    31  		makeErr = wrapper.Make(request, response)
    32  	})
    33  
    34  	Describe("Make", func() {
    35  		Context("when the error is not from the UAA", func() {
    36  			BeforeEach(func() {
    37  				fakeConnectionErr.StatusCode = http.StatusTeapot
    38  				fakeConnectionErr.RawResponse = []byte("an error that's not from the UAA server")
    39  				fakeConnection.MakeReturns(fakeConnectionErr)
    40  			})
    41  
    42  			It("returns a RawHTTPStatusError", func() {
    43  				Expect(fakeConnection.MakeCallCount()).To(Equal(1))
    44  				requestCall, responseCall := fakeConnection.MakeArgsForCall(0)
    45  				Expect(requestCall).To(Equal(request))
    46  				Expect(responseCall).To(Equal(response))
    47  
    48  				Expect(makeErr).To(MatchError(fakeConnectionErr))
    49  			})
    50  		})
    51  
    52  		Context("when the error is from the UAA", func() {
    53  			Context("(400) Bad Request", func() {
    54  				BeforeEach(func() {
    55  					fakeConnectionErr.StatusCode = http.StatusBadRequest
    56  				})
    57  
    58  				Context("generic 400", func() {
    59  					BeforeEach(func() {
    60  						fakeConnectionErr.RawResponse = []byte(`{"error":"not invalid_scim_resource"}`)
    61  						fakeConnection.MakeReturns(fakeConnectionErr)
    62  					})
    63  
    64  					It("returns a RawHTTPStatusError", func() {
    65  						Expect(fakeConnection.MakeCallCount()).To(Equal(1))
    66  
    67  						Expect(makeErr).To(MatchError(fakeConnectionErr))
    68  					})
    69  				})
    70  
    71  				Context("invalid scim resource", func() {
    72  					BeforeEach(func() {
    73  						fakeConnectionErr.RawResponse = []byte(`{
    74    "error": "invalid_scim_resource",
    75    "error_description": "A username must be provided"
    76  }`)
    77  						fakeConnection.MakeReturns(fakeConnectionErr)
    78  					})
    79  
    80  					It("returns an InvalidAuthTokenError", func() {
    81  						Expect(fakeConnection.MakeCallCount()).To(Equal(1))
    82  
    83  						Expect(makeErr).To(MatchError(InvalidSCIMResourceError{Message: "A username must be provided"}))
    84  					})
    85  				})
    86  			})
    87  
    88  			Context("(401) Unauthorized", func() {
    89  				BeforeEach(func() {
    90  					fakeConnectionErr.StatusCode = http.StatusUnauthorized
    91  				})
    92  
    93  				Context("generic 401", func() {
    94  					BeforeEach(func() {
    95  						fakeConnectionErr.RawResponse = []byte(`{"error":"not invalid_token"}`)
    96  						fakeConnection.MakeReturns(fakeConnectionErr)
    97  					})
    98  
    99  					It("returns a RawHTTPStatusError", func() {
   100  						Expect(fakeConnection.MakeCallCount()).To(Equal(1))
   101  
   102  						Expect(makeErr).To(MatchError(fakeConnectionErr))
   103  					})
   104  				})
   105  
   106  				Context("invalid token", func() {
   107  					BeforeEach(func() {
   108  						fakeConnectionErr.RawResponse = []byte(`{
   109    "error": "invalid_token",
   110    "error_description": "your token is invalid!"
   111  }`)
   112  						fakeConnection.MakeReturns(fakeConnectionErr)
   113  					})
   114  
   115  					It("returns an InvalidAuthTokenError", func() {
   116  						Expect(fakeConnection.MakeCallCount()).To(Equal(1))
   117  
   118  						Expect(makeErr).To(MatchError(InvalidAuthTokenError{Message: "your token is invalid!"}))
   119  					})
   120  				})
   121  
   122  				Context("unauthorized", func() {
   123  					BeforeEach(func() {
   124  						fakeConnectionErr.RawResponse = []byte(`{
   125    "error": "unauthorized",
   126    "error_description": "Bad credentials"
   127  }`)
   128  						fakeConnection.MakeReturns(fakeConnectionErr)
   129  					})
   130  
   131  					It("returns a BadCredentialsError", func() {
   132  						Expect(fakeConnection.MakeCallCount()).To(Equal(1))
   133  
   134  						Expect(makeErr).To(MatchError(BadCredentialsError{Message: "Bad credentials"}))
   135  					})
   136  				})
   137  			})
   138  
   139  			Context("(403) Forbidden", func() {
   140  				BeforeEach(func() {
   141  					fakeConnectionErr.StatusCode = http.StatusForbidden
   142  				})
   143  
   144  				Context("generic 403", func() {
   145  					BeforeEach(func() {
   146  						fakeConnectionErr.RawResponse = []byte(`{"error":"not insufficient_scope"}`)
   147  						fakeConnection.MakeReturns(fakeConnectionErr)
   148  					})
   149  
   150  					It("returns a RawHTTPStatusError", func() {
   151  						Expect(fakeConnection.MakeCallCount()).To(Equal(1))
   152  
   153  						Expect(makeErr).To(MatchError(fakeConnectionErr))
   154  					})
   155  				})
   156  
   157  				Context("insufficient scope", func() {
   158  					BeforeEach(func() {
   159  						fakeConnectionErr.RawResponse = []byte(`
   160  							{
   161  								"error": "insufficient_scope",
   162  								"error_description": "Insufficient scope for this resource",
   163  								"scope": "admin scim.write scim.create zones.admin"
   164  							}
   165  `)
   166  						fakeConnection.MakeReturns(fakeConnectionErr)
   167  					})
   168  
   169  					It("returns an InsufficientScopeError", func() {
   170  						Expect(fakeConnection.MakeCallCount()).To(Equal(1))
   171  
   172  						Expect(makeErr).To(MatchError(InsufficientScopeError{Message: "Insufficient scope for this resource"}))
   173  					})
   174  				})
   175  			})
   176  
   177  			Context("(409) Conflict", func() {
   178  				BeforeEach(func() {
   179  					fakeConnectionErr.StatusCode = http.StatusConflict
   180  					fakeConnectionErr.RawResponse = []byte(`{
   181  	"error": "scim_resource_already_exists",
   182    "error_description": "Username already in use: some-user"
   183  }`)
   184  					fakeConnection.MakeReturns(fakeConnectionErr)
   185  				})
   186  
   187  				It("returns a ConflictError", func() {
   188  					Expect(fakeConnection.MakeCallCount()).To(Equal(1))
   189  
   190  					Expect(makeErr).To(MatchError(ConflictError{Message: "Username already in use: some-user"}))
   191  				})
   192  			})
   193  
   194  			Context("unhandled Error Codes", func() {
   195  				BeforeEach(func() {
   196  					fakeConnectionErr.StatusCode = http.StatusTeapot
   197  					fakeConnectionErr.RawResponse = []byte(`{"error":"some-teapot-error"}`)
   198  					fakeConnection.MakeReturns(fakeConnectionErr)
   199  				})
   200  
   201  				It("returns a RawHTTPStatusError", func() {
   202  					Expect(fakeConnection.MakeCallCount()).To(Equal(1))
   203  
   204  					Expect(makeErr).To(MatchError(fakeConnectionErr))
   205  				})
   206  			})
   207  		})
   208  	})
   209  })