-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(user): add uuid to user information (#110)
* feat: add uuid field in user * chore: update proton commit and docker-compose image * fix(asset): fix star repository getstargazers
- Loading branch information
Showing
50 changed files
with
1,240 additions
and
981 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package api | ||
|
||
type Config struct { | ||
IdentityHeaderKey string | ||
IdentityUUIDHeaderKey string | ||
IdentityEmailHeaderKey string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1,103 @@ | ||
package middleware | ||
|
||
import ( | ||
"encoding/json" | ||
"context" | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime" | ||
"github.com/odpf/columbus/api/httpapi/handlers" | ||
"github.com/odpf/columbus/lib/mocks" | ||
"github.com/odpf/columbus/user" | ||
"github.com/odpf/salt/log" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
const ( | ||
dummyRoute = "/v1beta1/dummy" | ||
identityHeaderKey = "Columbus-User-ID" | ||
dummyRoute = "/v1beta1/dummy" | ||
identityUUIDHeaderKey = "Columbus-User-ID" | ||
identityEmailHeaderKey = "Columbus-User-Email" | ||
userUUID = "user-uuid" | ||
userID = "user-id" | ||
userEmail = "some-email" | ||
) | ||
|
||
var userCfg = user.Config{IdentityProviderDefaultName: "shield"} | ||
|
||
func TestValidateUser(t *testing.T) { | ||
|
||
t.Run("should return HTTP 400 when identity header not present", func(t *testing.T) { | ||
userSvc := user.NewService(nil, userCfg) | ||
|
||
r := runtime.NewServeMux() | ||
err := r.HandlePath(http.MethodGet, dummyRoute, | ||
ValidateUser(identityHeaderKey, userSvc, nil)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
req, _ := http.NewRequest("GET", dummyRoute, nil) | ||
|
||
rr := httptest.NewRecorder() | ||
|
||
r.ServeHTTP(rr, req) | ||
|
||
assert.Equal(t, http.StatusBadRequest, rr.Code) | ||
response := &handlers.ErrorResponse{} | ||
err = json.Unmarshal(rr.Body.Bytes(), &response) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assert.Equal(t, "identity header is empty", response.Reason) | ||
}) | ||
|
||
t.Run("should return HTTP 500 when something error with user service", func(t *testing.T) { | ||
customError := errors.New("some error") | ||
mockUserRepository := &mocks.UserRepository{} | ||
mockUserRepository.On("GetID", mock.Anything, mock.Anything).Return("", customError) | ||
mockUserRepository.On("Create", mock.Anything, mock.Anything).Return("", customError) | ||
|
||
userSvc := user.NewService(mockUserRepository, userCfg) | ||
|
||
r := runtime.NewServeMux() | ||
err := r.HandlePath(http.MethodGet, dummyRoute, | ||
ValidateUser(identityHeaderKey, userSvc, nil)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
req, _ := http.NewRequest("GET", dummyRoute, nil) | ||
req.Header.Set(identityHeaderKey, "some-email") | ||
rr := httptest.NewRecorder() | ||
|
||
r.ServeHTTP(rr, req) | ||
|
||
assert.Equal(t, http.StatusInternalServerError, rr.Code) | ||
response := &handlers.ErrorResponse{} | ||
err = json.Unmarshal(rr.Body.Bytes(), &response) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assert.Equal(t, customError.Error(), response.Reason) | ||
}) | ||
|
||
t.Run("should return HTTP 200 with propagated user ID when user validation success", func(t *testing.T) { | ||
userID := "user-id" | ||
userEmail := "some-email" | ||
mockUserRepository := &mocks.UserRepository{} | ||
mockUserRepository.On("GetID", mock.Anything, mock.Anything).Return(userID, nil) | ||
mockUserRepository.On("Create", mock.Anything, mock.Anything).Return(userID, nil) | ||
|
||
userSvc := user.NewService(mockUserRepository, userCfg) | ||
|
||
r := runtime.NewServeMux() | ||
if err := r.HandlePath(http.MethodGet, dummyRoute, | ||
ValidateUser(identityHeaderKey, userSvc, runtime.HandlerFunc(func(rw http.ResponseWriter, r *http.Request, pathParams map[string]string) { | ||
type testCase struct { | ||
Description string | ||
Setup func(ctx context.Context, userRepo *mocks.UserRepository, req *http.Request) | ||
Handler runtime.HandlerFunc | ||
ExpectStatus int | ||
} | ||
|
||
var testCases = []testCase{ | ||
{ | ||
Description: "should return HTTP 400 when identity header not present", | ||
ExpectStatus: http.StatusBadRequest, | ||
}, | ||
{ | ||
Description: "should return HTTP 500 when something error with user service", | ||
Setup: func(ctx context.Context, userRepo *mocks.UserRepository, req *http.Request) { | ||
req.Header.Set(identityUUIDHeaderKey, userUUID) | ||
req.Header.Set(identityEmailHeaderKey, userEmail) | ||
|
||
customError := errors.New("some error") | ||
userRepo.EXPECT().GetByUUID(mock.Anything, mock.Anything).Return(user.User{}, customError) | ||
userRepo.EXPECT().UpsertByEmail(mock.Anything, mock.Anything).Return("", customError) | ||
}, | ||
ExpectStatus: http.StatusInternalServerError, | ||
}, | ||
{ | ||
Description: "should return HTTP 200 with propagated user ID when user validation success", | ||
Handler: func(rw http.ResponseWriter, r *http.Request, pathParams map[string]string) { | ||
propagatedUserID := user.FromContext(r.Context()) | ||
_, err := rw.Write([]byte(propagatedUserID)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
rw.WriteHeader(http.StatusOK) | ||
}))); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
req, _ := http.NewRequest("GET", dummyRoute, nil) | ||
req.Header.Set(identityHeaderKey, userEmail) | ||
|
||
rr := httptest.NewRecorder() | ||
|
||
r.ServeHTTP(rr, req) | ||
|
||
assert.Equal(t, userID, rr.Body.String()) | ||
}) | ||
}, | ||
Setup: func(ctx context.Context, userRepo *mocks.UserRepository, req *http.Request) { | ||
req.Header.Set(identityUUIDHeaderKey, userUUID) | ||
req.Header.Set(identityEmailHeaderKey, userEmail) | ||
|
||
userRepo.EXPECT().GetByUUID(mock.Anything, mock.Anything).Return(user.User{ | ||
ID: userID, | ||
UUID: userUUID, | ||
Email: userEmail, | ||
}, nil) | ||
}, | ||
ExpectStatus: http.StatusOK, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.Description, func(t *testing.T) { | ||
ctx := context.Background() | ||
logger := log.NewNoop() | ||
userRepo := new(mocks.UserRepository) | ||
userSvc := user.NewService(logger, userRepo) | ||
|
||
r := runtime.NewServeMux() | ||
err := r.HandlePath(http.MethodGet, dummyRoute, | ||
ValidateUser(identityUUIDHeaderKey, identityEmailHeaderKey, userSvc, tc.Handler)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
req, _ := http.NewRequest("GET", dummyRoute, nil) | ||
rr := httptest.NewRecorder() | ||
|
||
if tc.Setup != nil { | ||
tc.Setup(ctx, userRepo, req) | ||
} | ||
|
||
r.ServeHTTP(rr, req) | ||
|
||
assert.Equal(t, tc.ExpectStatus, rr.Code) | ||
}) | ||
} | ||
} |
Oops, something went wrong.