all repos

onasty @ c0b5079c234cf570a675ada6bdd08b690e4e91c7

a one-time notes service

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

Smirnov Oleksandr Smirnov Oleksandr
ss2316544@gmail.com
refactor: remove `username` (#112)..., 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
		email    string
15
		password string
16
	}{
17
		{
18
			name:     "valid",
19
			fail:     false,
20
			email:    "test@example.org",
21
			password: "superhardasspassword",
22
		},
23
		{
24
			name:     "all fields empty",
25
			fail:     true,
26
			email:    "",
27
			password: "",
28
		},
29
		{
30
			name:     "invalid email",
31
			fail:     true,
32
			email:    "test",
33
			password: "superhardasspassword",
34
		},
35
		{
36
			name:     "invalid password",
37
			fail:     true,
38
			email:    "test@example.org",
39
			password: "12345",
40
		},
41
	}
42
43
	for _, tt := range tests {
44
		t.Run(tt.name, func(t *testing.T) {
45
			err := User{ //nolint:exhaustruct
46
				Email:    tt.email,
47
				Password: tt.password,
48
			}.Validate()
49
50
			if tt.fail {
51
				require.Error(t, err)
52
			} else {
53
				require.NoError(t, err)
54
			}
55
		})
56
	}
57
}