|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package httpserver |
| 16 | + |
| 17 | +import ( |
| 18 | + "net/http" |
| 19 | + "net/http/httptest" |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/GoogleChrome/webstatus.dev/lib/auth" |
| 24 | + "github.com/GoogleChrome/webstatus.dev/lib/backendtypes" |
| 25 | + "github.com/GoogleChrome/webstatus.dev/lib/gen/openapi/backend" |
| 26 | +) |
| 27 | + |
| 28 | +func TestGetSubscription(t *testing.T) { |
| 29 | + now := time.Now() |
| 30 | + testUser := &auth.User{ |
| 31 | + ID: "test-user", |
| 32 | + GitHubUserID: nil, |
| 33 | + } |
| 34 | + |
| 35 | + testCases := []struct { |
| 36 | + name string |
| 37 | + cfg *MockGetSavedSearchSubscriptionConfig |
| 38 | + expectedCallCount int |
| 39 | + authMiddlewareOption testServerOption |
| 40 | + request *http.Request |
| 41 | + expectedResponse *http.Response |
| 42 | + }{ |
| 43 | + { |
| 44 | + name: "success", |
| 45 | + cfg: &MockGetSavedSearchSubscriptionConfig{ |
| 46 | + expectedUserID: "test-user", |
| 47 | + expectedSubscriptionID: "sub-id", |
| 48 | + output: &backend.SubscriptionResponse{ |
| 49 | + Id: "sub-id", |
| 50 | + ChannelId: "channel-id", |
| 51 | + SavedSearchId: "search-id", |
| 52 | + Triggers: []backend.SubscriptionTriggerResponseItem{ |
| 53 | + { |
| 54 | + Value: backendtypes.AttemptToStoreSubscriptionTrigger("trigger"), |
| 55 | + RawValue: nil, |
| 56 | + }, |
| 57 | + }, |
| 58 | + Frequency: "daily", |
| 59 | + CreatedAt: now, |
| 60 | + UpdatedAt: now, |
| 61 | + }, |
| 62 | + err: nil, |
| 63 | + }, |
| 64 | + expectedCallCount: 1, |
| 65 | + authMiddlewareOption: withAuthMiddleware(mockAuthMiddleware(testUser)), |
| 66 | + request: httptest.NewRequest( |
| 67 | + http.MethodGet, |
| 68 | + "/v1/users/me/subscriptions/sub-id", |
| 69 | + nil, |
| 70 | + ), |
| 71 | + expectedResponse: testJSONResponse(http.StatusOK, |
| 72 | + `{ |
| 73 | + "id":"sub-id","channel_id":"channel-id", |
| 74 | + "saved_search_id":"search-id", |
| 75 | + "triggers":[{"value":"trigger"}], |
| 76 | + "frequency":"daily", |
| 77 | + "created_at":"`+now.Format(time.RFC3339Nano)+`", |
| 78 | + "updated_at":"`+now.Format(time.RFC3339Nano)+`"}`), |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "not found", |
| 82 | + cfg: &MockGetSavedSearchSubscriptionConfig{ |
| 83 | + expectedUserID: "test-user", |
| 84 | + expectedSubscriptionID: "sub-id", |
| 85 | + output: nil, |
| 86 | + err: backendtypes.ErrEntityDoesNotExist, |
| 87 | + }, |
| 88 | + expectedCallCount: 1, |
| 89 | + authMiddlewareOption: withAuthMiddleware(mockAuthMiddleware(testUser)), |
| 90 | + request: httptest.NewRequest( |
| 91 | + http.MethodGet, |
| 92 | + "/v1/users/me/subscriptions/sub-id", |
| 93 | + nil, |
| 94 | + ), |
| 95 | + expectedResponse: testJSONResponse(http.StatusNotFound, `{"code":404,"message":"subscription not found"}`), |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "forbidden - user cannot access subscription", |
| 99 | + cfg: &MockGetSavedSearchSubscriptionConfig{ |
| 100 | + expectedUserID: "test-user", |
| 101 | + expectedSubscriptionID: "sub-id", |
| 102 | + output: nil, |
| 103 | + err: backendtypes.ErrUserNotAuthorizedForAction, |
| 104 | + }, |
| 105 | + expectedCallCount: 1, |
| 106 | + authMiddlewareOption: withAuthMiddleware(mockAuthMiddleware(testUser)), |
| 107 | + request: httptest.NewRequest( |
| 108 | + http.MethodGet, |
| 109 | + "/v1/users/me/subscriptions/sub-id", |
| 110 | + nil, |
| 111 | + ), |
| 112 | + expectedResponse: testJSONResponse(http.StatusForbidden, |
| 113 | + `{"code":403,"message":"user not authorized to access this subscription"}`), |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "internal server error", |
| 117 | + cfg: &MockGetSavedSearchSubscriptionConfig{ |
| 118 | + expectedUserID: "test-user", |
| 119 | + expectedSubscriptionID: "sub-id", |
| 120 | + output: nil, |
| 121 | + err: errTest, |
| 122 | + }, |
| 123 | + expectedCallCount: 1, |
| 124 | + authMiddlewareOption: withAuthMiddleware(mockAuthMiddleware(testUser)), |
| 125 | + request: httptest.NewRequest( |
| 126 | + http.MethodGet, |
| 127 | + "/v1/users/me/subscriptions/sub-id", |
| 128 | + nil, |
| 129 | + ), |
| 130 | + expectedResponse: testJSONResponse(http.StatusInternalServerError, |
| 131 | + `{"code":500,"message":"could not get subscription"}`), |
| 132 | + }, |
| 133 | + } |
| 134 | + |
| 135 | + for _, tc := range testCases { |
| 136 | + t.Run(tc.name, func(t *testing.T) { |
| 137 | + //nolint:exhaustruct |
| 138 | + mockStorer := &MockWPTMetricsStorer{ |
| 139 | + getSavedSearchSubscriptionCfg: tc.cfg, |
| 140 | + t: t, |
| 141 | + } |
| 142 | + myServer := Server{ |
| 143 | + wptMetricsStorer: mockStorer, |
| 144 | + metadataStorer: nil, |
| 145 | + userGitHubClientFactory: nil, |
| 146 | + operationResponseCaches: nil, |
| 147 | + baseURL: getTestBaseURL(t), |
| 148 | + } |
| 149 | + assertTestServerRequest(t, &myServer, tc.request, tc.expectedResponse, tc.authMiddlewareOption) |
| 150 | + assertMocksExpectations(t, |
| 151 | + tc.expectedCallCount, |
| 152 | + mockStorer.callCountGetSavedSearchSubscription, |
| 153 | + "GetSavedSearchSubscription", |
| 154 | + nil) |
| 155 | + }) |
| 156 | + } |
| 157 | +} |
0 commit comments