2020-12-29 02:04:20 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 20:19:13 +01:00
|
|
|
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
|
2020-01-26 18:08:06 +01:00
|
|
|
//
|
2020-12-29 02:04:20 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2020-01-26 18:08:06 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2020-12-29 02:04:20 +01:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
2020-01-26 18:08:06 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2020-01-26 18:08:06 +01:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2020-12-29 02:04:20 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-01-26 18:08:06 +01:00
|
|
|
|
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
2020-10-11 22:10:03 +02:00
|
|
|
"testing"
|
|
|
|
|
2020-01-26 18:08:06 +01:00
|
|
|
"code.vikunja.io/api/pkg/db"
|
2022-08-02 15:07:08 +02:00
|
|
|
|
2020-01-26 18:08:06 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-08-02 15:07:08 +02:00
|
|
|
"xorm.io/builder"
|
2020-01-26 18:08:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateUser(t *testing.T) {
|
|
|
|
// Our dummy user for testing
|
|
|
|
dummyuser := &User{
|
|
|
|
Username: "testuser",
|
|
|
|
Password: "1234",
|
|
|
|
Email: "noone@example.com",
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
createdUser, err := CreateUser(s, dummyuser)
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotZero(t, createdUser.Created)
|
|
|
|
})
|
|
|
|
t.Run("already existing", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
_, err := CreateUser(s, &User{
|
2020-01-26 18:08:06 +01:00
|
|
|
Username: "user1",
|
|
|
|
Password: "12345",
|
|
|
|
Email: "email@example.com",
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrUsernameExists(err))
|
|
|
|
})
|
|
|
|
t.Run("same email", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
_, err := CreateUser(s, &User{
|
2020-01-26 18:08:06 +01:00
|
|
|
Username: "testuser",
|
|
|
|
Password: "12345",
|
|
|
|
Email: "user1@example.com",
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrUserEmailExists(err))
|
|
|
|
})
|
|
|
|
t.Run("no username", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
_, err := CreateUser(s, &User{
|
2020-01-26 18:08:06 +01:00
|
|
|
Username: "",
|
|
|
|
Password: "12345",
|
|
|
|
Email: "user1@example.com",
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrNoUsernamePassword(err))
|
|
|
|
})
|
|
|
|
t.Run("no password", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
_, err := CreateUser(s, &User{
|
2020-01-26 18:08:06 +01:00
|
|
|
Username: "testuser",
|
|
|
|
Password: "",
|
|
|
|
Email: "user1@example.com",
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrNoUsernamePassword(err))
|
|
|
|
})
|
|
|
|
t.Run("no email", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
_, err := CreateUser(s, &User{
|
2020-01-26 18:08:06 +01:00
|
|
|
Username: "testuser",
|
|
|
|
Password: "12345",
|
|
|
|
Email: "",
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrNoUsernamePassword(err))
|
|
|
|
})
|
2020-11-21 17:38:58 +01:00
|
|
|
t.Run("same email but different issuer", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
_, err := CreateUser(s, &User{
|
2020-11-21 17:38:58 +01:00
|
|
|
Username: "somenewuser",
|
|
|
|
Email: "user1@example.com",
|
|
|
|
Issuer: "https://some.site",
|
|
|
|
Subject: "12345",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
t.Run("same subject but different issuer", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
_, err := CreateUser(s, &User{
|
2020-11-21 17:38:58 +01:00
|
|
|
Username: "somenewuser",
|
|
|
|
Email: "somenewuser@example.com",
|
|
|
|
Issuer: "https://some.site",
|
|
|
|
Subject: "12345",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
2020-01-26 18:08:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUser(t *testing.T) {
|
|
|
|
t.Run("by name", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
theuser, err := getUser(
|
|
|
|
s,
|
|
|
|
&User{
|
|
|
|
Username: "user1",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
)
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, theuser.ID, int64(1))
|
|
|
|
assert.Empty(t, theuser.Email)
|
|
|
|
})
|
|
|
|
t.Run("by email", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
theuser, err := getUser(
|
|
|
|
s,
|
|
|
|
&User{
|
|
|
|
Email: "user1@example.com",
|
|
|
|
},
|
|
|
|
false)
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, theuser.ID, int64(1))
|
|
|
|
assert.Empty(t, theuser.Email)
|
|
|
|
})
|
|
|
|
t.Run("by id", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
theuser, err := GetUserByID(s, 1)
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, theuser.ID, int64(1))
|
|
|
|
assert.Equal(t, theuser.Username, "user1")
|
|
|
|
assert.Empty(t, theuser.Email)
|
|
|
|
})
|
|
|
|
t.Run("invalid id", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
_, err := GetUserByID(s, 99999)
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrUserDoesNotExist(err))
|
|
|
|
})
|
|
|
|
t.Run("nonexistant", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
_, err := GetUserByID(s, 0)
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrUserDoesNotExist(err))
|
|
|
|
})
|
|
|
|
t.Run("empty name", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
_, err := GetUserByUsername(s, "")
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrUserDoesNotExist(err))
|
|
|
|
})
|
|
|
|
t.Run("with email", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
theuser, err := GetUserWithEmail(s, &User{ID: 1})
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, theuser.ID, int64(1))
|
|
|
|
assert.Equal(t, theuser.Username, "user1")
|
|
|
|
assert.NotEmpty(t, theuser.Email)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckUserCredentials(t *testing.T) {
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
_, err := CheckUserCredentials(s, &Login{Username: "user1", Password: "1234"})
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
t.Run("unverified email", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
_, err := CheckUserCredentials(s, &Login{Username: "user5", Password: "1234"})
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrEmailNotConfirmed(err))
|
|
|
|
})
|
|
|
|
t.Run("wrong password", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
_, err := CheckUserCredentials(s, &Login{Username: "user1", Password: "12345"})
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrWrongUsernameOrPassword(err))
|
|
|
|
})
|
|
|
|
t.Run("nonexistant user", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
_, err := CheckUserCredentials(s, &Login{Username: "dfstestuu", Password: "1234"})
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrWrongUsernameOrPassword(err))
|
|
|
|
})
|
|
|
|
t.Run("empty password", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
_, err := CheckUserCredentials(s, &Login{Username: "user1"})
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrNoUsernamePassword(err))
|
|
|
|
})
|
|
|
|
t.Run("empty username", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
_, err := CheckUserCredentials(s, &Login{Password: "1234"})
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrNoUsernamePassword(err))
|
|
|
|
})
|
2020-12-18 14:54:49 +01:00
|
|
|
t.Run("email", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
_, err := CheckUserCredentials(s, &Login{Username: "user1@example.com", Password: "1234"})
|
2020-12-18 14:54:49 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
2020-01-26 18:08:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateUser(t *testing.T) {
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
uuser, err := UpdateUser(s, &User{
|
2020-01-26 18:08:06 +01:00
|
|
|
ID: 1,
|
|
|
|
Password: "LoremIpsum",
|
|
|
|
Email: "testing@example.com",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.", uuser.Password) // Password should not change
|
|
|
|
assert.Equal(t, "user1", uuser.Username) // Username should not change either
|
|
|
|
})
|
|
|
|
t.Run("change username", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
uuser, err := UpdateUser(s, &User{
|
2020-01-26 18:08:06 +01:00
|
|
|
ID: 1,
|
|
|
|
Username: "changedname",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.", uuser.Password) // Password should not change
|
|
|
|
assert.Equal(t, "changedname", uuser.Username)
|
|
|
|
})
|
|
|
|
t.Run("nonexistant", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
_, err := UpdateUser(s, &User{
|
2020-01-26 18:08:06 +01:00
|
|
|
ID: 99999,
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrUserDoesNotExist(err))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateUserPassword(t *testing.T) {
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
err := UpdateUserPassword(s, &User{
|
2020-01-26 18:08:06 +01:00
|
|
|
ID: 1,
|
2020-12-23 16:32:28 +01:00
|
|
|
}, "12345")
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
t.Run("nonexistant user", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
err := UpdateUserPassword(s, &User{
|
2020-01-26 18:08:06 +01:00
|
|
|
ID: 9999,
|
|
|
|
}, "12345")
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrUserDoesNotExist(err))
|
|
|
|
})
|
|
|
|
t.Run("empty password", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
err := UpdateUserPassword(s, &User{
|
2020-01-26 18:08:06 +01:00
|
|
|
ID: 1,
|
2020-12-23 16:32:28 +01:00
|
|
|
}, "")
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrEmptyNewPassword(err))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListUsers(t *testing.T) {
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2022-08-02 13:26:42 +02:00
|
|
|
all, err := ListUsers(s, "user1", nil)
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, len(all) > 0)
|
|
|
|
assert.Equal(t, all[0].Username, "user1")
|
|
|
|
})
|
|
|
|
t.Run("all users", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2021-04-07 18:28:58 +02:00
|
|
|
all, err := ListAllUsers(s)
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.NoError(t, err)
|
2020-11-21 17:38:58 +01:00
|
|
|
assert.Len(t, all, 14)
|
2020-01-26 18:08:06 +01:00
|
|
|
})
|
2021-04-07 18:28:58 +02:00
|
|
|
t.Run("no search term", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2022-08-02 13:26:42 +02:00
|
|
|
all, err := ListUsers(s, "", nil)
|
2021-04-07 18:28:58 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, all, 0)
|
|
|
|
})
|
|
|
|
t.Run("not discoverable by email", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2022-08-02 13:26:42 +02:00
|
|
|
all, err := ListUsers(s, "user1@example.com", nil)
|
2021-04-07 18:28:58 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, all, 0)
|
|
|
|
db.AssertExists(t, "users", map[string]interface{}{
|
2022-08-02 13:26:42 +02:00
|
|
|
"email": "user1@example.com",
|
|
|
|
"discoverable_by_email": false,
|
2021-04-07 18:28:58 +02:00
|
|
|
}, false)
|
|
|
|
})
|
|
|
|
t.Run("not discoverable by name", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2022-08-02 13:26:42 +02:00
|
|
|
all, err := ListUsers(s, "one else", nil)
|
2021-04-07 18:28:58 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, all, 0)
|
|
|
|
db.AssertExists(t, "users", map[string]interface{}{
|
2022-08-02 13:26:42 +02:00
|
|
|
"name": "Some one else",
|
|
|
|
"discoverable_by_name": false,
|
2021-04-07 18:28:58 +02:00
|
|
|
}, false)
|
|
|
|
})
|
|
|
|
t.Run("discoverable by email", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2022-08-02 13:26:42 +02:00
|
|
|
all, err := ListUsers(s, "user7@example.com", nil)
|
2021-04-07 18:28:58 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, all, 1)
|
|
|
|
assert.Equal(t, int64(7), all[0].ID)
|
2022-08-02 13:26:42 +02:00
|
|
|
db.AssertExists(t, "users", map[string]interface{}{
|
|
|
|
"email": "user7@example.com",
|
|
|
|
"discoverable_by_email": true,
|
|
|
|
}, false)
|
2021-04-07 18:28:58 +02:00
|
|
|
})
|
|
|
|
t.Run("discoverable by partial name", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2022-08-02 13:26:42 +02:00
|
|
|
all, err := ListUsers(s, "with space", nil)
|
2021-04-07 18:28:58 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, all, 1)
|
|
|
|
assert.Equal(t, int64(12), all[0].ID)
|
2022-08-02 13:26:42 +02:00
|
|
|
db.AssertExists(t, "users", map[string]interface{}{
|
|
|
|
"name": "Name with spaces",
|
|
|
|
"discoverable_by_name": true,
|
|
|
|
}, false)
|
|
|
|
})
|
|
|
|
t.Run("discoverable by email with extra condition", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2022-08-02 15:02:00 +02:00
|
|
|
all, err := ListUsers(s, "user7@example.com", &ListUserOpts{AdditionalCond: builder.In("id", 7)})
|
2022-08-02 13:26:42 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, all, 1)
|
|
|
|
assert.Equal(t, int64(7), all[0].ID)
|
|
|
|
db.AssertExists(t, "users", map[string]interface{}{
|
|
|
|
"email": "user7@example.com",
|
|
|
|
"discoverable_by_email": true,
|
|
|
|
}, false)
|
2021-04-07 18:28:58 +02:00
|
|
|
})
|
2020-01-26 18:08:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserPasswordReset(t *testing.T) {
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-01-26 18:08:06 +01:00
|
|
|
reset := &PasswordReset{
|
|
|
|
Token: "passwordresettesttoken",
|
|
|
|
NewPassword: "12345",
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := ResetPassword(s, reset)
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
t.Run("without password", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-01-26 18:08:06 +01:00
|
|
|
reset := &PasswordReset{
|
|
|
|
Token: "passwordresettesttoken",
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := ResetPassword(s, reset)
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrNoUsernamePassword(err))
|
|
|
|
})
|
|
|
|
t.Run("empty token", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-01-26 18:08:06 +01:00
|
|
|
reset := &PasswordReset{
|
2020-12-30 21:43:14 +01:00
|
|
|
Token: "",
|
2020-01-26 18:08:06 +01:00
|
|
|
NewPassword: "12345",
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := ResetPassword(s, reset)
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.Error(t, err)
|
2020-12-30 21:43:14 +01:00
|
|
|
assert.True(t, IsErrNoPasswordResetToken(err))
|
2020-01-26 18:08:06 +01:00
|
|
|
})
|
|
|
|
t.Run("wrong token", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-01-26 18:08:06 +01:00
|
|
|
reset := &PasswordReset{
|
|
|
|
Token: "somethingsomething",
|
|
|
|
NewPassword: "12345",
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := ResetPassword(s, reset)
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrInvalidPasswordResetToken(err))
|
|
|
|
})
|
|
|
|
}
|