173 lines
4.6 KiB
Go
173 lines
4.6 KiB
Go
|
package pfug
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestUserChangePassword(t *testing.T) {
|
||
|
firstPassword := "123456"
|
||
|
secondPassword := "123456789"
|
||
|
|
||
|
user := CreateUser("johndoe", "", firstPassword)
|
||
|
|
||
|
if !user.IsUsingThisPassword(firstPassword) {
|
||
|
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", false, true, firstPassword)
|
||
|
}
|
||
|
|
||
|
if user.IsUsingThisPassword(secondPassword) {
|
||
|
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", true, false, secondPassword)
|
||
|
}
|
||
|
|
||
|
user, err := user.ChangePassword(secondPassword, firstPassword)
|
||
|
if err == nil {
|
||
|
t.Errorf("\ngot: %v\nwanted: %v", err, errors.New("wrong password"))
|
||
|
}
|
||
|
|
||
|
if !user.IsUsingThisPassword(firstPassword) {
|
||
|
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", false, true, firstPassword)
|
||
|
}
|
||
|
|
||
|
if user.IsUsingThisPassword(secondPassword) {
|
||
|
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", true, false, secondPassword)
|
||
|
}
|
||
|
|
||
|
user, err = user.ChangePassword(firstPassword, secondPassword)
|
||
|
if err != nil {
|
||
|
t.Errorf("\ngot: %v\nwanted: %v", err, nil)
|
||
|
}
|
||
|
|
||
|
if user.IsUsingThisPassword(firstPassword) {
|
||
|
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", true, false, firstPassword)
|
||
|
}
|
||
|
|
||
|
if !user.IsUsingThisPassword(secondPassword) {
|
||
|
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", false, true, secondPassword)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestUserIsUsingThisPassword(t *testing.T) {
|
||
|
user := CreateUser("johndoe", "", "123456")
|
||
|
|
||
|
data := map[string]bool{
|
||
|
"": false,
|
||
|
" ": false,
|
||
|
"12345": false,
|
||
|
"123456": true,
|
||
|
"1234567": false,
|
||
|
}
|
||
|
|
||
|
for d, expectedResult := range data {
|
||
|
result := user.IsUsingThisPassword(d)
|
||
|
if result != expectedResult {
|
||
|
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestGroupAddMember(t *testing.T) {
|
||
|
user := CreateUser("johndoe", "", "123456")
|
||
|
group := CreateGroup("mastersOfSecrets")
|
||
|
|
||
|
group, err := group.AddMember(user)
|
||
|
if err != nil {
|
||
|
t.Errorf("\ngot: %v\nwanted: %v", err, nil)
|
||
|
}
|
||
|
|
||
|
members, _ := json.Marshal(group.Members)
|
||
|
result := string(members)
|
||
|
expectedResult := fmt.Sprintf("{\"%s\":true}", user.UUID)
|
||
|
if result != expectedResult {
|
||
|
t.Errorf("\ngot: %v\nwanted: %v", result, expectedResult)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestGroupRemoveMember(t *testing.T) {
|
||
|
user := CreateUser("johndoe", "", "123456")
|
||
|
group, _ := CreateGroup("mastersOfSecrets").AddMember(user)
|
||
|
|
||
|
group = group.RemoveMember(user)
|
||
|
|
||
|
members, _ := json.Marshal(group.Members)
|
||
|
result := string(members)
|
||
|
expectedResult := "{}"
|
||
|
if result != expectedResult {
|
||
|
t.Errorf("\ngot: %v\nwanted: %v", result, expectedResult)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestUserIsMemberOfGroup(t *testing.T) {
|
||
|
user := CreateUser("johndoe", "", "123456")
|
||
|
user2 := CreateUser("janedoe", "", "abc123")
|
||
|
group, _ := CreateGroup("mastersOfSecrets").AddMember(user)
|
||
|
|
||
|
if !user.IsMemberOfGroup(group) {
|
||
|
t.Errorf("\ngot: %t\nwanted: %t", false, true)
|
||
|
}
|
||
|
|
||
|
if user2.IsMemberOfGroup(group) {
|
||
|
t.Errorf("\ngot: %t\nwanted: %t", true, false)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestGroupHasUserAsMember(t *testing.T) {
|
||
|
user := CreateUser("johndoe", "", "123456")
|
||
|
user2 := CreateUser("janedoe", "", "abc123")
|
||
|
group, _ := CreateGroup("mastersOfSecrets").AddMember(user)
|
||
|
|
||
|
if !group.HasUserAsMember(user) {
|
||
|
t.Errorf("\ngot: %t\nwanted: %t", false, true)
|
||
|
}
|
||
|
|
||
|
if group.HasUserAsMember(user2) {
|
||
|
t.Errorf("\ngot: %t\nwanted: %t", true, false)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestGroupAddPermission(t *testing.T) {
|
||
|
permission := CreatePermission("accessToSecrets")
|
||
|
group := CreateGroup("mastersOfSecrets")
|
||
|
|
||
|
group, err := group.AddPermission(permission)
|
||
|
if err != nil {
|
||
|
t.Errorf("\ngot: %v\nwanted: %v", err, nil)
|
||
|
}
|
||
|
|
||
|
permissions, _ := json.Marshal(group.Permissions)
|
||
|
result := string(permissions)
|
||
|
expectedResult := fmt.Sprintf("{\"%s\":true}", permission.UUID)
|
||
|
if result != expectedResult {
|
||
|
t.Errorf("\ngot: %v\nwanted: %v", result, expectedResult)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestGroupRemovePermission(t *testing.T) {
|
||
|
permission := CreatePermission("accessToSecrets")
|
||
|
group, _ := CreateGroup("mastersOfSecrets").AddPermission(permission)
|
||
|
|
||
|
group = group.RemovePermission(permission)
|
||
|
|
||
|
permissions, _ := json.Marshal(group.Permissions)
|
||
|
result := string(permissions)
|
||
|
expectedResult := "{}"
|
||
|
if result != expectedResult {
|
||
|
t.Errorf("\ngot: %v\nwanted: %v", result, expectedResult)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestGroupHasPermission(t *testing.T) {
|
||
|
permission := CreatePermission("accessToSecrets")
|
||
|
permission2 := CreatePermission("accessToTheBathroom")
|
||
|
group, _ := CreateGroup("mastersOfSecrets").AddPermission(permission)
|
||
|
|
||
|
if !group.HasPermission(permission) {
|
||
|
t.Errorf("\ngot: %t\nwanted: %t", false, true)
|
||
|
}
|
||
|
|
||
|
if group.HasPermission(permission2) {
|
||
|
t.Errorf("\ngot: %t\nwanted: %t", true, false)
|
||
|
}
|
||
|
}
|