From b6da6585535209f3916eeab245d793f30e8bd650 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 4 Feb 2017 09:20:56 +0800 Subject: [PATCH] test: Add user mail testing. (#833) --- models/fixtures/email_address.yml | 35 +++++++ models/fixtures/user.yml | 16 +++ models/user_mail_test.go | 169 ++++++++++++++++++++++++++++++ 3 files changed, 220 insertions(+) create mode 100644 models/fixtures/email_address.yml create mode 100644 models/user_mail_test.go diff --git a/models/fixtures/email_address.yml b/models/fixtures/email_address.yml new file mode 100644 index 000000000..c37d9a787 --- /dev/null +++ b/models/fixtures/email_address.yml @@ -0,0 +1,35 @@ +- + id: 1 + uid: 1 + email: user11@example.com + is_activated: false + +- + id: 2 + uid: 1 + email: user12@example.com + is_activated: false + +- + id: 3 + uid: 2 + email: user2@example.com + is_activated: true + +- + id: 4 + uid: 2 + email: user21@example.com + is_activated: false + +- + id: 5 + uid: 9999999 + email: user9999999@example.com + is_activated: true + +- + id: 6 + uid: 10 + email: user101@example.com + is_activated: true diff --git a/models/fixtures/user.yml b/models/fixtures/user.yml index e8f13b93b..1bca6049f 100644 --- a/models/fixtures/user.yml +++ b/models/fixtures/user.yml @@ -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: user10@example.com + passwd: password + type: 0 # user + salt: salt + is_admin: false + avatar: avatar10 + avatar_email: user10@example.com + num_repos: 0 + num_members: 1 + is_active: true diff --git a/models/user_mail_test.go b/models/user_mail_test.go new file mode 100644 index 000000000..4c0b6804d --- /dev/null +++ b/models/user_mail_test.go @@ -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("user11@example.com") + assert.True(t, isExist) + isExist, _ = IsEmailUsed("user1234567890@example.com") + assert.False(t, isExist) +} + +func TestAddEmailAddress(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + + assert.NoError(t, AddEmailAddress(&EmailAddress{ + Email: "user1234567890@example.com", + IsPrimary: true, + IsActivated: true, + })) + + // ErrEmailAlreadyUsed + err := AddEmailAddress(&EmailAddress{ + Email: "user1234567890@example.com", + }) + 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: "user1234@example.com", + IsActivated: true, + } + emails[1] = &EmailAddress{ + Email: "user5678@example.com", + IsActivated: true, + } + assert.NoError(t, AddEmailAddresses(emails)) + + // 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: "user11@example.com", + })) + + assert.NoError(t, DeleteEmailAddress(&EmailAddress{ + UID: int64(1), + Email: "user12@example.com", + })) + + // Email address does not exist + err := DeleteEmailAddress(&EmailAddress{ + UID: int64(1), + Email: "user1234567890@example.com", + }) + 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: "user2@example.com", + } + emails[1] = &EmailAddress{ + UID: int64(2), + Email: "user21@example.com", + } + assert.NoError(t, DeleteEmailAddresses(emails)) + + // ErrEmailAlreadyUsed + err := DeleteEmailAddresses(emails) + assert.Error(t, err) +} + +func TestMakeEmailPrimary(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + + email := &EmailAddress{ + Email: "user567890@example.com", + } + err := MakeEmailPrimary(email) + assert.Error(t, err) + assert.Equal(t, ErrEmailNotExist.Error(), err.Error()) + + email = &EmailAddress{ + Email: "user11@example.com", + } + err = MakeEmailPrimary(email) + assert.Error(t, err) + assert.Equal(t, ErrEmailNotActivated.Error(), err.Error()) + + email = &EmailAddress{ + Email: "user9999999@example.com", + } + err = MakeEmailPrimary(email) + assert.Error(t, err) + assert.True(t, IsErrUserNotExist(err)) + + email = &EmailAddress{ + Email: "user101@example.com", + } + err = MakeEmailPrimary(email) + assert.NoError(t, err) + + user, _ := GetUserByID(int64(10)) + assert.Equal(t, "user101@example.com", user.Email) +} + +func TestActivate(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + + email := &EmailAddress{ + ID: int64(1), + UID: int64(1), + Email: "user11@example.com", + } + 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) +}