-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
admin: unsubscribing users from mailing list
- Loading branch information
1 parent
3f7ccaf
commit 225f7ae
Showing
6 changed files
with
204 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,3 +65,88 @@ func TestGetMailingListSubscribersUnauthorized(t *testing.T) { | |
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusUnauthorized) | ||
} | ||
} | ||
|
||
func TestUnsubscribeMailingListSuccess(t *testing.T) { | ||
s := setup() | ||
defer tests.Teardown() | ||
|
||
// Add a test subscriber first | ||
_, err := s.DB.Exec(` | ||
INSERT INTO mailing_list (email, welcome_email_sent, subscribed) | ||
VALUES ($1, $2, $3) | ||
`, "[email protected]", true, true) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
token, _ := tests.GenerateTestJWT(1) // Admin user | ||
body := `{"email": "[email protected]"}` | ||
req, err := http.NewRequest("POST", "/api/mailing-list/unsubscribe", tests.StringToReader(body)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
req.Header.Set("Authorization", "Bearer "+token) | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
rr := httptest.NewRecorder() | ||
handler := http.HandlerFunc(s.JwtMiddleware(s.UnsubscribeMailingListRoute)) | ||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusOK { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) | ||
} | ||
|
||
// Verify the subscriber was actually unsubscribed | ||
var subscribed bool | ||
err = s.DB.QueryRow("SELECT subscribed FROM mailing_list WHERE email = $1", "[email protected]").Scan(&subscribed) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if subscribed { | ||
t.Error("subscriber was not unsubscribed") | ||
} | ||
} | ||
|
||
func TestUnsubscribeMailingListUnauthorized(t *testing.T) { | ||
s := setup() | ||
defer tests.Teardown() | ||
|
||
token, _ := tests.GenerateTestJWT(2) // Non-admin user | ||
body := `{"email": "[email protected]"}` | ||
req, err := http.NewRequest("POST", "/api/mailing-list/unsubscribe", tests.StringToReader(body)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
req.Header.Set("Authorization", "Bearer "+token) | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
rr := httptest.NewRecorder() | ||
handler := http.HandlerFunc(s.JwtMiddleware(s.UnsubscribeMailingListRoute)) | ||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusUnauthorized { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusUnauthorized) | ||
} | ||
} | ||
|
||
func TestUnsubscribeMailingListInvalidEmail(t *testing.T) { | ||
s := setup() | ||
defer tests.Teardown() | ||
|
||
token, _ := tests.GenerateTestJWT(1) // Admin user | ||
body := `{"email": "[email protected]"}` | ||
req, err := http.NewRequest("POST", "/api/mailing-list/unsubscribe", tests.StringToReader(body)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
req.Header.Set("Authorization", "Bearer "+token) | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
rr := httptest.NewRecorder() | ||
handler := http.HandlerFunc(s.JwtMiddleware(s.UnsubscribeMailingListRoute)) | ||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusInternalServerError { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusInternalServerError) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters