2020-07-10 10:58:06 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2020-11-21 18:08:41 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-07-10 10:58:06 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewPassword(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
p := NewPassword("abc567", "passwd")
|
|
|
|
assert.Len(t, p.Hash, 60)
|
|
|
|
})
|
|
|
|
t.Run("empty password", func(t *testing.T) {
|
|
|
|
p := NewPassword("abc567", "")
|
|
|
|
assert.Equal(t, "", p.Hash)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPassword_SetPassword(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
p := NewPassword("abc567", "passwd")
|
|
|
|
assert.Len(t, p.Hash, 60)
|
|
|
|
if err := p.SetPassword("abcd"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Len(t, p.Hash, 60)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-11 22:44:11 +02:00
|
|
|
func TestPassword_Is(t *testing.T) {
|
|
|
|
t.Run("Empty", func(t *testing.T) {
|
2020-07-10 10:58:06 +02:00
|
|
|
p := Password{Hash: ""}
|
2022-10-11 22:44:11 +02:00
|
|
|
assert.True(t, p.Is(""))
|
2020-07-10 10:58:06 +02:00
|
|
|
})
|
2022-10-11 22:44:11 +02:00
|
|
|
t.Run("Ok", func(t *testing.T) {
|
2020-07-10 10:58:06 +02:00
|
|
|
p := NewPassword("abc567", "")
|
2022-10-11 22:44:11 +02:00
|
|
|
assert.True(t, p.Is(""))
|
2020-07-10 10:58:06 +02:00
|
|
|
})
|
2022-10-11 22:44:11 +02:00
|
|
|
t.Run("Wrong", func(t *testing.T) {
|
|
|
|
p := NewPassword("abc567", "passwd")
|
|
|
|
assert.False(t, p.Is("$2a$14$p3HKuLvrTuePG/pjXLJQseUnSeAVeVO2cy4b0.34KXsLPK8lkI92G"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPassword_IsWrong(t *testing.T) {
|
|
|
|
t.Run("Empty", func(t *testing.T) {
|
|
|
|
p := Password{Hash: ""}
|
|
|
|
assert.False(t, p.IsWrong(""))
|
|
|
|
})
|
|
|
|
t.Run("Ok", func(t *testing.T) {
|
|
|
|
p := NewPassword("abc567", "")
|
|
|
|
assert.False(t, p.IsWrong(""))
|
|
|
|
})
|
|
|
|
t.Run("Wrong", func(t *testing.T) {
|
2020-07-10 10:58:06 +02:00
|
|
|
p := NewPassword("abc567", "passwd")
|
2022-10-11 22:44:11 +02:00
|
|
|
assert.True(t, p.IsWrong("$2a$14$p3HKuLvrTuePG/pjXLJQseUnSeAVeVO2cy4b0.34KXsLPK8lkI92G"))
|
2020-07-10 10:58:06 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-10 16:09:21 +02:00
|
|
|
// TODO fails on mariadb
|
2020-07-10 10:58:06 +02:00
|
|
|
func TestPassword_Create(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
p := Password{}
|
|
|
|
|
|
|
|
err := p.Create()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindPassword(t *testing.T) {
|
|
|
|
t.Run("not existing", func(t *testing.T) {
|
|
|
|
r := FindPassword("xxx")
|
|
|
|
assert.Nil(t, r)
|
|
|
|
})
|
|
|
|
t.Run("existing", func(t *testing.T) {
|
|
|
|
p := NewPassword("abc567", "passwd")
|
|
|
|
if err := p.Save(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r := FindPassword("abc567")
|
|
|
|
assert.NotEmpty(t, r)
|
|
|
|
})
|
2021-08-10 12:30:04 +02:00
|
|
|
t.Run("alice", func(t *testing.T) {
|
|
|
|
if p := FindPassword("uqxetse3cy5eo9z2"); p == nil {
|
|
|
|
t.Fatal("password not found")
|
|
|
|
} else {
|
2022-10-11 22:44:11 +02:00
|
|
|
assert.False(t, p.IsWrong("Alice123!"))
|
2021-08-10 12:30:04 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("bob", func(t *testing.T) {
|
|
|
|
if p := FindPassword("uqxc08w3d0ej2283"); p == nil {
|
|
|
|
t.Fatal("password not found")
|
|
|
|
} else {
|
2022-10-11 22:44:11 +02:00
|
|
|
assert.False(t, p.IsWrong("Bobbob123!"))
|
2021-08-10 12:30:04 +02:00
|
|
|
}
|
|
|
|
})
|
2020-07-10 10:58:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPassword_String(t *testing.T) {
|
|
|
|
t.Run("return string", func(t *testing.T) {
|
|
|
|
p := NewPassword("abc567", "lkjhgtyu")
|
|
|
|
assert.Len(t, p.String(), 60)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPassword_Unknown(t *testing.T) {
|
|
|
|
t.Run("false", func(t *testing.T) {
|
|
|
|
p := NewPassword("abc567", "lkjhgtyu")
|
|
|
|
assert.False(t, p.Unknown())
|
|
|
|
})
|
|
|
|
t.Run("true", func(t *testing.T) {
|
|
|
|
p := Password{}
|
|
|
|
assert.True(t, p.Unknown())
|
|
|
|
})
|
|
|
|
}
|