|
| 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 | +// nolint: dupl // WONTFIX |
| 16 | +package httpserver |
| 17 | + |
| 18 | +import ( |
| 19 | + "net/http" |
| 20 | + "net/http/httptest" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/GoogleChrome/webstatus.dev/lib/gcpspanner/spanneradapters/backendtypes" |
| 24 | +) |
| 25 | + |
| 26 | +func TestPutUserSavedSearchBookmark(t *testing.T) { |
| 27 | + authMiddlewareOption := withAuthMiddleware(mockAuthMiddleware(createTestID1User())) |
| 28 | + testCases := []basicHTTPTestCase[MockPutUserSavedSearchBookmarkConfig]{ |
| 29 | + { |
| 30 | + name: "success", |
| 31 | + cfg: &MockPutUserSavedSearchBookmarkConfig{ |
| 32 | + expectedSavedSearchID: "saved-search-id", |
| 33 | + expectedUserID: "testID1", |
| 34 | + err: nil, |
| 35 | + }, |
| 36 | + request: httptest.NewRequest( |
| 37 | + http.MethodPut, |
| 38 | + "/v1/users/me/saved-searches/saved-search-id/bookmark_status", |
| 39 | + nil, |
| 40 | + ), |
| 41 | + expectedResponse: createEmptyBodyResponse(http.StatusOK), |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "general error", |
| 45 | + cfg: &MockPutUserSavedSearchBookmarkConfig{ |
| 46 | + expectedSavedSearchID: "saved-search-id", |
| 47 | + expectedUserID: "testID1", |
| 48 | + err: errTest, |
| 49 | + }, |
| 50 | + request: httptest.NewRequest( |
| 51 | + http.MethodPut, |
| 52 | + "/v1/users/me/saved-searches/saved-search-id/bookmark_status", |
| 53 | + nil, |
| 54 | + ), |
| 55 | + expectedResponse: testJSONResponse(500, |
| 56 | + `{ |
| 57 | + "code":500, |
| 58 | + "message":"unable to add bookmark" |
| 59 | + }`, |
| 60 | + ), |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "not found", |
| 64 | + cfg: &MockPutUserSavedSearchBookmarkConfig{ |
| 65 | + expectedSavedSearchID: "saved-search-id", |
| 66 | + expectedUserID: "testID1", |
| 67 | + err: backendtypes.ErrEntityDoesNotExist, |
| 68 | + }, |
| 69 | + request: httptest.NewRequest( |
| 70 | + http.MethodPut, |
| 71 | + "/v1/users/me/saved-searches/saved-search-id/bookmark_status", |
| 72 | + nil, |
| 73 | + ), |
| 74 | + expectedResponse: testJSONResponse(404, |
| 75 | + `{ |
| 76 | + "code":404, |
| 77 | + "message":"saved search to bookmark not found" |
| 78 | + }`, |
| 79 | + ), |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "too many bookmarks", |
| 83 | + cfg: &MockPutUserSavedSearchBookmarkConfig{ |
| 84 | + expectedSavedSearchID: "saved-search-id", |
| 85 | + expectedUserID: "testID1", |
| 86 | + err: backendtypes.ErrUserMaxBookmarks, |
| 87 | + }, |
| 88 | + request: httptest.NewRequest( |
| 89 | + http.MethodPut, |
| 90 | + "/v1/users/me/saved-searches/saved-search-id/bookmark_status", |
| 91 | + nil, |
| 92 | + ), |
| 93 | + expectedResponse: testJSONResponse(403, |
| 94 | + `{ |
| 95 | + "code":403, |
| 96 | + "message":"user has reached the maximum number of allowed bookmarks" |
| 97 | + }`, |
| 98 | + ), |
| 99 | + }, |
| 100 | + } |
| 101 | + for _, tc := range testCases { |
| 102 | + t.Run(tc.name, func(t *testing.T) { |
| 103 | + //nolint:exhaustruct // WONTFIX |
| 104 | + mockStorer := &MockWPTMetricsStorer{ |
| 105 | + putUserSavedSearchBookmarkCfg: tc.cfg, |
| 106 | + t: t, |
| 107 | + } |
| 108 | + myServer := Server{wptMetricsStorer: mockStorer, metadataStorer: nil, |
| 109 | + operationResponseCaches: nil} |
| 110 | + assertTestServerRequest(t, &myServer, tc.request, tc.expectedResponse, |
| 111 | + []testServerOption{authMiddlewareOption}...) |
| 112 | + assertMocksExpectations(t, 1, mockStorer.callCountPutUserSavedSearchBookmark, |
| 113 | + "PutUserSavedSearchBookmark", nil) |
| 114 | + }) |
| 115 | + } |
| 116 | +} |
0 commit comments