package models

import (
	"github.com/stretchr/testify/assert"
	"testing"
)

func TestCreateUser(t *testing.T) {
	// Create test database
	assert.NoError(t, PrepareTestDatabase())

	// Our dummy user for testing
	dummyuser := User{
		Name:     "testu",
		Username: "testuu",
		Password: "1234",
		Email:    "noone@example.com",
		IsAdmin:  true,
	}

	// 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)
		}*/

	// Create a new user
	createdUser, err := CreateUser(dummyuser)
	assert.NoError(t, err)

	// Check if it fails to create the same user again
	createdUser, err = CreateUser(dummyuser)
	assert.Error(t, err)

	// Check if it fails to create a user with just the same username
	createdUser, err = CreateUser(User{Username: "testuu"})
	assert.Error(t, err)

	// Check if it fails to create a user without password and username
	createdUser, err = CreateUser(User{})
	assert.Error(t, err)

	createdUser, err = CreateUser(User{Name: "blub"})
	assert.Error(t, err)

	// Check if he exists
	_, exists, err := GetUser(createdUser)
	assert.NoError(t, err)
	assert.True(t, exists)

	// Check the user credentials
	user, err := CheckUserCredentials(&UserLogin{"testuu", "1234"})
	assert.NoError(t, err)
	assert.Equal(t, dummyuser.Name, user.Name)

	// List one user

	// Delete it
}
