-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolls_test.go
69 lines (47 loc) · 1.55 KB
/
polls_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
"testing"
"github.com/AnshVM/flashpoll-backend/db"
"github.com/AnshVM/flashpoll-backend/router"
"github.com/AnshVM/flashpoll-backend/types"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/stretchr/testify/assert"
)
type OptionResponse = types.OptionResponse
type GetPollResponse = types.GetPollResponse
func login(r *gin.Engine, email string, password string) string {
payload := map[string]any{
"email": email,
"password": password,
}
response := request(r, "POST", "/login", payload, nil)
decoded := decodeJSON[map[string]string](response)
return decoded["accessToken"]
}
func getPollById(r *gin.Engine, pollID uint) GetPollResponse {
response := request(r, "GET", fmt.Sprintf("/poll/%d", pollID), nil, nil)
return decodeJSON[GetPollResponse](response)
}
func submitVote(r *gin.Engine, optionID uint, accessToken string) {
headers := map[string]string{"Authorization": fmt.Sprintf("Bearer %s", accessToken)}
payload := map[string]any{"optionID": optionID}
request(r, "POST", "/poll/submit", payload, headers)
}
func TestVoteSubmission(t *testing.T) {
POLL_ID := 8
godotenv.Load()
err := db.CreateConnection()
if err != nil {
panic(err)
}
r := router.SetupRouter()
accessToken := login(r, "[email protected]", "test")
pollData := getPollById(r, uint(POLL_ID))
votesBefore := pollData.Options[0].Votes
submitVote(r, pollData.Options[0].ID, accessToken)
pollData = getPollById(r, uint(POLL_ID))
votesAfter := pollData.Options[0].Votes
assert.Equal(t, 1, votesAfter-votesBefore)
}