|
| 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 TestListSubscriptions(t *testing.T) { |
| 29 | + now := time.Now() |
| 30 | + testUser := &auth.User{ |
| 31 | + ID: "test-user", |
| 32 | + GitHubUserID: nil, |
| 33 | + } |
| 34 | + testCases := []struct { |
| 35 | + name string |
| 36 | + cfg *MockListSavedSearchSubscriptionsConfig |
| 37 | + expectedCallCount int |
| 38 | + authMiddlewareOption testServerOption |
| 39 | + request *http.Request |
| 40 | + expectedResponse *http.Response |
| 41 | + }{ |
| 42 | + { |
| 43 | + name: "success", |
| 44 | + cfg: &MockListSavedSearchSubscriptionsConfig{ |
| 45 | + expectedUserID: "test-user", |
| 46 | + expectedPageSize: 100, |
| 47 | + expectedPageToken: nil, |
| 48 | + output: &backend.SubscriptionPage{ |
| 49 | + Data: &[]backend.SubscriptionResponse{ |
| 50 | + { |
| 51 | + Id: "sub-id", |
| 52 | + ChannelId: "channel-id", |
| 53 | + SavedSearchId: "search-id", |
| 54 | + Triggers: []backend.SubscriptionTriggerResponseItem{ |
| 55 | + { |
| 56 | + Value: backendtypes.AttemptToStoreSubscriptionTrigger("trigger"), |
| 57 | + RawValue: nil, |
| 58 | + }, |
| 59 | + }, |
| 60 | + Frequency: "daily", |
| 61 | + CreatedAt: now, |
| 62 | + UpdatedAt: now, |
| 63 | + }, |
| 64 | + }, |
| 65 | + Metadata: &backend.PageMetadata{ |
| 66 | + NextPageToken: nil, |
| 67 | + }, |
| 68 | + }, |
| 69 | + err: nil, |
| 70 | + }, |
| 71 | + expectedCallCount: 1, |
| 72 | + request: httptest.NewRequest( |
| 73 | + http.MethodGet, |
| 74 | + "/v1/users/me/subscriptions", |
| 75 | + nil, |
| 76 | + ), |
| 77 | + expectedResponse: testJSONResponse(http.StatusOK, |
| 78 | + `{ |
| 79 | + "data":[ |
| 80 | + { |
| 81 | + "id":"sub-id", |
| 82 | + "channel_id":"channel-id", |
| 83 | + "saved_search_id":"search-id", |
| 84 | + "triggers":[{"value":"trigger"}], |
| 85 | + "frequency":"daily", |
| 86 | + "created_at":"`+now.Format(time.RFC3339Nano)+`", |
| 87 | + "updated_at":"`+now.Format(time.RFC3339Nano)+`" |
| 88 | + } |
| 89 | + ], |
| 90 | + "metadata":{}}`), |
| 91 | + authMiddlewareOption: withAuthMiddleware(mockAuthMiddleware(testUser)), |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "internal server error", |
| 95 | + cfg: &MockListSavedSearchSubscriptionsConfig{ |
| 96 | + expectedUserID: "test-user", |
| 97 | + expectedPageSize: 100, |
| 98 | + expectedPageToken: nil, |
| 99 | + output: nil, |
| 100 | + err: errTest, |
| 101 | + }, |
| 102 | + expectedCallCount: 1, |
| 103 | + request: httptest.NewRequest( |
| 104 | + http.MethodGet, |
| 105 | + "/v1/users/me/subscriptions", |
| 106 | + nil, |
| 107 | + ), |
| 108 | + expectedResponse: testJSONResponse(http.StatusInternalServerError, |
| 109 | + `{ |
| 110 | + "code":500, |
| 111 | + "message":"could not list subscriptions" |
| 112 | + }`), |
| 113 | + authMiddlewareOption: withAuthMiddleware(mockAuthMiddleware(testUser)), |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "success with page token and size", |
| 117 | + cfg: &MockListSavedSearchSubscriptionsConfig{ |
| 118 | + expectedUserID: "test-user", |
| 119 | + expectedPageSize: 50, |
| 120 | + expectedPageToken: valuePtr("page-token"), |
| 121 | + err: nil, |
| 122 | + output: &backend.SubscriptionPage{ |
| 123 | + Data: &[]backend.SubscriptionResponse{ |
| 124 | + { |
| 125 | + Id: "sub-id-2", |
| 126 | + ChannelId: "channel-id-2", |
| 127 | + SavedSearchId: "search-id-2", |
| 128 | + Triggers: []backend.SubscriptionTriggerResponseItem{ |
| 129 | + { |
| 130 | + Value: backendtypes.AttemptToStoreSubscriptionTrigger("trigger-2"), |
| 131 | + RawValue: nil, |
| 132 | + }, |
| 133 | + }, |
| 134 | + Frequency: "weekly", |
| 135 | + CreatedAt: now, |
| 136 | + UpdatedAt: now, |
| 137 | + }, |
| 138 | + }, |
| 139 | + Metadata: &backend.PageMetadata{ |
| 140 | + NextPageToken: valuePtr("next-page-token"), |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + expectedCallCount: 1, |
| 145 | + request: httptest.NewRequest( |
| 146 | + http.MethodGet, |
| 147 | + "/v1/users/me/subscriptions?page_size=50&page_token=page-token", |
| 148 | + nil, |
| 149 | + ), |
| 150 | + expectedResponse: testJSONResponse(http.StatusOK, |
| 151 | + `{ |
| 152 | + "data":[ |
| 153 | + { |
| 154 | + "id":"sub-id-2", |
| 155 | + "channel_id":"channel-id-2", |
| 156 | + "saved_search_id":"search-id-2", |
| 157 | + "triggers":[{"value":"trigger-2"}], |
| 158 | + "frequency":"weekly", |
| 159 | + "created_at":"`+now.Format(time.RFC3339Nano)+`", |
| 160 | + "updated_at":"`+now.Format(time.RFC3339Nano)+`" |
| 161 | + } |
| 162 | + ], |
| 163 | + "metadata":{ |
| 164 | + "next_page_token":"next-page-token" |
| 165 | + } |
| 166 | + }`), |
| 167 | + authMiddlewareOption: withAuthMiddleware(mockAuthMiddleware(testUser)), |
| 168 | + }, |
| 169 | + } |
| 170 | + |
| 171 | + for _, tc := range testCases { |
| 172 | + t.Run(tc.name, func(t *testing.T) { |
| 173 | + //nolint:exhaustruct |
| 174 | + mockStorer := &MockWPTMetricsStorer{ |
| 175 | + listSavedSearchSubscriptionsCfg: tc.cfg, |
| 176 | + t: t, |
| 177 | + } |
| 178 | + myServer := Server{ |
| 179 | + wptMetricsStorer: mockStorer, |
| 180 | + metadataStorer: nil, |
| 181 | + userGitHubClientFactory: nil, |
| 182 | + operationResponseCaches: nil, |
| 183 | + baseURL: getTestBaseURL(t), |
| 184 | + } |
| 185 | + assertTestServerRequest(t, &myServer, tc.request, tc.expectedResponse, tc.authMiddlewareOption) |
| 186 | + assertMocksExpectations(t, |
| 187 | + tc.expectedCallCount, |
| 188 | + mockStorer.callCountListSavedSearchSubscriptions, |
| 189 | + "ListSavedSearchSubscriptions", |
| 190 | + nil) |
| 191 | + }) |
| 192 | + } |
| 193 | + |
| 194 | +} |
0 commit comments