From 390b128a52aff02314f971112e989882ba5c567e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Champouillon?= Date: Sat, 4 Nov 2023 09:23:48 +0100 Subject: [PATCH] chore: unit tests and CI/CD (#1) --- .github/workflows/CD.yml | 18 ++++++++++++++++++ .github/workflows/CI.yml | 11 +++++++++++ .github/workflows/build.yml | 9 --------- discord/security_test.go | 7 ++++--- duolingo/users.go | 3 ++- go.mod | 2 +- utils/date.go | 14 ++++++++++++++ utils/date_test.go | 32 ++++++++++++++++++++++++++++++++ 8 files changed, 82 insertions(+), 14 deletions(-) create mode 100644 .github/workflows/CD.yml create mode 100644 .github/workflows/CI.yml delete mode 100644 .github/workflows/build.yml create mode 100644 utils/date.go create mode 100644 utils/date_test.go diff --git a/.github/workflows/CD.yml b/.github/workflows/CD.yml new file mode 100644 index 0000000..57eadd3 --- /dev/null +++ b/.github/workflows/CD.yml @@ -0,0 +1,18 @@ +name: Continuous Deployment +on: + push: + branches: + - main +jobs: + build: + name: Build and publish Docker image + runs-on: ubuntu-latest + steps: + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - uses: actions/checkout@v2 + - name: Build and publish + run: ./build.sh diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml new file mode 100644 index 0000000..288259b --- /dev/null +++ b/.github/workflows/CI.yml @@ -0,0 +1,11 @@ +name: Continuous Integration +on: [pull_request] + +jobs: + test: + name: Test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Test + run: go test -v ./... diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 3f4bcf6..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,9 +0,0 @@ -name: Build the Docker image -on: [push] -jobs: - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Build and publish - run: ./build.sh diff --git a/discord/security_test.go b/discord/security_test.go index 1fda744..7b2ecf3 100644 --- a/discord/security_test.go +++ b/discord/security_test.go @@ -34,10 +34,11 @@ func TestGetRequestSignature(t *testing.T) { } func TestVerifySignature(t *testing.T) { + t.Setenv("DISCORD_BOT_TOKEN", "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") + t.Setenv("DISCORD_PUBLIC_KEY", "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") req := createRequestWithInvalidSignature() err := VerifySignature(req) - if err != nil { - t.Skip() + if err.Error() != "invalid signature" { + t.Fail() } - t.Fail() } diff --git a/duolingo/users.go b/duolingo/users.go index 6ee783f..48c0971 100644 --- a/duolingo/users.go +++ b/duolingo/users.go @@ -1,6 +1,7 @@ package duolingo import ( + "duolingo/utils" "encoding/json" "net/http" "strconv" @@ -24,7 +25,7 @@ type XPSummaries struct { } func getXPGains(userId int) int { - req, newRequestErr := http.NewRequest("GET", BaseURL+"/users/"+strconv.Itoa(userId)+"/xp_summaries?startDate=2023-11-01", nil) + req, newRequestErr := http.NewRequest("GET", BaseURL+"/users/"+strconv.Itoa(userId)+"/xp_summaries?startDate="+utils.FormatBeginningOfMonth(utils.GetBeginningOfMonth()), nil) if newRequestErr != nil { println("Error creating request") return 0 diff --git a/go.mod b/go.mod index 84b0ffa..64a7b04 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module duolingo -go 1.21.0 +go 1.21 require github.com/tdewolff/canvas v0.0.0-20230725155945-641901f69684 diff --git a/utils/date.go b/utils/date.go new file mode 100644 index 0000000..1445182 --- /dev/null +++ b/utils/date.go @@ -0,0 +1,14 @@ +package utils + +import ( + "time" +) + +func GetBeginningOfMonth() time.Time { + now := time.Now() + return time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC) +} + +func FormatBeginningOfMonth(date time.Time) string { + return date.Format("2006-01-02") +} diff --git a/utils/date_test.go b/utils/date_test.go new file mode 100644 index 0000000..fbe7549 --- /dev/null +++ b/utils/date_test.go @@ -0,0 +1,32 @@ +package utils + +import ( + "testing" + "time" +) + +func TestGetBeginningOfMonth(t *testing.T) { + date := GetBeginningOfMonth() + if date.Day() != 1 { + t.Fatalf("Expected day to be 1, got %d", date.Day()) + } + if date.Hour() != 0 { + t.Fatalf("Expected hour to be 0, got %d", date.Hour()) + } + if date.Minute() != 0 { + t.Fatalf("Expected minute to be 0, got %d", date.Minute()) + } + if date.Second() != 0 { + t.Fatalf("Expected second to be 0, got %d", date.Second()) + } + if date.Nanosecond() != 0 { + t.Fatalf("Expected nanosecond to be 0, got %d", date.Nanosecond()) + } +} + +func TestFormatBeginningOfMonth(t *testing.T) { + date := FormatBeginningOfMonth(time.Date(2021, 10, 1, 0, 0, 0, 0, time.UTC)) + if date != "2021-10-01" { + t.Fatalf("Expected date to be 2021-10-01, got %s", date) + } +}