This repository has been archived on 2025-10-08. You can view files and clone it, but cannot push or open issues or pull requests.
miniauthold/pkg/userstore/users_test.go
kekskurse 66791d32a5
All checks were successful
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/push/playwright Pipeline was successful
ci/woodpecker/push/deplyoment Pipeline was successful
chore: store can write users
2025-03-13 16:02:24 +01:00

61 lines
1.5 KiB
Go

package userstore
import (
"fmt"
"os"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)
func TestWriteUser(t *testing.T) {
tts := []struct {
name string
username string
password string
exptError error
exptErrorString string
}{
{
name: "successfull-insert-first-user",
username: "kekskurse",
password: "kekskurse",
exptError: nil,
},
{
name: "failed-username-alreadey-used",
username: "kekskurse",
password: "kekskurse",
exptError: ErrCantExecuteQuery,
exptErrorString: "cant execute query: constraint failed: UNIQUE constraint failed: users.username (2067)",
},
}
store := getTestStore(t)
initDabase(t, store)
for _, tt := range tts {
t.Run(tt.name, func(t *testing.T) {
err := store.UserWrite(tt.username, tt.password)
if tt.exptError == nil {
assert.Nil(t, err, "should store user without error")
} else {
assert.ErrorIs(t, err, tt.exptError, "should return error")
assert.Equal(t, tt.exptErrorString, err.Error(), "should return right error message")
}
})
}
}
func getTestStore(t *testing.T) Store {
name, err := os.MkdirTemp("", "miniauth-teststore")
assert.Nil(t, err, "[setup] should be abel to create tmp folder")
c := Config{}
c.SQLite.Path = fmt.Sprintf("file://%v/%v", name, uuid.NewString())
store, err := NewStore(c)
assert.Nil(t, err, "[setup] should be abel to create store")
return store
}