From 7e621eb26310a62d3502d146d6497d56dc42718d Mon Sep 17 00:00:00 2001 From: Leigh MacDonald Date: Fri, 21 Jun 2024 05:01:16 -0600 Subject: [PATCH] Add votes log tests --- internal/test/main_test.go | 29 ++++++++++++++++- internal/test/servers_test.go | 12 +++---- internal/test/votes_test.go | 59 +++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 internal/test/votes_test.go diff --git a/internal/test/main_test.go b/internal/test/main_test.go index d84405d5..8a4a8067 100644 --- a/internal/test/main_test.go +++ b/internal/test/main_test.go @@ -49,6 +49,7 @@ import ( var ( container *postgresContainer + testServer domain.Server configUC domain.ConfigUsecase wikiUC domain.WikiUsecase personUC domain.PersonUsecase @@ -72,6 +73,7 @@ var ( serversUC domain.ServersUsecase stateUC domain.StateUsecase votesUC domain.VoteUsecase + votesRepo domain.VoteRepository wordFilterUC domain.WordFilterUsecase ) @@ -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() } @@ -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 } diff --git a/internal/test/servers_test.go b/internal/test/servers_test.go index 0d7e41dd..46b1c094 100644 --- a/internal/test/servers_test.go +++ b/internal/test/servers_test.go @@ -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", @@ -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", @@ -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, }, diff --git a/internal/test/votes_test.go b/internal/test/votes_test.go new file mode 100644 index 00000000..fbfc00f7 --- /dev/null +++ b/internal/test/votes_test.go @@ -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, + }, + }) +}