Skip to content

Commit 1bdef76

Browse files
committed
Add Tests and Rewrite Comment
Signed-off-by: Gary Kim <gary@garykim.dev>
1 parent a21e094 commit 1bdef76

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

models/fixtures/user.yml

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
name: user1
77
full_name: User One
88
email: user1@example.com
9+
email_notifications_preference: enabled
910
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
1011
type: 0 # individual
1112
salt: ZogKvWdyEx
@@ -22,6 +23,7 @@
2223
full_name: " < U<se>r Tw<o > >< "
2324
email: user2@example.com
2425
keep_email_private: true
26+
email_notifications_preference: enabled
2527
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
2628
type: 0 # individual
2729
salt: ZogKvWdyEx
@@ -40,6 +42,7 @@
4042
name: user3
4143
full_name: " <<<< >> >> > >> > >>> >> "
4244
email: user3@example.com
45+
email_notifications_preference: onmention
4346
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
4447
type: 1 # organization
4548
salt: ZogKvWdyEx
@@ -56,6 +59,7 @@
5659
name: user4
5760
full_name: " "
5861
email: user4@example.com
62+
email_notifications_preference: onmention
5963
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
6064
type: 0 # individual
6165
salt: ZogKvWdyEx
@@ -72,6 +76,7 @@
7276
name: user5
7377
full_name: User Five
7478
email: user5@example.com
79+
email_notifications_preference: enabled
7580
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
7681
type: 0 # individual
7782
salt: ZogKvWdyEx
@@ -89,6 +94,7 @@
8994
name: user6
9095
full_name: User Six
9196
email: user6@example.com
97+
email_notifications_preference: enabled
9298
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
9399
type: 1 # organization
94100
salt: ZogKvWdyEx
@@ -105,6 +111,7 @@
105111
name: user7
106112
full_name: User Seven
107113
email: user7@example.com
114+
email_notifications_preference: disabled
108115
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
109116
type: 1 # organization
110117
salt: ZogKvWdyEx
@@ -121,6 +128,7 @@
121128
name: user8
122129
full_name: User Eight
123130
email: user8@example.com
131+
email_notifications_preference: enabled
124132
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
125133
type: 0 # individual
126134
salt: ZogKvWdyEx
@@ -138,6 +146,7 @@
138146
name: user9
139147
full_name: User Nine
140148
email: user9@example.com
149+
email_notifications_preference: onmention
141150
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
142151
type: 0 # individual
143152
salt: ZogKvWdyEx

models/user.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,8 @@ func getUserByName(e Engine, name string) (*User, error) {
12721272
return u, nil
12731273
}
12741274

1275-
// GetUserEmailsByNames returns a list of e-mails corresponds to names.
1275+
// GetUserEmailsByNames returns a list of e-mails corresponds to names of users
1276+
// that have their email notifications set to enabled or onmention.
12761277
func GetUserEmailsByNames(names []string) []string {
12771278
return getUserEmailsByNames(x, names)
12781279
}

models/user_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ func TestGetUserEmailsByNames(t *testing.T) {
7474
// ignore none active user email
7575
assert.Equal(t, []string{"user8@example.com"}, GetUserEmailsByNames([]string{"user8", "user9"}))
7676
assert.Equal(t, []string{"user8@example.com", "user5@example.com"}, GetUserEmailsByNames([]string{"user8", "user5"}))
77+
78+
assert.Equal(t, []string{"user8@example.com"}, GetUserEmailsByNames([]string{"user8", "user7"}))
7779
}
7880

7981
func TestUser_APIFormat(t *testing.T) {
@@ -196,6 +198,37 @@ func TestDeleteUser(t *testing.T) {
196198
test(11)
197199
}
198200

201+
func TestEmailNotificationPreferences(t *testing.T) {
202+
assert.NoError(t, PrepareTestDatabase())
203+
for _, test := range []struct {
204+
expected string
205+
userID int64
206+
}{
207+
{EmailNotificationsEnabled, 1},
208+
{EmailNotificationsEnabled, 2},
209+
{EmailNotificationsOnMention, 3},
210+
{EmailNotificationsOnMention, 4},
211+
{EmailNotificationsEnabled, 5},
212+
{EmailNotificationsEnabled, 6},
213+
{EmailNotificationsDisabled, 7},
214+
{EmailNotificationsEnabled, 8},
215+
{EmailNotificationsOnMention, 9},
216+
} {
217+
user := AssertExistsAndLoadBean(t, &User{ID: test.userID}).(*User)
218+
assert.Equal(t, test.expected, user.EmailNotifications())
219+
220+
// Try all possible settings
221+
user.SetEmailNotifications(EmailNotificationsDisabled)
222+
assert.Equal(t, EmailNotificationsDisabled, user.EmailNotifications())
223+
224+
user.SetEmailNotifications(EmailNotificationsOnMention)
225+
assert.Equal(t, EmailNotificationsOnMention, user.EmailNotifications())
226+
227+
user.SetEmailNotifications(EmailNotificationsDisabled)
228+
assert.Equal(t, EmailNotificationsDisabled, user.EmailNotifications())
229+
}
230+
}
231+
199232
func TestHashPasswordDeterministic(t *testing.T) {
200233
b := make([]byte, 16)
201234
rand.Read(b)

0 commit comments

Comments
 (0)