all repos

onasty @ 215b63bea0c822935fad1478fcbbb2646a2425f0

a one-time notes service

onasty/internal/models/user_test.go (view raw)

Smirnov Oleksandr Smirnov Oleksandr
ss2316544@gmail.com
chore(golangci-lint): upgrade config (#14)..., 1 year ago
1
package models
2
3
import (
4
	"testing"
5
6
	"github.com/stretchr/testify/require"
7
)
8
9
func TestUser_Validate(t *testing.T) {
10
	tests := []struct {
11
		name string
12
		fail bool
13
14
		username string
15
		email    string
16
		password string
17
	}{
18
		{
19
			name:     "valid",
20
			fail:     false,
21
			email:    "test@example.org",
22
			username: "iuserarchbtw",
23
			password: "superhardasspassword",
24
		},
25
		{
26
			name:     "all fields empty",
27
			fail:     true,
28
			email:    "",
29
			username: "",
30
			password: "",
31
		},
32
		{
33
			name:     "invalid email",
34
			fail:     true,
35
			email:    "test",
36
			username: "iuserarchbtw",
37
			password: "superhardasspassword",
38
		},
39
		{
40
			name:     "invalid password",
41
			fail:     true,
42
			email:    "test@example.org",
43
			username: "iuserarchbtw",
44
			password: "12345",
45
		},
46
		{
47
			name:     "invalid username",
48
			fail:     true,
49
			email:    "test@example.org",
50
			username: "",
51
			password: "superhardasspassword",
52
		},
53
	}
54
55
	for _, tt := range tests {
56
		t.Run(tt.name, func(t *testing.T) {
57
			err := User{ //nolint:exhaustruct
58
				Username: tt.username,
59
				Email:    tt.email,
60
				Password: tt.password,
61
			}.Validate()
62
63
			if tt.fail {
64
				require.Error(t, err)
65
			} else {
66
				require.NoError(t, err)
67
			}
68
		})
69
	}
70
}