|
1 | 1 | package middleware |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/json" |
| 4 | + "context" |
5 | 5 | "errors" |
6 | 6 | "net/http" |
7 | 7 | "net/http/httptest" |
8 | 8 | "testing" |
9 | 9 |
|
10 | 10 | "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" |
11 | | - "github.com/odpf/columbus/api/httpapi/handlers" |
12 | 11 | "github.com/odpf/columbus/lib/mocks" |
13 | 12 | "github.com/odpf/columbus/user" |
| 13 | + "github.com/odpf/salt/log" |
14 | 14 | "github.com/stretchr/testify/assert" |
15 | 15 | "github.com/stretchr/testify/mock" |
16 | 16 | ) |
17 | 17 |
|
18 | 18 | const ( |
19 | | - dummyRoute = "/v1beta1/dummy" |
20 | | - identityHeaderKey = "Columbus-User-ID" |
| 19 | + dummyRoute = "/v1beta1/dummy" |
| 20 | + identityUUIDHeaderKey = "Columbus-User-ID" |
| 21 | + identityEmailHeaderKey = "Columbus-User-Email" |
| 22 | + userUUID = "user-uuid" |
| 23 | + userID = "user-id" |
| 24 | + userEmail = "some-email" |
21 | 25 | ) |
22 | 26 |
|
23 | | -var userCfg = user.Config{IdentityProviderDefaultName: "shield"} |
24 | | - |
25 | 27 | func TestValidateUser(t *testing.T) { |
26 | 28 |
|
27 | | - t.Run("should return HTTP 400 when identity header not present", func(t *testing.T) { |
28 | | - userSvc := user.NewService(nil, userCfg) |
29 | | - |
30 | | - r := runtime.NewServeMux() |
31 | | - err := r.HandlePath(http.MethodGet, dummyRoute, |
32 | | - ValidateUser(identityHeaderKey, userSvc, nil)) |
33 | | - if err != nil { |
34 | | - t.Fatal(err) |
35 | | - } |
36 | | - |
37 | | - req, _ := http.NewRequest("GET", dummyRoute, nil) |
38 | | - |
39 | | - rr := httptest.NewRecorder() |
40 | | - |
41 | | - r.ServeHTTP(rr, req) |
42 | | - |
43 | | - assert.Equal(t, http.StatusBadRequest, rr.Code) |
44 | | - response := &handlers.ErrorResponse{} |
45 | | - err = json.Unmarshal(rr.Body.Bytes(), &response) |
46 | | - if err != nil { |
47 | | - t.Fatal(err) |
48 | | - } |
49 | | - |
50 | | - assert.Equal(t, "identity header is empty", response.Reason) |
51 | | - }) |
52 | | - |
53 | | - t.Run("should return HTTP 500 when something error with user service", func(t *testing.T) { |
54 | | - customError := errors.New("some error") |
55 | | - mockUserRepository := &mocks.UserRepository{} |
56 | | - mockUserRepository.On("GetID", mock.Anything, mock.Anything).Return("", customError) |
57 | | - mockUserRepository.On("Create", mock.Anything, mock.Anything).Return("", customError) |
58 | | - |
59 | | - userSvc := user.NewService(mockUserRepository, userCfg) |
60 | | - |
61 | | - r := runtime.NewServeMux() |
62 | | - err := r.HandlePath(http.MethodGet, dummyRoute, |
63 | | - ValidateUser(identityHeaderKey, userSvc, nil)) |
64 | | - if err != nil { |
65 | | - t.Fatal(err) |
66 | | - } |
67 | | - |
68 | | - req, _ := http.NewRequest("GET", dummyRoute, nil) |
69 | | - req.Header.Set(identityHeaderKey, "some-email") |
70 | | - rr := httptest.NewRecorder() |
71 | | - |
72 | | - r.ServeHTTP(rr, req) |
73 | | - |
74 | | - assert.Equal(t, http.StatusInternalServerError, rr.Code) |
75 | | - response := &handlers.ErrorResponse{} |
76 | | - err = json.Unmarshal(rr.Body.Bytes(), &response) |
77 | | - if err != nil { |
78 | | - t.Fatal(err) |
79 | | - } |
80 | | - |
81 | | - assert.Equal(t, customError.Error(), response.Reason) |
82 | | - }) |
83 | | - |
84 | | - t.Run("should return HTTP 200 with propagated user ID when user validation success", func(t *testing.T) { |
85 | | - userID := "user-id" |
86 | | - userEmail := "some-email" |
87 | | - mockUserRepository := &mocks.UserRepository{} |
88 | | - mockUserRepository.On("GetID", mock.Anything, mock.Anything).Return(userID, nil) |
89 | | - mockUserRepository.On("Create", mock.Anything, mock.Anything).Return(userID, nil) |
90 | | - |
91 | | - userSvc := user.NewService(mockUserRepository, userCfg) |
92 | | - |
93 | | - r := runtime.NewServeMux() |
94 | | - if err := r.HandlePath(http.MethodGet, dummyRoute, |
95 | | - ValidateUser(identityHeaderKey, userSvc, runtime.HandlerFunc(func(rw http.ResponseWriter, r *http.Request, pathParams map[string]string) { |
| 29 | + type testCase struct { |
| 30 | + Description string |
| 31 | + Setup func(ctx context.Context, userRepo *mocks.UserRepository, req *http.Request) |
| 32 | + Handler runtime.HandlerFunc |
| 33 | + ExpectStatus int |
| 34 | + } |
| 35 | + |
| 36 | + var testCases = []testCase{ |
| 37 | + { |
| 38 | + Description: "should return HTTP 400 when identity header not present", |
| 39 | + ExpectStatus: http.StatusBadRequest, |
| 40 | + }, |
| 41 | + { |
| 42 | + Description: "should return HTTP 500 when something error with user service", |
| 43 | + Setup: func(ctx context.Context, userRepo *mocks.UserRepository, req *http.Request) { |
| 44 | + req.Header.Set(identityUUIDHeaderKey, userUUID) |
| 45 | + req.Header.Set(identityEmailHeaderKey, userEmail) |
| 46 | + |
| 47 | + customError := errors.New("some error") |
| 48 | + userRepo.EXPECT().GetByUUID(mock.Anything, mock.Anything).Return(user.User{}, customError) |
| 49 | + userRepo.EXPECT().UpsertByEmail(mock.Anything, mock.Anything).Return("", customError) |
| 50 | + }, |
| 51 | + ExpectStatus: http.StatusInternalServerError, |
| 52 | + }, |
| 53 | + { |
| 54 | + Description: "should return HTTP 200 with propagated user ID when user validation success", |
| 55 | + Handler: func(rw http.ResponseWriter, r *http.Request, pathParams map[string]string) { |
96 | 56 | propagatedUserID := user.FromContext(r.Context()) |
97 | 57 | _, err := rw.Write([]byte(propagatedUserID)) |
98 | 58 | if err != nil { |
99 | 59 | t.Fatal(err) |
100 | 60 | } |
101 | 61 | rw.WriteHeader(http.StatusOK) |
102 | | - }))); err != nil { |
103 | | - t.Fatal(err) |
104 | | - } |
105 | | - |
106 | | - req, _ := http.NewRequest("GET", dummyRoute, nil) |
107 | | - req.Header.Set(identityHeaderKey, userEmail) |
108 | | - |
109 | | - rr := httptest.NewRecorder() |
110 | | - |
111 | | - r.ServeHTTP(rr, req) |
112 | | - |
113 | | - assert.Equal(t, userID, rr.Body.String()) |
114 | | - }) |
| 62 | + }, |
| 63 | + Setup: func(ctx context.Context, userRepo *mocks.UserRepository, req *http.Request) { |
| 64 | + req.Header.Set(identityUUIDHeaderKey, userUUID) |
| 65 | + req.Header.Set(identityEmailHeaderKey, userEmail) |
| 66 | + |
| 67 | + userRepo.EXPECT().GetByUUID(mock.Anything, mock.Anything).Return(user.User{ |
| 68 | + ID: userID, |
| 69 | + UUID: userUUID, |
| 70 | + Email: userEmail, |
| 71 | + }, nil) |
| 72 | + }, |
| 73 | + ExpectStatus: http.StatusOK, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + for _, tc := range testCases { |
| 78 | + t.Run(tc.Description, func(t *testing.T) { |
| 79 | + ctx := context.Background() |
| 80 | + logger := log.NewNoop() |
| 81 | + userRepo := new(mocks.UserRepository) |
| 82 | + userSvc := user.NewService(logger, userRepo) |
| 83 | + |
| 84 | + r := runtime.NewServeMux() |
| 85 | + err := r.HandlePath(http.MethodGet, dummyRoute, |
| 86 | + ValidateUser(identityUUIDHeaderKey, identityEmailHeaderKey, userSvc, tc.Handler)) |
| 87 | + if err != nil { |
| 88 | + t.Fatal(err) |
| 89 | + } |
| 90 | + |
| 91 | + req, _ := http.NewRequest("GET", dummyRoute, nil) |
| 92 | + rr := httptest.NewRecorder() |
| 93 | + |
| 94 | + if tc.Setup != nil { |
| 95 | + tc.Setup(ctx, userRepo, req) |
| 96 | + } |
| 97 | + |
| 98 | + r.ServeHTTP(rr, req) |
| 99 | + |
| 100 | + assert.Equal(t, tc.ExpectStatus, rr.Code) |
| 101 | + }) |
| 102 | + } |
115 | 103 | } |
0 commit comments