diff --git a/cmd/web/handlers_test.go b/cmd/web/handlers_test.go index 1e5e65a..300bfe0 100644 --- a/cmd/web/handlers_test.go +++ b/cmd/web/handlers_test.go @@ -14,6 +14,8 @@ import ( "time" ) +const baseUri = "/api/" + func TestCreateElection(t *testing.T) { app := newTestApplication(t) server := newTestServer(t, app.routes()) @@ -24,7 +26,7 @@ func TestCreateElection(t *testing.T) { On("Insert", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). Return(1, nil) - path := "/api/election" + path := baseUri + "election" tests := []struct { name string @@ -200,7 +202,7 @@ func TestCreateElection_ServerError(t *testing.T) { On("Insert", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). Return(0, fmt.Errorf("")) - path := "/api/election" + path := baseUri + "election" requestBody := api.CreateElectionRequest{ Choices: []string{"宮本武蔵", "伊東一刀斎"}, ExpiresAt: time.Now().Add(24 * time.Hour), @@ -254,7 +256,7 @@ func TestCreateVotes_UnknownVotersElection(t *testing.T) { On("Insert", mock.Anything, mock.Anything, mock.Anything, mock.Anything). Return(1, nil) - path := "/api/election/1/votes" + path := baseUri + "election/1/votes" tests := []struct { name string @@ -359,7 +361,7 @@ func TestCreateVotes_KnownVotersElection(t *testing.T) { On("Exists", mock.Anything, mock.Anything). Return(false, nil) - path := "/api/election/1/votes" + path := baseUri + "election/1/votes" tests := []struct { name string @@ -467,7 +469,7 @@ func TestCreateVotes_NonExistingElection(t *testing.T) { On("GetById", mock.Anything). Return((*models.Election)(nil), sql.ErrNoRows) - path := "/api/election/1/votes" + path := baseUri + "election/1/votes" requestBody := api.CreateVotesRequest{ Choices: []struct { ChoiceText string `json:"choiceText"` @@ -498,7 +500,7 @@ func TestCreateVotes_NonNumberElectionID(t *testing.T) { On("GetById", mock.Anything). Return((*models.Election)(nil), sql.ErrNoRows) - path := "/api/election/1a/votes" + path := baseUri + "election/1a/votes" requestBody := api.CreateVotesRequest{ Choices: []struct { ChoiceText string `json:"choiceText"` @@ -555,8 +557,8 @@ func TestCreateVotes_AlreadyVoted(t *testing.T) { On("Exists", mock.Anything, mock.Anything). Return(true, nil) - knownVotersElectionPath := "/api/election/1/votes" - unknownVotersElectionPath := "/api/election/2/votes" + knownVotersElectionPath := baseUri + "election/1/votes" + unknownVotersElectionPath := baseUri + "election/2/votes" voterIdentity := "anything" tests := []struct { @@ -640,7 +642,7 @@ func TestCreateVotes_UnknownVotersElectionMaxVotersReached(t *testing.T) { On("CountByElection", mock.Anything). Return(10, nil) - path := "/api/election/1/votes" + path := baseUri + "election/1/votes" requestBody := api.CreateVotesRequest{ Choices: []struct { ChoiceText string `json:"choiceText"` @@ -691,7 +693,7 @@ func TestCreateVotes_ExpiredElection(t *testing.T) { On("CountByElection", mock.Anything). Return(10, nil) - path := "/api/election/1/votes" + path := baseUri + "election/1/votes" requestBody := api.CreateVotesRequest{ Choices: []struct { ChoiceText string `json:"choiceText"` @@ -759,7 +761,7 @@ func TestGetElectionResults(t *testing.T) { On("GetByElection", mock.Anything). Return(&votes, nil) - path := "/api/election/1/results" + path := baseUri + "election/1/results" code, _, body := server.get(t, path) assert.Equal(t, http.StatusOK, code) @@ -783,3 +785,5 @@ func TestGetElectionResults(t *testing.T) { } } } + +// TODO write test for not found getElectionResults