Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
wardviaene committed Aug 26, 2024
1 parent b500768 commit 51dfe5b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
6 changes: 3 additions & 3 deletions pkg/rest/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ func (c *Context) userStatsHandler(w http.ResponseWriter, r *http.Request) {
}
handshake, err := time.Parse(wireguard.TIMESTAMP_FORMAT, inputSplit[5])
if err == nil {
if _, ok := handshakeData[userID]; !ok {
handshakeData[userID] = []UserStatsDataPoint{}
}
handshake = handshake.Add(time.Duration(offset) * time.Minute)
if dateEqual(handshake, date) && !handshake.Equal(handshakeLast[userID]) {
if _, ok := handshakeData[userID]; !ok {
handshakeData[userID] = []UserStatsDataPoint{}
}
handshakeData[userID] = append(handshakeData[userID], UserStatsDataPoint{X: handshake.Format(wireguard.TIMESTAMP_FORMAT), Y: 1})
}
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/rest/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rest

import (
"encoding/json"
"fmt"
"net/http/httptest"
"path"
"strings"
Expand Down Expand Up @@ -31,7 +30,7 @@ func TestUserStatsHandler(t *testing.T) {
2024-08-23T20:04:03,3df97301-5f73-407a-a26b-91829f1e7f48,1,26574324,83886164,2024-08-23T20:02:53
2024-08-23T20:09:03,3df97301-5f73-407a-a26b-91829f1e7f48,1,39928520,85171728,2024-08-23T20:08:54`

statsFile := c.Storage.Client.ConfigPath(path.Join(wireguard.VPN_STATS_DIR, "user-"+time.Now().Format("2006-01-02")) + ".log")
statsFile := path.Join(wireguard.VPN_STATS_DIR, "user-"+time.Now().Format("2006-01-02")) + ".log"
err = c.Storage.Client.WriteFile(statsFile, []byte(strings.ReplaceAll(testData, "2024-08-23", time.Now().Format("2006-01-02"))))
if err != nil {
t.Fatalf("Cannot write test file")
Expand Down Expand Up @@ -63,5 +62,8 @@ func TestUserStatsHandler(t *testing.T) {
if userStatsResponse.TransmitBytes.Datasets[0].Data[1].Y != 813588 {
t.Fatalf("unexpected data: %f", userStatsResponse.TransmitBytes.Datasets[0].Data[1].Y)
}
fmt.Printf("%+v\n", userStatsResponse.Handshakes)
if userStatsResponse.Handshakes.Datasets[0].Data[1].X != "2024-08-25T18:30:42" {
t.Fatalf("unexpected data: %s", userStatsResponse.Handshakes.Datasets[0].Data[1].X)
}

}

0 comments on commit 51dfe5b

Please sign in to comment.