Skip to content

test: Add user mail testing. #833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions models/fixtures/email_address.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
-
id: 1
uid: 1
email: [email protected]
is_activated: false

-
id: 2
uid: 1
email: [email protected]
is_activated: false

-
id: 3
uid: 2
email: [email protected]
is_activated: true

-
id: 4
uid: 2
email: [email protected]
is_activated: false

-
id: 5
uid: 9999999
email: [email protected]
is_activated: true

-
id: 6
uid: 10
email: [email protected]
is_activated: true
16 changes: 16 additions & 0 deletions models/fixtures/user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,19 @@
num_repos: 0
num_members: 1
is_active: false

-
id: 10
lower_name: user10
name: user10
full_name: User Ten
email: [email protected]
passwd: password
type: 0 # user
salt: salt
is_admin: false
avatar: avatar10
avatar_email: [email protected]
num_repos: 0
num_members: 1
is_active: true
169 changes: 169 additions & 0 deletions models/user_mail_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package models

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetEmailAddresses(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

emails, _ := GetEmailAddresses(int64(1))
assert.Len(t, emails, 3)
assert.False(t, emails[0].IsPrimary)
assert.True(t, emails[2].IsActivated)
assert.True(t, emails[2].IsPrimary)

emails, _ = GetEmailAddresses(int64(2))
assert.Len(t, emails, 2)
assert.True(t, emails[0].IsPrimary)
assert.True(t, emails[0].IsActivated)
}

func TestIsEmailUsed(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

isExist, _ := IsEmailUsed("")
assert.True(t, isExist)
isExist, _ = IsEmailUsed("[email protected]")
assert.True(t, isExist)
isExist, _ = IsEmailUsed("[email protected]")
assert.False(t, isExist)
}

func TestAddEmailAddress(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

assert.NoError(t, AddEmailAddress(&EmailAddress{
Email: "[email protected]",
IsPrimary: true,
IsActivated: true,
}))

// ErrEmailAlreadyUsed
err := AddEmailAddress(&EmailAddress{
Email: "[email protected]",
})
assert.Error(t, err)
assert.True(t, IsErrEmailAlreadyUsed(err))
}

func TestAddEmailAddresses(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

// insert multiple email address
emails := make([]*EmailAddress, 2)
emails[0] = &EmailAddress{
Email: "[email protected]",
IsActivated: true,
}
emails[1] = &EmailAddress{
Email: "[email protected]",
IsActivated: true,
}
assert.NoError(t, AddEmailAddresses(emails))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not to be nitpicky, but this function tests both AddEmailAddress(..) and AddEmailAddresses(..). Could we split it into two tests: TestAddEmailAddress and TestAddEmailAddresses?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// ErrEmailAlreadyUsed
err := AddEmailAddresses(emails)
assert.Error(t, err)
assert.True(t, IsErrEmailAlreadyUsed(err))
}

func TestDeleteEmailAddress(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

assert.NoError(t, DeleteEmailAddress(&EmailAddress{
UID: int64(1),
ID: int64(1),
Email: "[email protected]",
}))

assert.NoError(t, DeleteEmailAddress(&EmailAddress{
UID: int64(1),
Email: "[email protected]",
}))

// Email address does not exist
err := DeleteEmailAddress(&EmailAddress{
UID: int64(1),
Email: "[email protected]",
})
assert.Error(t, err)
}

func TestDeleteEmailAddresses(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

// delete multiple email address
emails := make([]*EmailAddress, 2)
emails[0] = &EmailAddress{
UID: int64(2),
ID: int64(3),
Email: "[email protected]",
}
emails[1] = &EmailAddress{
UID: int64(2),
Email: "[email protected]",
}
assert.NoError(t, DeleteEmailAddresses(emails))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// ErrEmailAlreadyUsed
err := DeleteEmailAddresses(emails)
assert.Error(t, err)
}

func TestMakeEmailPrimary(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

email := &EmailAddress{
Email: "[email protected]",
}
err := MakeEmailPrimary(email)
assert.Error(t, err)
assert.Equal(t, ErrEmailNotExist.Error(), err.Error())

email = &EmailAddress{
Email: "[email protected]",
}
err = MakeEmailPrimary(email)
assert.Error(t, err)
assert.Equal(t, ErrEmailNotActivated.Error(), err.Error())

email = &EmailAddress{
Email: "[email protected]",
}
err = MakeEmailPrimary(email)
assert.Error(t, err)
assert.True(t, IsErrUserNotExist(err))

email = &EmailAddress{
Email: "[email protected]",
}
err = MakeEmailPrimary(email)
assert.NoError(t, err)

user, _ := GetUserByID(int64(10))
assert.Equal(t, "[email protected]", user.Email)
}

func TestActivate(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

email := &EmailAddress{
ID: int64(1),
UID: int64(1),
Email: "[email protected]",
}
assert.NoError(t, email.Activate())

emails, _ := GetEmailAddresses(int64(1))
assert.Len(t, emails, 3)
assert.True(t, emails[0].IsActivated)
assert.True(t, emails[2].IsActivated)
assert.True(t, emails[2].IsPrimary)
}