From 7a414da9a802d5eeee911b3536790a061e1d7503 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Thu, 29 Jun 2023 23:33:02 +0200 Subject: ref: Move all controller under the same folder Move all controller to the same folder and rename them to service. Moving them to the same folder allow an easier setup for testing. --- pkg/service/auth_test.go | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 pkg/service/auth_test.go (limited to 'pkg/service/auth_test.go') 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:]...) +} -- cgit v1.2.3