|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/LeanerCloud/CUDly/pkg/common" |
| 9 | + "github.com/aws/aws-lambda-go/events" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/mock" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// --------------------------------------------------------------------------- |
| 16 | +// GET /api/notifications/unsubscribe |
| 17 | +// --------------------------------------------------------------------------- |
| 18 | + |
| 19 | +func validUnsubToken(email, scope string) string { |
| 20 | + return common.DeriveMuteToken(nil, email, scope) |
| 21 | +} |
| 22 | + |
| 23 | +func TestUnsubscribeHandler_Success(t *testing.T) { |
| 24 | + ctx := context.Background() |
| 25 | + email := "user@example.com" |
| 26 | + scope := string(common.ScopePurchaseApprovals) |
| 27 | + token := validUnsubToken(email, scope) |
| 28 | + |
| 29 | + mockStore := new(MockConfigStore) |
| 30 | + mockStore.On("UpsertNotificationMute", ctx, email, scope, token).Return(nil) |
| 31 | + t.Cleanup(func() { mockStore.AssertExpectations(t) }) |
| 32 | + |
| 33 | + h := &Handler{config: mockStore} |
| 34 | + r := newTestRouter(h) |
| 35 | + |
| 36 | + req := &events.LambdaFunctionURLRequest{ |
| 37 | + QueryStringParameters: map[string]string{ |
| 38 | + "token": token, |
| 39 | + "email": email, |
| 40 | + "scope": scope, |
| 41 | + }, |
| 42 | + } |
| 43 | + result, err := r.unsubscribeHandler(ctx, req, nil) |
| 44 | + require.NoError(t, err) |
| 45 | + raw, ok := result.(*rawResponse) |
| 46 | + require.True(t, ok, "expected *rawResponse") |
| 47 | + assert.Equal(t, "text/html; charset=utf-8", raw.contentType) |
| 48 | + assert.Contains(t, raw.body, "Unsubscribed") |
| 49 | + assert.Contains(t, raw.body, "purchase approval request") |
| 50 | +} |
| 51 | + |
| 52 | +func TestUnsubscribeHandler_ForgedToken_Returns401(t *testing.T) { |
| 53 | + ctx := context.Background() |
| 54 | + req := &events.LambdaFunctionURLRequest{ |
| 55 | + QueryStringParameters: map[string]string{ |
| 56 | + "token": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 57 | + "email": "attacker@example.com", |
| 58 | + "scope": string(common.ScopePurchaseApprovals), |
| 59 | + }, |
| 60 | + } |
| 61 | + h := &Handler{config: new(MockConfigStore)} |
| 62 | + r := newTestRouter(h) |
| 63 | + |
| 64 | + _, err := r.unsubscribeHandler(ctx, req, nil) |
| 65 | + require.Error(t, err) |
| 66 | + ce, ok := IsClientError(err) |
| 67 | + require.True(t, ok) |
| 68 | + assert.Equal(t, 401, ce.code) |
| 69 | +} |
| 70 | + |
| 71 | +func TestUnsubscribeHandler_MissingParams_Returns400(t *testing.T) { |
| 72 | + ctx := context.Background() |
| 73 | + h := &Handler{config: new(MockConfigStore)} |
| 74 | + r := newTestRouter(h) |
| 75 | + |
| 76 | + req := &events.LambdaFunctionURLRequest{ |
| 77 | + QueryStringParameters: map[string]string{ |
| 78 | + "token": "something", |
| 79 | + // email and scope missing |
| 80 | + }, |
| 81 | + } |
| 82 | + _, err := r.unsubscribeHandler(ctx, req, nil) |
| 83 | + require.Error(t, err) |
| 84 | + ce, ok := IsClientError(err) |
| 85 | + require.True(t, ok) |
| 86 | + assert.Equal(t, 400, ce.code) |
| 87 | +} |
| 88 | + |
| 89 | +func TestUnsubscribeHandler_UnknownScope_Returns400(t *testing.T) { |
| 90 | + ctx := context.Background() |
| 91 | + email := "user@example.com" |
| 92 | + scope := "unknown_scope" |
| 93 | + // Use the dev-key token so HMAC passes but scope guard fires first. |
| 94 | + token := common.DeriveMuteToken(nil, email, scope) |
| 95 | + |
| 96 | + req := &events.LambdaFunctionURLRequest{ |
| 97 | + QueryStringParameters: map[string]string{ |
| 98 | + "token": token, |
| 99 | + "email": email, |
| 100 | + "scope": scope, |
| 101 | + }, |
| 102 | + } |
| 103 | + h := &Handler{config: new(MockConfigStore)} |
| 104 | + r := newTestRouter(h) |
| 105 | + |
| 106 | + _, err := r.unsubscribeHandler(ctx, req, nil) |
| 107 | + require.Error(t, err) |
| 108 | + ce, ok := IsClientError(err) |
| 109 | + require.True(t, ok) |
| 110 | + assert.Equal(t, 400, ce.code) |
| 111 | +} |
| 112 | + |
| 113 | +func TestUnsubscribeHandler_StoreError_Returns500(t *testing.T) { |
| 114 | + ctx := context.Background() |
| 115 | + email := "user@example.com" |
| 116 | + scope := string(common.ScopePurchaseApprovals) |
| 117 | + token := validUnsubToken(email, scope) |
| 118 | + |
| 119 | + mockStore := new(MockConfigStore) |
| 120 | + mockStore.On("UpsertNotificationMute", mock.Anything, email, scope, token). |
| 121 | + Return(assert.AnError) |
| 122 | + t.Cleanup(func() { mockStore.AssertExpectations(t) }) |
| 123 | + |
| 124 | + h := &Handler{config: mockStore} |
| 125 | + r := newTestRouter(h) |
| 126 | + |
| 127 | + req := &events.LambdaFunctionURLRequest{ |
| 128 | + QueryStringParameters: map[string]string{ |
| 129 | + "token": token, |
| 130 | + "email": email, |
| 131 | + "scope": scope, |
| 132 | + }, |
| 133 | + } |
| 134 | + _, err := r.unsubscribeHandler(ctx, req, nil) |
| 135 | + require.Error(t, err) |
| 136 | + _, isClient := IsClientError(err) |
| 137 | + assert.False(t, isClient, "store error should be a 500, not a client error") |
| 138 | +} |
| 139 | + |
| 140 | +// --------------------------------------------------------------------------- |
| 141 | +// scopeLabel helper |
| 142 | +// --------------------------------------------------------------------------- |
| 143 | + |
| 144 | +func TestScopeLabel_KnownScopes(t *testing.T) { |
| 145 | + assert.Equal(t, "purchase approval request", scopeLabel(string(common.ScopePurchaseApprovals))) |
| 146 | + assert.Equal(t, "RI exchange approval request", scopeLabel(string(common.ScopeRIExchangeApprovals))) |
| 147 | + assert.Equal(t, "notification", scopeLabel("bogus")) |
| 148 | +} |
| 149 | + |
| 150 | +// --------------------------------------------------------------------------- |
| 151 | +// redactEmailLocal |
| 152 | +// --------------------------------------------------------------------------- |
| 153 | + |
| 154 | +func TestRedactEmailLocal(t *testing.T) { |
| 155 | + cases := []struct { |
| 156 | + in, want string |
| 157 | + }{ |
| 158 | + {"user@example.com", "us***@example.com"}, |
| 159 | + {"ab@x.com", "***@x.com"}, |
| 160 | + {"a@x.com", "***@x.com"}, |
| 161 | + {"noemail", "***"}, |
| 162 | + } |
| 163 | + for _, c := range cases { |
| 164 | + assert.Equal(t, c.want, redactEmailLocal(c.in), "input: %s", c.in) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +// --------------------------------------------------------------------------- |
| 169 | +// isPublicEndpoint includes /api/notifications/unsubscribe |
| 170 | +// --------------------------------------------------------------------------- |
| 171 | + |
| 172 | +func TestIsPublicEndpoint_UnsubscribePath(t *testing.T) { |
| 173 | + h := &Handler{} |
| 174 | + assert.True(t, h.isPublicEndpoint("/api/notifications/unsubscribe")) |
| 175 | + assert.True(t, h.isPublicEndpoint("/api/notifications/unsubscribe?token=x&email=y&scope=z")) |
| 176 | +} |
| 177 | + |
| 178 | +// --------------------------------------------------------------------------- |
| 179 | +// Route is registered (router smoke test) |
| 180 | +// --------------------------------------------------------------------------- |
| 181 | + |
| 182 | +func TestRouter_UnsubscribeRoute_Registered(t *testing.T) { |
| 183 | + // Verify the route is wired: an unsigned token returns 401, which can only |
| 184 | + // happen if the router dispatched to the correct handler. |
| 185 | + ctx := context.Background() |
| 186 | + h := &Handler{config: new(MockConfigStore)} |
| 187 | + r := NewRouter(h) |
| 188 | + |
| 189 | + req := &events.LambdaFunctionURLRequest{ |
| 190 | + RequestContext: events.LambdaFunctionURLRequestContext{ |
| 191 | + HTTP: events.LambdaFunctionURLRequestContextHTTPDescription{ |
| 192 | + Method: "GET", |
| 193 | + Path: "/api/notifications/unsubscribe", |
| 194 | + }, |
| 195 | + }, |
| 196 | + QueryStringParameters: map[string]string{ |
| 197 | + "token": strings.Repeat("a", 64), |
| 198 | + "email": "user@example.com", |
| 199 | + "scope": string(common.ScopePurchaseApprovals), |
| 200 | + }, |
| 201 | + } |
| 202 | + _, err := r.Route(ctx, "GET", "/api/notifications/unsubscribe", req) |
| 203 | + require.Error(t, err) |
| 204 | + ce, ok := IsClientError(err) |
| 205 | + require.True(t, ok) |
| 206 | + assert.Equal(t, 401, ce.code) |
| 207 | +} |
0 commit comments