Skip to content

Commit

Permalink
chore: unit tests and CI/CD (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
cchampou committed Nov 4, 2023
1 parent c22d7ce commit 390b128
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 14 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/CD.yml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -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 ./...
9 changes: 0 additions & 9 deletions .github/workflows/build.yml

This file was deleted.

7 changes: 4 additions & 3 deletions discord/security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
3 changes: 2 additions & 1 deletion duolingo/users.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package duolingo

import (
"duolingo/utils"
"encoding/json"
"net/http"
"strconv"
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module duolingo

go 1.21.0
go 1.21

require github.com/tdewolff/canvas v0.0.0-20230725155945-641901f69684

Expand Down
14 changes: 14 additions & 0 deletions utils/date.go
Original file line number Diff line number Diff line change
@@ -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")
}
32 changes: 32 additions & 0 deletions utils/date_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 390b128

Please sign in to comment.