Browse Source

fmt

master
konrad 1 year ago
parent
commit
e99aeb67c6
Signed by: kolaente <konrad@kola-entertainments.de> GPG Key ID: F40E70337AB24C9B
6 changed files with 20 additions and 21 deletions
  1. 4
    4
      models/author_test.go
  2. 2
    2
      models/error.go
  3. 1
    0
      models/unit_tests.go
  4. 1
    1
      models/user.go
  5. 1
    2
      models/user_add_update.go
  6. 11
    12
      models/user_test.go

+ 4
- 4
models/author_test.go View File

@@ -85,11 +85,11 @@ func TestAddOrUpdateAuthor(t *testing.T) {
x.DropTables(Author{})

// Test inserting
_, err = AddOrUpdateAuthor(Author{Forename:"ff", Lastname: "fff"})
_, err = AddOrUpdateAuthor(Author{Forename: "ff", Lastname: "fff"})
assert.Error(t, err)

// Test updating
_, err = AddOrUpdateAuthor(Author{ID: 3, Forename:"ff", Lastname: "fff"})
_, err = AddOrUpdateAuthor(Author{ID: 3, Forename: "ff", Lastname: "fff"})
assert.Error(t, err)

// Delete from some nonexistent
@@ -110,9 +110,9 @@ func TestGetAuthorsByBook(t *testing.T) {

// Drop the table to see it fail
x.DropTables(AuthorBook{})
_, err := GetAuthorsByBook(Book{ID:1})
_, err := GetAuthorsByBook(Book{ID: 1})
assert.Error(t, err)

//Aaaaaaaaaand recreate it
x.Sync(AuthorBook{})
}
}

+ 2
- 2
models/error.go View File

@@ -24,8 +24,8 @@ func (err ErrUsernameExists) Error() string {

// ErrUserEmailExists represents a "UserEmailExists" kind of error.
type ErrUserEmailExists struct {
UserID int64
Email string
UserID int64
Email string
}

// IsErrUserEmailExists checks if an error is a ErrUserEmailExists.

+ 1
- 0
models/unit_tests.go View File

@@ -25,6 +25,7 @@ func MainTest(m *testing.M, pathToRoot string) {
func createTestEngine(fixturesDir string) error {
var err error
x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
//x, err = xorm.NewEngine("sqlite3", "db.db")
if err != nil {
return err
}

+ 1
- 1
models/user.go View File

@@ -60,7 +60,7 @@ func GetUser(user User) (userOut User, exists bool, err error) {
func CheckUserCredentials(u *UserLogin) (User, error) {

// Check if the user exists
user, exists, err := GetUser(User{Username:u.Username})
user, exists, err := GetUser(User{Username: u.Username})
if err != nil {
return User{}, err
}

+ 1
- 2
models/user_add_update.go View File

@@ -24,7 +24,7 @@ func CreateUser(user User) (newUser User, err error) {
}

// Check if the user already existst with that username
existingUser, exists, err = GetUser(User{Email:newUser.Email})
existingUser, exists, err = GetUser(User{Email: newUser.Email})
if err != nil {
return User{}, err
}
@@ -59,7 +59,6 @@ func hashPassword(password string) (string, error) {
// UpdateUser updates a user
func UpdateUser(user User) (updatedUser User, err error) {


// Check if it exists
theUser, exists, err := GetUserByID(user.ID)
if err != nil {

+ 11
- 12
models/user_test.go View File

@@ -19,14 +19,13 @@ func TestCreateUser(t *testing.T) {
}

// Delete every preexisting user to have a fresh start
/* allusers, err := ListUsers("")
for _, user := range allusers {
// Delete it
assert.Equal(t, dummyuser.Name, user.Name)
assert.Equal(t, dummyuser.Username, user.Username)
assert.Equal(t, dummyuser.Email, user.Email)
assert.Equal(t, dummyuser.IsAdmin, user.IsAdmin)
}*/
allusers, err := ListUsers("")
assert.NoError(t, err)
for _, user := range allusers {
// Delete it
err := DeleteUserByID(user.ID)
assert.NoError(t, err)
}

// Create a new user
createdUser, err := CreateUser(dummyuser)
@@ -37,12 +36,12 @@ func TestCreateUser(t *testing.T) {
assert.Error(t, err)

// Check if it fails to create a user with just the same username
_, err = CreateUser(User{Username:dummyuser.Username, Password: "1234"})
_, err = CreateUser(User{Username: dummyuser.Username, Password: "fsdf"})
assert.Error(t, err)
assert.True(t, IsErrUsernameExists(err))

// Check if it fails to create one with the same email
_, err = CreateUser(User{Username: "noone", Password: "1234", Email:dummyuser.Email})
_, err = CreateUser(User{Username: "noone", Password: "1234", Email: dummyuser.Email})
assert.Error(t, err)
assert.True(t, IsErrUserEmailExists(err))

@@ -86,14 +85,14 @@ func TestCreateUser(t *testing.T) {

// Update the user
newname := "Test_te"
uuser, err := UpdateUser(User{ID:theuser.ID, Name: newname, Password: "444444"})
uuser, err := UpdateUser(User{ID: theuser.ID, Name: newname, Password: "444444"})
assert.NoError(t, err)
assert.Equal(t, newname, uuser.Name)
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
_, err = UpdateUser(User{ID:99999, Username: "dg"})
_, err = UpdateUser(User{ID: 99999, Username: "dg"})
assert.Error(t, err)
assert.True(t, IsErrUserDoesNotExist(err))


Loading…
Cancel
Save