vikunja-api/models/user_test.go

156 lines
4.2 KiB
Go
Raw Normal View History

2018-06-10 11:11:41 +02:00
package models
import (
2018-10-27 11:33:28 +02:00
"code.vikunja.io/api/models/utils"
2018-06-10 11:11:41 +02:00
"github.com/stretchr/testify/assert"
"testing"
)
func TestCreateUser(t *testing.T) {
// Create test database
2018-07-27 15:52:50 +02:00
//assert.NoError(t, PrepareTestDatabase())
2018-06-10 11:11:41 +02:00
// Get our doer
2018-08-30 19:14:02 +02:00
doer, err := GetUserByID(1)
2018-06-10 11:11:41 +02:00
assert.NoError(t, err)
// Our dummy user for testing
dummyuser := User{
Username: "testuu",
Password: "1234",
Email: "noone@example.com",
}
// Create a new user
2018-06-10 11:41:10 +02:00
createdUser, err := CreateUser(dummyuser)
2018-06-10 11:11:41 +02:00
assert.NoError(t, err)
// Create a second new user
2018-10-27 11:33:28 +02:00
_, err = CreateUser(User{Username: dummyuser.Username + "2", Email: dummyuser.Email + "m", Password: dummyuser.Password})
2018-06-10 11:11:41 +02:00
assert.NoError(t, err)
// Check if it fails to create the same user again
2018-06-10 11:41:10 +02:00
_, err = CreateUser(dummyuser)
2018-06-10 11:11:41 +02:00
assert.Error(t, err)
// Check if it fails to create a user with just the same username
2018-06-10 11:41:10 +02:00
_, err = CreateUser(User{Username: dummyuser.Username, Password: "fsdf"})
2018-06-10 11:11:41 +02:00
assert.Error(t, err)
assert.True(t, IsErrUsernameExists(err))
// Check if it fails to create one with the same email
2018-06-10 11:41:10 +02:00
_, err = CreateUser(User{Username: "noone", Password: "1234", Email: dummyuser.Email})
2018-06-10 11:11:41 +02:00
assert.Error(t, err)
assert.True(t, IsErrUserEmailExists(err))
// Check if it fails to create a user without password and username
2018-06-10 11:41:10 +02:00
_, err = CreateUser(User{})
2018-06-10 11:11:41 +02:00
assert.Error(t, err)
assert.True(t, IsErrNoUsernamePassword(err))
// Check if he exists
2018-08-30 19:14:02 +02:00
theuser, err := GetUser(createdUser)
2018-06-10 11:11:41 +02:00
assert.NoError(t, err)
// Get by his ID
2018-08-30 19:14:02 +02:00
_, err = GetUserByID(theuser.ID)
2018-06-10 11:11:41 +02:00
assert.NoError(t, err)
// Passing 0 as ID should return an error
2018-08-30 19:14:02 +02:00
_, err = GetUserByID(0)
assert.Error(t, err)
assert.True(t, IsErrUserDoesNotExist(err))
2018-06-10 11:11:41 +02:00
// Check the user credentials
user, err := CheckUserCredentials(&UserLogin{"testuu", "1234"})
assert.NoError(t, err)
2018-06-10 11:41:10 +02:00
assert.Equal(t, "testuu", user.Username)
2018-06-10 11:11:41 +02:00
// Check wrong password (should also fail)
_, err = CheckUserCredentials(&UserLogin{"testuu", "12345"})
assert.Error(t, err)
// Check usercredentials for a nonexistent user (should fail)
_, err = CheckUserCredentials(&UserLogin{"dfstestuu", "1234"})
assert.Error(t, err)
assert.True(t, IsErrUserDoesNotExist(err))
// Update the user
2018-06-10 11:41:10 +02:00
uuser, err := UpdateUser(User{ID: theuser.ID, Password: "444444"})
2018-06-10 11:11:41 +02:00
assert.NoError(t, err)
assert.Equal(t, theuser.Password, uuser.Password) // Password should not change
assert.Equal(t, theuser.Username, uuser.Username) // Username should not change either
// Try updating one which does not exist
2018-06-10 11:41:10 +02:00
_, err = UpdateUser(User{ID: 99999, Username: "dg"})
2018-06-10 11:11:41 +02:00
assert.Error(t, err)
assert.True(t, IsErrUserDoesNotExist(err))
// Update a users password
newpassword := "55555"
err = UpdateUserPassword(&theuser, newpassword)
2018-06-10 11:11:41 +02:00
assert.NoError(t, err)
// Check if it was changed
user, err = CheckUserCredentials(&UserLogin{theuser.Username, newpassword})
assert.NoError(t, err)
// Check if the searchterm works
all, err := ListUsers("test")
assert.NoError(t, err)
assert.True(t, len(all) > 0)
all, err = ListUsers("")
assert.NoError(t, err)
assert.True(t, len(all) > 0)
// Try updating the password of a nonexistent user (should fail)
err = UpdateUserPassword(&User{ID: 9999}, newpassword)
2018-06-10 11:11:41 +02:00
assert.Error(t, err)
assert.True(t, IsErrUserDoesNotExist(err))
// Delete it
err = DeleteUserByID(theuser.ID, &doer)
assert.NoError(t, err)
// Try deleting one with ID = 0
err = DeleteUserByID(0, &doer)
assert.Error(t, err)
assert.True(t, IsErrIDCannotBeZero(err))
2018-10-27 11:33:28 +02:00
}
func TestUserPasswordReset(t *testing.T) {
// Request a new token
tr := &PasswordTokenRequest{
2018-10-28 17:11:13 +01:00
Username: "user1",
2018-10-27 11:33:28 +02:00
}
err := RequestUserPasswordResetToken(tr)
assert.NoError(t, err)
// Get the token / inside the user object
userWithToken, err := GetUserByID(1)
assert.NoError(t, err)
// Try resetting it
reset := &PasswordReset{
UserID: 1,
Token: userWithToken.PasswordResetToken,
}
// Try resetting it without a password
reset.NewPassword = ""
err = UserPasswordReset(reset)
assert.True(t, IsErrNoUsernamePassword(err))
// Reset it
reset.NewPassword = "1234"
err = UserPasswordReset(reset)
assert.NoError(t, err)
2018-06-10 11:11:41 +02:00
2018-10-27 11:33:28 +02:00
// Try resetting it with a wrong token
reset.Token = utils.MakeRandomString(400)
err = UserPasswordReset(reset)
2018-06-10 11:11:41 +02:00
assert.Error(t, err)
2018-10-27 11:33:28 +02:00
assert.True(t, IsErrInvalidPasswordResetToken(err))
2018-06-10 11:11:41 +02:00
}