Browse Source

test: Add user mail testing. (#833)

Bo-Yi Wu 3 years ago
parent
commit
b6da658553
3 changed files with 220 additions and 0 deletions
  1. 35 0
      models/fixtures/email_address.yml
  2. 16 0
      models/fixtures/user.yml
  3. 169 0
      models/user_mail_test.go

+ 35 - 0
models/fixtures/email_address.yml

@@ -0,0 +1,35 @@
1
+-
2
+  id: 1
3
+  uid: 1
4
+  email: user11@example.com
5
+  is_activated: false
6
+
7
+-
8
+  id: 2
9
+  uid: 1
10
+  email: user12@example.com
11
+  is_activated: false
12
+
13
+-
14
+  id: 3
15
+  uid: 2
16
+  email: user2@example.com
17
+  is_activated: true
18
+
19
+-
20
+  id: 4
21
+  uid: 2
22
+  email: user21@example.com
23
+  is_activated: false
24
+
25
+-
26
+  id: 5
27
+  uid: 9999999
28
+  email: user9999999@example.com
29
+  is_activated: true
30
+
31
+-
32
+  id: 6
33
+  uid: 10
34
+  email: user101@example.com
35
+  is_activated: true

+ 16 - 0
models/fixtures/user.yml

@@ -132,3 +132,19 @@
132 132
   num_repos: 0
133 133
   num_members: 1
134 134
   is_active: false
135
+
136
+-
137
+  id: 10
138
+  lower_name: user10
139
+  name: user10
140
+  full_name: User Ten
141
+  email: user10@example.com
142
+  passwd: password
143
+  type: 0 # user
144
+  salt: salt
145
+  is_admin: false
146
+  avatar: avatar10
147
+  avatar_email: user10@example.com
148
+  num_repos: 0
149
+  num_members: 1
150
+  is_active: true

+ 169 - 0
models/user_mail_test.go

@@ -0,0 +1,169 @@
1
+// Copyright 2017 The Gitea Authors. All rights reserved.
2
+// Use of this source code is governed by a MIT-style
3
+// license that can be found in the LICENSE file.
4
+
5
+package models
6
+
7
+import (
8
+	"testing"
9
+
10
+	"github.com/stretchr/testify/assert"
11
+)
12
+
13
+func TestGetEmailAddresses(t *testing.T) {
14
+	assert.NoError(t, PrepareTestDatabase())
15
+
16
+	emails, _ := GetEmailAddresses(int64(1))
17
+	assert.Len(t, emails, 3)
18
+	assert.False(t, emails[0].IsPrimary)
19
+	assert.True(t, emails[2].IsActivated)
20
+	assert.True(t, emails[2].IsPrimary)
21
+
22
+	emails, _ = GetEmailAddresses(int64(2))
23
+	assert.Len(t, emails, 2)
24
+	assert.True(t, emails[0].IsPrimary)
25
+	assert.True(t, emails[0].IsActivated)
26
+}
27
+
28
+func TestIsEmailUsed(t *testing.T) {
29
+	assert.NoError(t, PrepareTestDatabase())
30
+
31
+	isExist, _ := IsEmailUsed("")
32
+	assert.True(t, isExist)
33
+	isExist, _ = IsEmailUsed("user11@example.com")
34
+	assert.True(t, isExist)
35
+	isExist, _ = IsEmailUsed("user1234567890@example.com")
36
+	assert.False(t, isExist)
37
+}
38
+
39
+func TestAddEmailAddress(t *testing.T) {
40
+	assert.NoError(t, PrepareTestDatabase())
41
+
42
+	assert.NoError(t, AddEmailAddress(&EmailAddress{
43
+		Email:       "user1234567890@example.com",
44
+		IsPrimary:   true,
45
+		IsActivated: true,
46
+	}))
47
+
48
+	// ErrEmailAlreadyUsed
49
+	err := AddEmailAddress(&EmailAddress{
50
+		Email: "user1234567890@example.com",
51
+	})
52
+	assert.Error(t, err)
53
+	assert.True(t, IsErrEmailAlreadyUsed(err))
54
+}
55
+
56
+func TestAddEmailAddresses(t *testing.T) {
57
+	assert.NoError(t, PrepareTestDatabase())
58
+
59
+	// insert multiple email address
60
+	emails := make([]*EmailAddress, 2)
61
+	emails[0] = &EmailAddress{
62
+		Email:       "user1234@example.com",
63
+		IsActivated: true,
64
+	}
65
+	emails[1] = &EmailAddress{
66
+		Email:       "user5678@example.com",
67
+		IsActivated: true,
68
+	}
69
+	assert.NoError(t, AddEmailAddresses(emails))
70
+
71
+	// ErrEmailAlreadyUsed
72
+	err := AddEmailAddresses(emails)
73
+	assert.Error(t, err)
74
+	assert.True(t, IsErrEmailAlreadyUsed(err))
75
+}
76
+
77
+func TestDeleteEmailAddress(t *testing.T) {
78
+	assert.NoError(t, PrepareTestDatabase())
79
+
80
+	assert.NoError(t, DeleteEmailAddress(&EmailAddress{
81
+		UID:   int64(1),
82
+		ID:    int64(1),
83
+		Email: "user11@example.com",
84
+	}))
85
+
86
+	assert.NoError(t, DeleteEmailAddress(&EmailAddress{
87
+		UID:   int64(1),
88
+		Email: "user12@example.com",
89
+	}))
90
+
91
+	// Email address does not exist
92
+	err := DeleteEmailAddress(&EmailAddress{
93
+		UID:   int64(1),
94
+		Email: "user1234567890@example.com",
95
+	})
96
+	assert.Error(t, err)
97
+}
98
+
99
+func TestDeleteEmailAddresses(t *testing.T) {
100
+	assert.NoError(t, PrepareTestDatabase())
101
+
102
+	// delete multiple email address
103
+	emails := make([]*EmailAddress, 2)
104
+	emails[0] = &EmailAddress{
105
+		UID:   int64(2),
106
+		ID:    int64(3),
107
+		Email: "user2@example.com",
108
+	}
109
+	emails[1] = &EmailAddress{
110
+		UID:   int64(2),
111
+		Email: "user21@example.com",
112
+	}
113
+	assert.NoError(t, DeleteEmailAddresses(emails))
114
+
115
+	// ErrEmailAlreadyUsed
116
+	err := DeleteEmailAddresses(emails)
117
+	assert.Error(t, err)
118
+}
119
+
120
+func TestMakeEmailPrimary(t *testing.T) {
121
+	assert.NoError(t, PrepareTestDatabase())
122
+
123
+	email := &EmailAddress{
124
+		Email: "user567890@example.com",
125
+	}
126
+	err := MakeEmailPrimary(email)
127
+	assert.Error(t, err)
128
+	assert.Equal(t, ErrEmailNotExist.Error(), err.Error())
129
+
130
+	email = &EmailAddress{
131
+		Email: "user11@example.com",
132
+	}
133
+	err = MakeEmailPrimary(email)
134
+	assert.Error(t, err)
135
+	assert.Equal(t, ErrEmailNotActivated.Error(), err.Error())
136
+
137
+	email = &EmailAddress{
138
+		Email: "user9999999@example.com",
139
+	}
140
+	err = MakeEmailPrimary(email)
141
+	assert.Error(t, err)
142
+	assert.True(t, IsErrUserNotExist(err))
143
+
144
+	email = &EmailAddress{
145
+		Email: "user101@example.com",
146
+	}
147
+	err = MakeEmailPrimary(email)
148
+	assert.NoError(t, err)
149
+
150
+	user, _ := GetUserByID(int64(10))
151
+	assert.Equal(t, "user101@example.com", user.Email)
152
+}
153
+
154
+func TestActivate(t *testing.T) {
155
+	assert.NoError(t, PrepareTestDatabase())
156
+
157
+	email := &EmailAddress{
158
+		ID:    int64(1),
159
+		UID:   int64(1),
160
+		Email: "user11@example.com",
161
+	}
162
+	assert.NoError(t, email.Activate())
163
+
164
+	emails, _ := GetEmailAddresses(int64(1))
165
+	assert.Len(t, emails, 3)
166
+	assert.True(t, emails[0].IsActivated)
167
+	assert.True(t, emails[2].IsActivated)
168
+	assert.True(t, emails[2].IsPrimary)
169
+}