aboutsummaryrefslogtreecommitdiff
path: root/pkg/service/auth_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/service/auth_test.go')
-rw-r--r--pkg/service/auth_test.go78
1 files changed, 78 insertions, 0 deletions
diff --git a/pkg/service/auth_test.go b/pkg/service/auth_test.go
new file mode 100644
index 0000000..35b2475
--- /dev/null
+++ b/pkg/service/auth_test.go
@@ -0,0 +1,78 @@
+//go:build unit
+
+package service
+
+import (
+ "context"
+ "testing"
+
+ "git.sr.ht/~gabrielgio/img/pkg/database/repository"
+ "git.sr.ht/~gabrielgio/img/pkg/ext"
+ "git.sr.ht/~gabrielgio/img/pkg/testkit"
+)
+
+type (
+ scene struct {
+ ctx context.Context
+ authRepository repository.AuthRepository
+ userRepository repository.UserRepository
+ controller *AuthController
+ }
+)
+
+var (
+ key = []byte("6368616e676520746869732070617373")
+)
+
+func setUp() *scene {
+ userRepository := NewUserRepository()
+ return &scene{
+ ctx: context.Background(),
+ authRepository: userRepository,
+ userRepository: userRepository,
+ controller: NewAuthController(userRepository, userRepository, key),
+ }
+}
+
+func TestInitialRegisterAndLogin(t *testing.T) {
+ testCases := []struct {
+ name string
+ username string
+ password []byte
+ }{
+ {
+ name: "Normal register",
+ username: "username",
+ password: []byte("this is an password"),
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ scene := setUp()
+
+ err := scene.controller.InitialRegister(scene.ctx, []byte(tc.username), tc.password, []byte("/"))
+ testkit.TestFatalError(t, "Register", err)
+
+ users, err := scene.userRepository.List(scene.ctx)
+ userID := users[0].ID
+
+ user, err := scene.userRepository.Get(scene.ctx, userID)
+ testkit.TestFatalError(t, "Get", err)
+ testkit.TestValue(t, "Register", tc.username, user.Username)
+
+ auth, err := scene.controller.Login(scene.ctx, []byte(tc.username), tc.password)
+ testkit.TestFatalError(t, "Login", err)
+
+ token, err := ext.ReadToken(auth, key)
+ testkit.TestFatalError(t, "Login", err)
+
+ testkit.TestValue(t, "Login", tc.username, token.Username)
+ testkit.TestValue(t, "Login", userID, token.UserID)
+ })
+ }
+}
+
+func remove[T any](slice []T, s int) []T {
+ return append(slice[:s], slice[s+1:]...)
+}