github.com/lukasheimann/cloudfoundrycli@v7.1.0+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  		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  		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 with bad credentials", 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(UnauthorizedError{Message: "Bad credentials"}))
   135  					})
   136  				})
   137  
   138  				Context("unauthorized - too many failed login attempts", func() {
   139  					BeforeEach(func() {
   140  						fakeConnectionErr.RawResponse = []byte(`{
   141    "error": "unauthorized",
   142    "error_description": "Your account has been locked because of too many failed attempts to login."
   143  }`)
   144  						fakeConnection.MakeReturns(fakeConnectionErr)
   145  					})
   146  
   147  					It("returns a BadCredentialsError", func() {
   148  						Expect(fakeConnection.MakeCallCount()).To(Equal(1))
   149  
   150  						Expect(makeErr).To(MatchError(AccountLockedError{Message: "Your account has been locked because of too many failed attempts to login."}))
   151  					})
   152  				})
   153  			})
   154  
   155  			Context("(403) Forbidden", func() {
   156  				BeforeEach(func() {
   157  					fakeConnectionErr.StatusCode = http.StatusForbidden
   158  				})
   159  
   160  				Context("generic 403", func() {
   161  					BeforeEach(func() {
   162  						fakeConnectionErr.RawResponse = []byte(`{"error":"not insufficient_scope"}`)
   163  						fakeConnection.MakeReturns(fakeConnectionErr)
   164  					})
   165  
   166  					It("returns a RawHTTPStatusError", func() {
   167  						Expect(fakeConnection.MakeCallCount()).To(Equal(1))
   168  
   169  						Expect(makeErr).To(MatchError(fakeConnectionErr))
   170  					})
   171  				})
   172  
   173  				Context("insufficient scope", func() {
   174  					BeforeEach(func() {
   175  						fakeConnectionErr.RawResponse = []byte(`
   176  							{
   177  								"error": "insufficient_scope",
   178  								"error_description": "Insufficient scope for this resource",
   179  								"scope": "admin scim.write scim.create zones.admin"
   180  							}
   181  `)
   182  						fakeConnection.MakeReturns(fakeConnectionErr)
   183  					})
   184  
   185  					It("returns an InsufficientScopeError", func() {
   186  						Expect(fakeConnection.MakeCallCount()).To(Equal(1))
   187  
   188  						Expect(makeErr).To(MatchError(InsufficientScopeError{Message: "Insufficient scope for this resource"}))
   189  					})
   190  				})
   191  			})
   192  
   193  			Context("(409) Conflict", func() {
   194  				BeforeEach(func() {
   195  					fakeConnectionErr.StatusCode = http.StatusConflict
   196  					fakeConnectionErr.RawResponse = []byte(`{
   197  	"error": "scim_resource_already_exists",
   198    "error_description": "Username already in use: some-user"
   199  }`)
   200  					fakeConnection.MakeReturns(fakeConnectionErr)
   201  				})
   202  
   203  				It("returns a ConflictError", func() {
   204  					Expect(fakeConnection.MakeCallCount()).To(Equal(1))
   205  
   206  					Expect(makeErr).To(MatchError(ConflictError{Message: "Username already in use: some-user"}))
   207  				})
   208  			})
   209  
   210  			Context("unhandled Error Codes", func() {
   211  				BeforeEach(func() {
   212  					fakeConnectionErr.StatusCode = http.StatusTeapot
   213  					fakeConnectionErr.RawResponse = []byte(`{"error":"some-teapot-error"}`)
   214  					fakeConnection.MakeReturns(fakeConnectionErr)
   215  				})
   216  
   217  				It("returns a RawHTTPStatusError", func() {
   218  					Expect(fakeConnection.MakeCallCount()).To(Equal(1))
   219  
   220  					Expect(makeErr).To(MatchError(fakeConnectionErr))
   221  				})
   222  			})
   223  		})
   224  	})
   225  })