aboutsummaryrefslogtreecommitdiff
path: root/pkg/components/auth/controller_test.go
blob: 33aa901cf701dcc8675776c97f36b57e7a202882 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//go:build unit

package auth

import (
	"context"
	"errors"
	"testing"

	"github.com/samber/lo"

	"git.sr.ht/~gabrielgio/img/pkg/ext"
	"git.sr.ht/~gabrielgio/img/pkg/testkit"
)

type (
	scene struct {
		ctx            context.Context
		mockRepository *MockUserRepository
		controller     Controller
	}

	mockUser struct {
		id       uint
		username string
		password []byte
	}

	MockUserRepository struct {
		index uint
		users []*mockUser
		err   error
	}
)

var (
	_   Repository = &MockUserRepository{}
	key            = []byte("6368616e676520746869732070617373")
)

func setUp() *scene {
	mockUserRepository := &MockUserRepository{}
	return &scene{
		ctx:            context.Background(),
		mockRepository: mockUserRepository,
		controller:     *NewController(mockUserRepository, key),
	}
}

func TestRegisterAndLogin(t *testing.T) {
	testCases := []struct {
		name     string
		username string
		password []byte
	}{
		{
			name:     "Normal register",
			username: "username",
			password: []byte("password"),
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			scene := setUp()

			err := scene.controller.Register(scene.ctx, []byte(tc.username), tc.password)
			testkit.TestFatalError(t, "Register", err)

			userID := scene.mockRepository.GetLastId()

			user, err := scene.mockRepository.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 toUser(m *mockUser, _ int) *User {
	return &User{
		ID:       m.id,
		Username: m.username,
	}
}

func (m *MockUserRepository) GetLastId() uint {
	return m.index
}

func (m *MockUserRepository) List(ctx context.Context) ([]*User, error) {
	if m.err != nil {
		return nil, m.err
	}

	return lo.Map(m.users, toUser), nil
}

func (m *MockUserRepository) Get(ctx context.Context, id uint) (*User, error) {
	if m.err != nil {
		return nil, m.err
	}

	for _, m := range m.users {
		if m.id == id {
			return toUser(m, 0), nil
		}
	}
	return nil, errors.New("Item not found")
}

func (m *MockUserRepository) GetIDByUsername(ctx context.Context, username string) (uint, error) {
	if m.err != nil {
		return 0, m.err
	}

	for _, m := range m.users {
		if m.username == username {
			return m.id, nil
		}
	}
	return 0, errors.New("Item not found")
}

func (m *MockUserRepository) GetPassword(ctx context.Context, id uint) ([]byte, error) {
	if m.err != nil {
		return nil, m.err
	}

	for _, m := range m.users {
		if m.id == id {
			return m.password, nil
		}
	}
	return nil, errors.New("Item not found")
}

func (m *MockUserRepository) Create(ctx context.Context, createUser *CreateUser) (uint, error) {
	if m.err != nil {
		return 0, m.err
	}

	m.index++

	m.users = append(m.users, &mockUser{
		id:       m.index,
		username: createUser.Username,
		password: createUser.Password,
	})

	return m.index, nil
}

func (m *MockUserRepository) Update(ctx context.Context, id uint, update *UpdateUser) error {
	if m.err != nil {
		return m.err
	}

	for _, m := range m.users {
		if m.id == id {
			m.username = update.Username
		}
	}
	return nil
}

func remove[T any](slice []T, s int) []T {
	return append(slice[:s], slice[s+1:]...)
}

func (r *MockUserRepository) Delete(ctx context.Context, id uint) error {
	if r.err != nil {
		return r.err
	}

	for i, m := range r.users {
		if m.id == id {
			r.users = remove(r.users, i)
		}
	}
	return nil
}