diff --git a/endpoint_test.go b/endpoint_test.go new file mode 100644 index 0000000..24f7568 --- /dev/null +++ b/endpoint_test.go @@ -0,0 +1,54 @@ +package main + +import ( + "testing" + "github.com/microlib/usermanager/lib" + "errors" + "context" + "fmt" + "reflect" +) + +type FkUserManagerService struct{} + +func (u *FkUserManagerService) FindUser(id string) (usermanager.UserInterface, error) { + if id == "1" { + return usermanager.NewUser("1", "Karl", "karl@email.com", "mypassword"), nil + } + return &usermanager.User{}, errors.New("User not found") +} + +func TestMakeFindUserEndpoint(t *testing.T) { + testCases := []struct { + findUserReq findUserRequest + expected findUserResponse + }{ + { + findUserRequest{Id:"1"}, + findUserResponse{ + Id:"1", + Name: "Karl", + Email: "karl@email.com", + Err: nil, + }, + }, + { + findUserRequest{Id:"2"}, + findUserResponse{ + Id:"", + Name: "", + Email: "", + Err: errors.New("User not found"), + }, + }, + } + + for _, tc := range testCases { + t.Run(fmt.Sprintf("User with id '%s'", tc.findUserReq.Id), func(t *testing.T) { + res, _ := makeFindUserEndpoint(&FkUserManagerService{})(context.Background(), tc.findUserReq) + if !reflect.DeepEqual(res, tc.expected) { + t.Errorf("got %s; want %s", res, tc.expected) + } + }) + } +} diff --git a/request.go b/request.go index d6584bc..40e4da7 100644 --- a/request.go +++ b/request.go @@ -2,16 +2,12 @@ package main type findUserRequest struct { Id string `json:"id"` - Name string `json:"name"` - Email string `json:"email"` - Password string `json:"password"` } type findUserResponse struct { Id string `json:"id"` Name string `json:"name"` Email string `json:"email"` - Password string `json:"password"` Err error `json:"err,omitempty"` }