90 lines
1.9 KiB
Go
90 lines
1.9 KiB
Go
|
package server_test
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"testing"
|
||
|
|
||
|
"git.t-juice.club/torjus/ministream/server"
|
||
|
)
|
||
|
|
||
|
func TestPassword(t *testing.T) {
|
||
|
t.Run("SetAndVerify", func(t *testing.T) {
|
||
|
user := &server.User{}
|
||
|
password := "i L0ve K1tt3ns"
|
||
|
if err := user.SetPassword(password); err != nil {
|
||
|
t.Fatalf("Error setting password: %s", err)
|
||
|
}
|
||
|
|
||
|
if err := user.VerifyPassword(password); err != nil {
|
||
|
t.Fatalf("Error verifying with correct password: %s", err)
|
||
|
}
|
||
|
|
||
|
if err := user.VerifyPassword("wrong"); err == nil {
|
||
|
t.Errorf("Verifying with wrong password did not return error")
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func TestUserStore(t *testing.T) {
|
||
|
t.Run("SaveLoad", func(t *testing.T) {
|
||
|
data := []struct {
|
||
|
Username string
|
||
|
Passord string
|
||
|
Admin bool
|
||
|
}{
|
||
|
{
|
||
|
Username: "admin",
|
||
|
Passord: "adminpw",
|
||
|
Admin: true,
|
||
|
},
|
||
|
{
|
||
|
Username: "dave",
|
||
|
Passord: "dave",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
users := make(map[string]*server.User)
|
||
|
for _, item := range data {
|
||
|
u := &server.User{Username: item.Username, IsAdmin: item.Admin}
|
||
|
err := u.SetPassword(item.Passord)
|
||
|
if err != nil {
|
||
|
t.Fatalf("Error setting password: %s", err)
|
||
|
}
|
||
|
users[item.Username] = u
|
||
|
}
|
||
|
|
||
|
s := server.NewUserStore()
|
||
|
for _, u := range users {
|
||
|
if err := s.Put(u); err != nil {
|
||
|
t.Fatalf("Error storing user: %s", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Write to buffer
|
||
|
var buf bytes.Buffer
|
||
|
if err := s.ToWriter(&buf); err != nil {
|
||
|
t.Fatalf("Error writing store to buffer: %s", err)
|
||
|
}
|
||
|
|
||
|
loaded, err := server.StoreFromReader(&buf)
|
||
|
if err != nil {
|
||
|
t.Fatalf("Error loading store: %s", err)
|
||
|
}
|
||
|
|
||
|
for _, item := range data {
|
||
|
u, err := loaded.Get(item.Username)
|
||
|
if err != nil {
|
||
|
t.Fatalf("Error getting user after load: %s", err)
|
||
|
}
|
||
|
|
||
|
if u.IsAdmin != item.Admin {
|
||
|
t.Fatalf("IsAdmin value changed.")
|
||
|
}
|
||
|
|
||
|
if err := u.VerifyPassword(item.Passord); err != nil {
|
||
|
t.Fatalf("Verifying password after load failed: %s", err)
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
}
|