Skip to content

Commit

Permalink
Added an additional test for checking if providing no password when p…
Browse files Browse the repository at this point in the history
…assword is set to "required" will make the request fail
  • Loading branch information
justinfarrelldev committed Aug 26, 2024
1 parent 9351eba commit 9bb0c44
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/game/create_game.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions internal/game/create_game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 9bb0c44

Please sign in to comment.