Skip to content

Commit

Permalink
Add Tests and Rewrite Comment
Browse files Browse the repository at this point in the history
Signed-off-by: Gary Kim <[email protected]>
  • Loading branch information
gary-kim committed Aug 23, 2019
1 parent a21e094 commit 1bdef76
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
9 changes: 9 additions & 0 deletions models/fixtures/user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
name: user1
full_name: User One
email: [email protected]
email_notifications_preference: enabled
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
type: 0 # individual
salt: ZogKvWdyEx
Expand All @@ -22,6 +23,7 @@
full_name: " < U<se>r Tw<o > >< "
email: [email protected]
keep_email_private: true
email_notifications_preference: enabled
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
type: 0 # individual
salt: ZogKvWdyEx
Expand All @@ -40,6 +42,7 @@
name: user3
full_name: " <<<< >> >> > >> > >>> >> "
email: [email protected]
email_notifications_preference: onmention
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
type: 1 # organization
salt: ZogKvWdyEx
Expand All @@ -56,6 +59,7 @@
name: user4
full_name: " "
email: [email protected]
email_notifications_preference: onmention
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
type: 0 # individual
salt: ZogKvWdyEx
Expand All @@ -72,6 +76,7 @@
name: user5
full_name: User Five
email: [email protected]
email_notifications_preference: enabled
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
type: 0 # individual
salt: ZogKvWdyEx
Expand All @@ -89,6 +94,7 @@
name: user6
full_name: User Six
email: [email protected]
email_notifications_preference: enabled
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
type: 1 # organization
salt: ZogKvWdyEx
Expand All @@ -105,6 +111,7 @@
name: user7
full_name: User Seven
email: [email protected]
email_notifications_preference: disabled
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
type: 1 # organization
salt: ZogKvWdyEx
Expand All @@ -121,6 +128,7 @@
name: user8
full_name: User Eight
email: [email protected]
email_notifications_preference: enabled
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
type: 0 # individual
salt: ZogKvWdyEx
Expand All @@ -138,6 +146,7 @@
name: user9
full_name: User Nine
email: [email protected]
email_notifications_preference: onmention
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
type: 0 # individual
salt: ZogKvWdyEx
Expand Down
3 changes: 2 additions & 1 deletion models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,8 @@ func getUserByName(e Engine, name string) (*User, error) {
return u, nil
}

// GetUserEmailsByNames returns a list of e-mails corresponds to names.
// GetUserEmailsByNames returns a list of e-mails corresponds to names of users
// that have their email notifications set to enabled or onmention.
func GetUserEmailsByNames(names []string) []string {
return getUserEmailsByNames(x, names)
}
Expand Down
33 changes: 33 additions & 0 deletions models/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ func TestGetUserEmailsByNames(t *testing.T) {
// ignore none active user email
assert.Equal(t, []string{"[email protected]"}, GetUserEmailsByNames([]string{"user8", "user9"}))
assert.Equal(t, []string{"[email protected]", "[email protected]"}, GetUserEmailsByNames([]string{"user8", "user5"}))

assert.Equal(t, []string{"[email protected]"}, GetUserEmailsByNames([]string{"user8", "user7"}))
}

func TestUser_APIFormat(t *testing.T) {
Expand Down Expand Up @@ -196,6 +198,37 @@ func TestDeleteUser(t *testing.T) {
test(11)
}

func TestEmailNotificationPreferences(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
for _, test := range []struct {
expected string
userID int64
}{
{EmailNotificationsEnabled, 1},
{EmailNotificationsEnabled, 2},
{EmailNotificationsOnMention, 3},
{EmailNotificationsOnMention, 4},
{EmailNotificationsEnabled, 5},
{EmailNotificationsEnabled, 6},
{EmailNotificationsDisabled, 7},
{EmailNotificationsEnabled, 8},
{EmailNotificationsOnMention, 9},
} {
user := AssertExistsAndLoadBean(t, &User{ID: test.userID}).(*User)
assert.Equal(t, test.expected, user.EmailNotifications())

// Try all possible settings
user.SetEmailNotifications(EmailNotificationsDisabled)
assert.Equal(t, EmailNotificationsDisabled, user.EmailNotifications())

user.SetEmailNotifications(EmailNotificationsOnMention)
assert.Equal(t, EmailNotificationsOnMention, user.EmailNotifications())

user.SetEmailNotifications(EmailNotificationsDisabled)
assert.Equal(t, EmailNotificationsDisabled, user.EmailNotifications())
}
}

func TestHashPasswordDeterministic(t *testing.T) {
b := make([]byte, 16)
rand.Read(b)
Expand Down

0 comments on commit 1bdef76

Please sign in to comment.