140 lines
3.4 KiB
Go
140 lines
3.4 KiB
Go
package main
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func createTempFile(t testing.TB, initialDB string) (*os.File, func()) {
|
|
t.Helper()
|
|
|
|
tempfile, err := ioutil.TempFile("", "db")
|
|
|
|
if err != nil {
|
|
t.Fatalf("could not create temp file %v", err)
|
|
}
|
|
|
|
tempfile.Write([]byte(initialDB))
|
|
|
|
removeFile := func() {
|
|
tempfile.Close()
|
|
os.Remove(tempfile.Name())
|
|
}
|
|
|
|
return tempfile, removeFile
|
|
}
|
|
|
|
func TestFileSystemStore(t *testing.T) {
|
|
t.Run("get username", func(t *testing.T) {
|
|
database, cleanDatabase := createTempFile(t, `[
|
|
{"Username": "testUser", "Key": "12345", "IpAddress": "123.123.123.123.50120", "IsAdmin": true},
|
|
{"Username": "jimmy", "Key": "67890", "IpAddress": "12.12.12.12:50121", "IsAdmin": false}]`)
|
|
defer cleanDatabase()
|
|
|
|
store, err := NewFileSystemUserStore(database)
|
|
|
|
//userList := store.userList
|
|
//user := userList.Find("testUser")
|
|
//user.UpdateAdminStatus(true)
|
|
|
|
AssertNoError(t, err)
|
|
|
|
got := store.GetUsers()
|
|
|
|
want := []User{
|
|
{Username: "testUser", Key: "12345", IpAddress: "123.123.123.123.50120", IsAdmin: true},
|
|
{Username: "jimmy", Key: "67890", IpAddress: "12.12.12.12:50121", IsAdmin: false},
|
|
}
|
|
|
|
AssertUsers(t, got, want)
|
|
|
|
//read again
|
|
got = store.GetUsers()
|
|
AssertUsers(t, got, want)
|
|
})
|
|
}
|
|
|
|
func TestCreateUser(t *testing.T) {
|
|
t.Run("get username", func(t *testing.T) {
|
|
database, cleanDatabase := createTempFile(t, `[
|
|
{"Username": "testUser", "Key": "12345", "IpAddress": "123.123.123.123.50120", "IsAdmin": true},
|
|
{"Username": "jimmy", "Key": "67890", "IpAddress": "12.12.12.12:50121", "IsAdmin": false}]`)
|
|
defer cleanDatabase()
|
|
|
|
store, err := NewFileSystemUserStore(database)
|
|
|
|
AssertNoError(t, err)
|
|
|
|
want := User{
|
|
Username: "newUser",
|
|
Key: "12345",
|
|
IpAddress: "1.1.1.1:1111",
|
|
IsAdmin: false,
|
|
}
|
|
|
|
store.AddUserToUserList(want)
|
|
|
|
got := store.GetUsers()
|
|
AssertUserExists(t, got, want)
|
|
})
|
|
}
|
|
|
|
func TestDeleteUser(t *testing.T) {
|
|
t.Run("get username", func(t *testing.T) {
|
|
database, cleanDatabase := createTempFile(t, `[
|
|
{"Username": "testUser", "Key": "12345", "IpAddress": "123.123.123.123.50120", "IsAdmin": true},
|
|
{"Username": "jimmy", "Key": "67890", "IpAddress": "12.12.12.12:50121", "IsAdmin": false}]`)
|
|
defer cleanDatabase()
|
|
|
|
store, err := NewFileSystemUserStore(database)
|
|
|
|
AssertNoError(t, err)
|
|
|
|
want := User{
|
|
Username: "testUser",
|
|
Key: "12345",
|
|
IpAddress: "123.123.123.123:50120",
|
|
IsAdmin: false,
|
|
}
|
|
|
|
want = *store.userList.Find("testUser")
|
|
store.DeleteUserFromUserList(want)
|
|
|
|
got := store.GetUsers()
|
|
AssertUserNotExists(t, got, want)
|
|
|
|
want = *store.userList.Find("jimmy")
|
|
AssertUserExists(t, got, want)
|
|
})
|
|
}
|
|
|
|
//todo move this to user_test.go and user.go
|
|
func TestUpdateUserStore(t *testing.T) {
|
|
t.Run("get username", func(t *testing.T) {
|
|
database, cleanDatabase := createTempFile(t, `[
|
|
{"Username": "testUser", "Key": "12345", "IpAddress": "123.123.123.123.50120", "IsAdmin": true},
|
|
{"Username": "jimmy", "Key": "67890", "IpAddress": "12.12.12.12:50121", "IsAdmin": false}]`)
|
|
defer cleanDatabase()
|
|
|
|
store, err := NewFileSystemUserStore(database)
|
|
|
|
AssertNoError(t, err)
|
|
|
|
userToUpdate := *store.userList.Find("testUser")
|
|
|
|
want := User{
|
|
Username: "testUser2",
|
|
Key: "123456",
|
|
IpAddress: "124.124.124.124:50120",
|
|
IsAdmin: false,
|
|
}
|
|
|
|
want = *store.userList.Find("testUser")
|
|
store.UpdateUserFromUserList(userToUpdate, want)
|
|
|
|
got := store.GetUsers()
|
|
AssertUserExists(t, got, want)
|
|
})
|
|
}
|