From 9bb0c4455a315ca82980619af1ced69262444cf8 Mon Sep 17 00:00:00 2001 From: Justin Farrell Date: Mon, 26 Aug 2024 10:40:40 -0400 Subject: [PATCH] Added an additional test for checking if providing no password when password is set to "required" will make the request fail --- internal/game/create_game.go | 3 ++- internal/game/create_game_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/internal/game/create_game.go b/internal/game/create_game.go index 04157e9..188f49f 100644 --- a/internal/game/create_game.go +++ b/internal/game/create_game.go @@ -21,6 +21,7 @@ type CreateGameArgs struct { } const ERROR_PASSWORD_TOO_SHORT = "password must be longer than 6 characters" +const ERROR_PASSWORD_REQUIRED_BUT_NO_PASSWORD = "password is required when password_protected is true" // CreateGame handles the creation of a new game. // @@ -55,7 +56,7 @@ func CreateGame(w http.ResponseWriter, r *http.Request) error { if game.PasswordProtected && game.Password == "" { w.WriteHeader(http.StatusBadRequest) - return errors.New("password is required when password_protected is true") + return errors.New(ERROR_PASSWORD_REQUIRED_BUT_NO_PASSWORD) } if !game.PasswordProtected && len(game.Password) > 0 { diff --git a/internal/game/create_game_test.go b/internal/game/create_game_test.go index 7c0a4ba..c2b8f9c 100644 --- a/internal/game/create_game_test.go +++ b/internal/game/create_game_test.go @@ -32,3 +32,27 @@ func TestCreateGame_PasswordTooShort(t *testing.T) { t.Errorf("CreateGame() error = %v, wantErr %v", err, expectedError) } } + +func TestCreateGame_PasswordRequiredWhenPasswordProtectedIsTrue(t *testing.T) { + // Create a test request with password protected set to true but no password + body := CreateGameArgs{ + PasswordProtected: true, + } + jsonBody, _ := json.Marshal(body) + req, err := http.NewRequest("POST", "/game", bytes.NewBuffer(jsonBody)) + if err != nil { + t.Fatal(err) + } + + // Create a ResponseRecorder to capture the response + rr := httptest.NewRecorder() + + // Call the function to test + err = CreateGame(rr, req) + + // Check if the error is what we expect + expectedError := ERROR_PASSWORD_REQUIRED_BUT_NO_PASSWORD + if err == nil || err.Error() != expectedError { + t.Errorf("CreateGame() error = %v, wantErr %v", err, expectedError) + } +}