Skip to content

Commit 97af6f3

Browse files
authored
Add some tests to get started (#104)
1 parent a90206b commit 97af6f3

File tree

8 files changed

+165
-2
lines changed

8 files changed

+165
-2
lines changed

.github/workflows/go-build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ jobs:
3232

3333
- name: Lint
3434
run: golangci-lint run
35+
36+
- name: Run Tests and Generate Coverage
37+
run: go test -v -coverprofile=coverage.out ./...

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ core
33
tmp
44
task-wizard.db
55
.vscode/launch.json
6+
coverage.out

go.mod

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,17 @@ require (
1717
gorm.io/gorm v1.25.10
1818
)
1919

20-
require github.com/wneessen/go-mail v0.6.2
20+
require (
21+
github.com/stretchr/testify v1.10.0
22+
github.com/wneessen/go-mail v0.6.2
23+
gorm.io/driver/sqlite v1.5.7
24+
)
25+
26+
require (
27+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
28+
github.com/mattn/go-sqlite3 v1.14.22 // indirect
29+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
30+
)
2131

2232
require (
2333
github.com/bytedance/sonic v1.11.8 // indirect

go.sum

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0V
7575
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
7676
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
7777
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
78+
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
79+
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
7880
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
7981
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
8082
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -119,8 +121,9 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
119121
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
120122
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
121123
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
122-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
123124
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
125+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
126+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
124127
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
125128
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
126129
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
@@ -237,6 +240,8 @@ gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
237240
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
238241
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
239242
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
243+
gorm.io/driver/sqlite v1.5.7 h1:8NvsrhP0ifM7LX9G4zPB97NwovUakUxc+2V2uuf3Z1I=
244+
gorm.io/driver/sqlite v1.5.7/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
240245
gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
241246
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
242247
modernc.org/cc/v4 v4.21.4 h1:3Be/Rdo1fpr8GrQ7IVw9OHtplU4gWbb+wNgeoBMmGLQ=

internal/utils/auth/auth_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package auth
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestEncodePasswordAndMatches(t *testing.T) {
10+
password := "securepassword"
11+
hashedPassword, err := EncodePassword(password)
12+
assert.NoError(t, err)
13+
assert.NoError(t, Matches(hashedPassword, password))
14+
assert.Error(t, Matches(hashedPassword, "wrongpassword"))
15+
}
16+
17+
func TestEncodeAndDecodeEmailAndCode(t *testing.T) {
18+
email := "user@example.com"
19+
code := "reset-code"
20+
21+
encoded := EncodeEmailAndCode(email, code)
22+
decodedEmail, decodedCode, err := DecodeEmailAndCode(encoded)
23+
24+
assert.NoError(t, err)
25+
assert.Equal(t, email, decodedEmail)
26+
assert.Equal(t, code, decodedCode)
27+
}
28+
29+
func TestGenerateEmailResetToken(t *testing.T) {
30+
token, err := GenerateEmailResetToken(nil)
31+
assert.NoError(t, err)
32+
assert.NotEmpty(t, token)
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package database
2+
3+
import (
4+
"testing"
5+
6+
"dkhalife.com/tasks/core/config"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestNewDatabase(t *testing.T) {
11+
// Mock configuration
12+
cfg := &config.Config{
13+
Database: config.DatabaseConfig{
14+
FilePath: ":memory:", // Use in-memory SQLite database for testing
15+
},
16+
}
17+
18+
// Test database connection
19+
db, err := NewDatabase(cfg)
20+
assert.NoError(t, err)
21+
assert.NotNil(t, db)
22+
23+
// Verify the connection
24+
sqlDB, err := db.DB()
25+
assert.NoError(t, err)
26+
assert.NoError(t, sqlDB.Ping())
27+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package middleware
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
"time"
8+
9+
"dkhalife.com/tasks/core/config"
10+
"github.com/gin-gonic/gin"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestRateLimitMiddleware(t *testing.T) {
15+
cfg := &config.Config{
16+
Server: config.ServerConfig{
17+
RateLimit: 1,
18+
RatePeriod: time.Second,
19+
},
20+
}
21+
limiter := NewRateLimiter(cfg)
22+
23+
router := gin.New()
24+
router.Use(RateLimitMiddleware(limiter))
25+
router.GET("/", func(c *gin.Context) {
26+
c.String(http.StatusOK, "OK")
27+
})
28+
29+
w := httptest.NewRecorder()
30+
req, _ := http.NewRequest("GET", "/", nil)
31+
router.ServeHTTP(w, req)
32+
assert.Equal(t, http.StatusOK, w.Code)
33+
34+
w = httptest.NewRecorder()
35+
router.ServeHTTP(w, req)
36+
assert.Equal(t, http.StatusTooManyRequests, w.Code)
37+
}
38+
39+
func TestRequestLogger(t *testing.T) {
40+
router := gin.New()
41+
router.Use(RequestLogger())
42+
router.GET("/", func(c *gin.Context) {
43+
c.String(http.StatusOK, "OK")
44+
})
45+
46+
w := httptest.NewRecorder()
47+
req, _ := http.NewRequest("GET", "/", nil)
48+
router.ServeHTTP(w, req)
49+
assert.Equal(t, http.StatusOK, w.Code)
50+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package migration
2+
3+
import (
4+
"testing"
5+
6+
"dkhalife.com/tasks/core/internal/models"
7+
"github.com/stretchr/testify/assert"
8+
"gorm.io/driver/sqlite"
9+
"gorm.io/gorm"
10+
)
11+
12+
func TestMigration(t *testing.T) {
13+
// Create an in-memory SQLite database for testing
14+
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
15+
assert.NoError(t, err)
16+
17+
// Run the migration
18+
err = Migration(db)
19+
assert.NoError(t, err)
20+
21+
// Verify that tables were created
22+
for _, model := range []interface{}{
23+
&models.User{},
24+
&models.UserPasswordReset{},
25+
&models.AppToken{},
26+
&models.Label{},
27+
&models.Task{},
28+
&models.TaskHistory{},
29+
&models.NotificationSettings{},
30+
&models.Notification{},
31+
} {
32+
assert.True(t, db.Migrator().HasTable(model), "Table for model should exist")
33+
}
34+
}

0 commit comments

Comments
 (0)