Skip to content

Commit

Permalink
GH-54: Improved test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
jirenius committed Dec 16, 2019
1 parent 4207ff0 commit 185cb20
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/02access_request_test.go
Expand Up @@ -209,3 +209,35 @@ func TestAccessDeniedHandler(t *testing.T) {
AssertError(t, res.ErrAccessDenied)
})
}

// Test that an access request without any access handler gives no response
func TestAccess_WithoutAccessHandler_SendsNoResponse(t *testing.T) {
runTest(t, func(s *Session) {
s.Handle("model", res.GetModel(func(r res.ModelRequest) {
r.Model(mock.Model)
}))
s.Handle("collection", res.Access(func(r res.AccessRequest) {
r.AccessGranted()
}))
}, func(s *Session) {
s.Request("access.test.model", mock.DefaultRequest())
inb := s.Request("access.test.collection", mock.DefaultRequest())
// Validate that the response is for the collection access, and not model access
s.GetMsg(t).AssertSubject(t, inb)
})
}

// Test that multiple responses to access request causes panic
func TestAccess_WithMultipleResponses_CausesPanic(t *testing.T) {
runTest(t, func(s *Session) {
s.Handle("model", res.Access(func(r res.AccessRequest) {
r.AccessGranted()
AssertPanic(t, func() {
r.AccessDenied()
})
}))
}, func(s *Session) {
inb := s.Request("access.test.model", mock.Request())
s.GetMsg(t).Equals(t, inb, mock.AccessGrantedResponse)
})
}
27 changes: 27 additions & 0 deletions test/03get_request_test.go
Expand Up @@ -558,3 +558,30 @@ func TestGetCollectionQuery_WithoutQueryCollection_SendsQueryCollectionResponse(
s.GetMsg(t).Equals(t, inb, mock.QueryCollectionResponse)
})
}

// Test that a get request without any get handler gives error system.notFound
func TestGet_WithoutGetHandler_SendsNotFoundError(t *testing.T) {
runTest(t, func(s *Session) {
s.Handle("model", res.Call("dummy", func(r res.CallRequest) {
r.OK(nil)
}))
}, func(s *Session) {
inb := s.Request("get.test.model", mock.Request())
s.GetMsg(t).AssertSubject(t, inb).AssertError(t, res.ErrNotFound)
})
}

// Test that multiple responses to get request causes panic
func TestGet_WithMultipleResponses_CausesPanic(t *testing.T) {
runTest(t, func(s *Session) {
s.Handle("model", res.GetModel(func(r res.ModelRequest) {
r.Model(mock.Model)
AssertPanic(t, func() {
r.NotFound()
})
}))
}, func(s *Session) {
inb := s.Request("get.test.model", mock.Request())
s.GetMsg(t).Equals(t, inb, mock.ModelResponse)
})
}
27 changes: 27 additions & 0 deletions test/04call_request_test.go
Expand Up @@ -403,3 +403,30 @@ func TestCallRequestTimeoutWithDurationLessThanZero(t *testing.T) {
s.GetMsg(t).AssertSubject(t, inb).AssertErrorCode(t, "system.internalError")
})
}

// Test call request with an unset method returns error system.methodNotFound
func TestCallRequest_UnknownMethod_ErrorMethodNotFound(t *testing.T) {
runTest(t, func(s *Session) {
s.Handle("model", res.Call("method", func(r res.CallRequest) {
r.OK(nil)
}))
}, func(s *Session) {
inb := s.Request("call.test.model.unset", nil)
s.GetMsg(t).AssertSubject(t, inb).AssertError(t, res.ErrMethodNotFound)
})
}

// Test that multiple responses to call request causes panic
func TestCall_WithMultipleResponses_CausesPanic(t *testing.T) {
runTest(t, func(s *Session) {
s.Handle("model", res.Call("method", func(r res.CallRequest) {
r.OK(nil)
AssertPanic(t, func() {
r.MethodNotFound()
})
}))
}, func(s *Session) {
inb := s.Request("call.test.model.method", mock.Request())
s.GetMsg(t).AssertSubject(t, inb).AssertResult(t, nil)
})
}
27 changes: 27 additions & 0 deletions test/06auth_request_test.go
Expand Up @@ -420,3 +420,30 @@ func TestAuthRequestNilTokenEvent(t *testing.T) {
s.GetMsg(t).AssertSubject(t, inb).AssertResult(t, nil)
})
}

// Test auth request with an unset method returns error system.methodNotFound
func TestAuthRequest_UnknownMethod_ErrorMethodNotFound(t *testing.T) {
runTest(t, func(s *Session) {
s.Handle("model", res.Auth("method", func(r res.AuthRequest) {
r.OK(nil)
}))
}, func(s *Session) {
inb := s.Request("auth.test.model.unset", mock.AuthRequest())
s.GetMsg(t).AssertSubject(t, inb).AssertError(t, res.ErrMethodNotFound)
})
}

// Test that multiple responses to auth request causes panic
func TestAuth_WithMultipleResponses_CausesPanic(t *testing.T) {
runTest(t, func(s *Session) {
s.Handle("model", res.Auth("method", func(r res.AuthRequest) {
r.OK(nil)
AssertPanic(t, func() {
r.MethodNotFound()
})
}))
}, func(s *Session) {
inb := s.Request("auth.test.model.method", mock.Request())
s.GetMsg(t).AssertSubject(t, inb).AssertResult(t, nil)
})
}
2 changes: 2 additions & 0 deletions test/resources.go
Expand Up @@ -32,6 +32,7 @@ type Mock struct {
ResultResponse json.RawMessage
CustomError *res.Error
Error error
AccessGrantedResponse json.RawMessage
// Unserializables
UnserializableValue interface{}
UnserializableError *res.Error
Expand Down Expand Up @@ -74,6 +75,7 @@ var mock = Mock{
json.RawMessage(`{"result":{"foo":"bar","zoo":42}}`), // ResultResponse
&res.Error{Code: "test.custom", Message: "Custom error", Data: map[string]string{"foo": "bar"}}, // CustomError
errors.New("custom error"), // Error
json.RawMessage(`{"result":{"get":true,"call":"*"}}`), // AccessGrantedResponse
// Unserializables
func() {}, // UnserializableValue
&res.Error{Code: "test.unserializable", Message: "Unserializable", Data: func() {}}, // UnserializableError
Expand Down

0 comments on commit 185cb20

Please sign in to comment.