Skip to content

Commit

Permalink
Add votes log tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmacdonald committed Jun 21, 2024
1 parent d284ce4 commit 7e621eb
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 7 deletions.
29 changes: 28 additions & 1 deletion internal/test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (

var (
container *postgresContainer
testServer domain.Server
configUC domain.ConfigUsecase
wikiUC domain.WikiUsecase
personUC domain.PersonUsecase
Expand All @@ -72,6 +73,7 @@ var (
serversUC domain.ServersUsecase
stateUC domain.StateUsecase
votesUC domain.VoteUsecase
votesRepo domain.VoteRepository
wordFilterUC domain.WordFilterUsecase
)

Expand Down Expand Up @@ -135,10 +137,34 @@ func TestMain(m *testing.M) {

matchUC = match.NewMatchUsecase(match.NewMatchRepository(eventBroadcaster, databaseConn, personUC, serversUC, discordUC, stateUC, weaponsMap), stateUC, serversUC, discordUC)
chatUC = chat.NewChatUsecase(configUC, chat.NewChatRepository(databaseConn, personUC, wordFilterUC, matchUC, eventBroadcaster), wordFilterUC, stateUC, banSteamUC, personUC, discordUC)
votesUC = votes.NewVoteUsecase(votes.NewVoteRepository(databaseConn), personUC, matchUC, discordUC, configUC, eventBroadcaster)
votesRepo = votes.NewVoteRepository(databaseConn)
votesUC = votes.NewVoteUsecase(votesRepo, personUC, matchUC, discordUC, configUC, eventBroadcaster)

container = dbContainer

server, errServer := serversUC.Save(context.Background(), domain.RequestServerUpdate{
ServerName: stringutil.SecureRandomString(20),
ServerNameShort: stringutil.SecureRandomString(5),
Host: "1.2.3.4",
Port: 27015,
ReservedSlots: 8,
Password: stringutil.SecureRandomString(8),
RCON: stringutil.SecureRandomString(8),
Lat: 10,
Lon: 10,
CC: "de",
Region: "eu",
IsEnabled: true,
EnableStats: false,
LogSecret: 23456789,
})

if errServer != nil {
panic(errStore)
}

testServer = server

m.Run()
}

Expand All @@ -159,6 +185,7 @@ func testRouter() *gin.Engine {
steamgroup.NewSteamgroupHandler(router, banGroupUC, authUC)
news.NewNewsHandler(router, newsUC, discordUC, authUC)
wiki.NewWIkiHandler(router, wikiUC, authUC)
votes.NewVoteHandler(router, votesUC, authUC)

return router
}
Expand Down
12 changes: 6 additions & 6 deletions internal/test/servers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ func TestServers(t *testing.T) {

var servers []domain.Server
testEndpointWithReceiver(t, router, http.MethodGet, "/api/servers_admin", nil, http.StatusOK, owner, &servers)
require.Empty(t, servers)
require.Len(t, servers, 1)

var safeServers []domain.ServerInfoSafe
testEndpointWithReceiver(t, router, http.MethodGet, "/api/servers", nil, http.StatusOK, user, &safeServers)
require.Empty(t, servers)
require.Len(t, safeServers, 1)

newServer := domain.RequestServerUpdate{
ServerName: "test-1 long",
Expand Down Expand Up @@ -59,10 +59,10 @@ func TestServers(t *testing.T) {
require.Equal(t, newServer.LogSecret, server.LogSecret)

testEndpointWithReceiver(t, router, http.MethodGet, "/api/servers_admin", nil, http.StatusOK, owner, &servers)
require.NotEmpty(t, servers)
require.Len(t, servers, 2)

testEndpointWithReceiver(t, router, http.MethodGet, "/api/servers", nil, http.StatusOK, user, &safeServers)
require.NotEmpty(t, servers)
require.Len(t, safeServers, 2)

update := domain.RequestServerUpdate{
ServerName: "test-2 long",
Expand Down Expand Up @@ -119,14 +119,14 @@ func TestServersPermissions(t *testing.T) {
levels: admin,
},
{
path: "/api/servers/:server_id",
path: "/api/servers/1",
method: http.MethodDelete,
code: http.StatusForbidden,
levels: admin,
},
{
path: "/api/servers_admin",
method: http.MethodPost,
method: http.MethodGet,
code: http.StatusForbidden,
levels: admin,
},
Expand Down
59 changes: 59 additions & 0 deletions internal/test/votes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package test_test

import (
"context"
"net/http"
"testing"
"time"

"github.com/leighmacdonald/gbans/internal/domain"
"github.com/leighmacdonald/gbans/pkg/logparse"
"github.com/stretchr/testify/require"
)

func TestVotes(t *testing.T) {
t.Parallel()

router := testRouter()
source := getUser()
target := getUser()
moderator := loginUser(getModerator())

var results domain.LazyResult
req := domain.VoteQueryFilter{
Success: -1,
}
testEndpointWithReceiver(t, router, http.MethodPost, "/api/votes", req, http.StatusOK, moderator, &results)
require.Empty(t, results.Data)

require.NoError(t, votesRepo.AddResult(context.Background(), domain.VoteResult{
SourceID: source.SteamID,
SourceName: source.PersonaName,
SourceAvatarHash: source.AvatarHash,
TargetID: target.SteamID,
TargetName: target.PersonaName,
TargetAvatarHash: target.AvatarHash,
Name: "kick",
Success: false,
ServerID: testServer.ServerID,
ServerName: testServer.ShortName,
Code: logparse.VoteCodeFailNoOutnumberYes,
CreatedOn: time.Now(),
}))

testEndpointWithReceiver(t, router, http.MethodPost, "/api/votes", req, http.StatusOK, moderator, &results)
require.NotEmpty(t, results.Data)
}

func TestVotesPermissions(t *testing.T) {
t.Parallel()

testPermissions(t, testRouter(), []permTestValues{
{
path: "/api/votes",
method: http.MethodPost,
code: http.StatusForbidden,
levels: moderators,
},
})
}

0 comments on commit 7e621eb

Please sign in to comment.