Stian Aune
5 years ago
5 changed files with 225 additions and 0 deletions
@ -0,0 +1,102 @@ |
|||||
|
package models_test |
||||
|
|
||||
|
import ( |
||||
|
"git.aiterp.net/lucifer/lucifer/models" |
||||
|
"github.com/stretchr/testify/assert" |
||||
|
"testing" |
||||
|
) |
||||
|
|
||||
|
func TestLight_SetColorRGB(t *testing.T) { |
||||
|
light := models.Light{} |
||||
|
|
||||
|
light.SetColorRGB(32, 55, 100) |
||||
|
|
||||
|
assert.Equal(t, "203764", light.Color) |
||||
|
} |
||||
|
|
||||
|
func TestLight_SetColorRGBf(t *testing.T) { |
||||
|
light := models.Light{} |
||||
|
|
||||
|
light.SetColorRGBf(0.1, 0.2, 0.3) |
||||
|
|
||||
|
assert.Equal(t, "19334c", light.Color) |
||||
|
} |
||||
|
|
||||
|
func TestLight_ColorRGB_Empty(t *testing.T) { |
||||
|
light := models.Light{} |
||||
|
light.Color = "" |
||||
|
|
||||
|
r, g, b, err := light.ColorRGB() |
||||
|
|
||||
|
assert.Equal(t, uint8(0), r) |
||||
|
assert.Equal(t, uint8(0), g) |
||||
|
assert.Equal(t, uint8(0), b) |
||||
|
assert.Nil(t, err) |
||||
|
} |
||||
|
|
||||
|
func TestLight_ColorRGB_Invalid(t *testing.T) { |
||||
|
light := models.Light{} |
||||
|
light.Color = "Utterly bonkers" |
||||
|
|
||||
|
_, _, _, err := light.ColorRGB() |
||||
|
|
||||
|
assert.NotNil(t, err) |
||||
|
assert.Equal(t, models.ErrMalformedColor, err) |
||||
|
} |
||||
|
|
||||
|
func TestLight_ColorRGB_TooShort(t *testing.T) { |
||||
|
light := models.Light{} |
||||
|
light.Color = "3002" |
||||
|
|
||||
|
_, _, _, err := light.ColorRGB() |
||||
|
|
||||
|
assert.NotNil(t, err) |
||||
|
assert.Equal(t, models.ErrMalformedColor, err) |
||||
|
} |
||||
|
|
||||
|
func TestLight_ColorRGB_TooLong(t *testing.T) { |
||||
|
light := models.Light{} |
||||
|
light.Color = "3002DB05" |
||||
|
|
||||
|
_, _, _, err := light.ColorRGB() |
||||
|
|
||||
|
assert.NotNil(t, err) |
||||
|
assert.Equal(t, models.ErrMalformedColor, err) |
||||
|
} |
||||
|
|
||||
|
func TestLight_ColorRGB_Valid(t *testing.T) { |
||||
|
light := models.Light{} |
||||
|
light.Color = "203764" |
||||
|
|
||||
|
r, g, b, err := light.ColorRGB() |
||||
|
|
||||
|
assert.Equal(t, uint8(32), r) |
||||
|
assert.Equal(t, uint8(55), g) |
||||
|
assert.Equal(t, uint8(100), b) |
||||
|
assert.Nil(t, err) |
||||
|
} |
||||
|
|
||||
|
func TestLight_ColorRGBf_Invalid(t *testing.T) { |
||||
|
light := models.Light{} |
||||
|
light.Color = "G0I1J2" |
||||
|
|
||||
|
r, g, b, err := light.ColorRGBf() |
||||
|
|
||||
|
assert.Equal(t, float64(0), r) |
||||
|
assert.Equal(t, float64(0), g) |
||||
|
assert.Equal(t, float64(0), b) |
||||
|
assert.NotNil(t, err) |
||||
|
} |
||||
|
|
||||
|
func TestLight_ColorRGBf_Valid(t *testing.T) { |
||||
|
light := models.Light{} |
||||
|
light.Color = "19334C" |
||||
|
|
||||
|
r, g, b, err := light.ColorRGBf() |
||||
|
|
||||
|
// Within half a percent
|
||||
|
assert.InDelta(t, float64(0.1), r, 0.005) |
||||
|
assert.InDelta(t, float64(0.2), g, 0.005) |
||||
|
assert.InDelta(t, float64(0.3), b, 0.005) |
||||
|
assert.Nil(t, err) |
||||
|
} |
@ -0,0 +1,73 @@ |
|||||
|
package models_test |
||||
|
|
||||
|
import ( |
||||
|
"context" |
||||
|
"git.aiterp.net/lucifer/lucifer/models" |
||||
|
"github.com/stretchr/testify/assert" |
||||
|
"testing" |
||||
|
"time" |
||||
|
) |
||||
|
|
||||
|
func TestSession_GenerateID_Works(t *testing.T) { |
||||
|
session := &models.Session{} |
||||
|
|
||||
|
err := session.GenerateID() |
||||
|
|
||||
|
assert.Nil(t, err) |
||||
|
} |
||||
|
|
||||
|
func TestSession_Cookie_IsCorrect(t *testing.T) { |
||||
|
sessID := "testId" |
||||
|
expiry := time.Now().AddDate(0, 0, 2) |
||||
|
|
||||
|
session := &models.Session{ |
||||
|
ID: sessID, |
||||
|
UserID: 0, |
||||
|
Expires: expiry, |
||||
|
} |
||||
|
|
||||
|
cookie := session.Cookie() |
||||
|
|
||||
|
assert.Equal(t, sessID, cookie.Value) |
||||
|
assert.Equal(t, expiry, cookie.Expires) |
||||
|
} |
||||
|
|
||||
|
func TestSession_Expired_BeforeExpiry(t *testing.T) { |
||||
|
session := &models.Session{ |
||||
|
ID: "12345", |
||||
|
UserID: 0, |
||||
|
Expires: time.Now().AddDate(0, 0, 1), |
||||
|
} |
||||
|
|
||||
|
assert.False(t, session.Expired()) |
||||
|
} |
||||
|
|
||||
|
func TestSession_Expired_AfterExpiry(t *testing.T) { |
||||
|
session := &models.Session{ |
||||
|
ID: "12345", |
||||
|
UserID: 0, |
||||
|
Expires: time.Now().AddDate(0, 0, -1), |
||||
|
} |
||||
|
|
||||
|
assert.True(t, session.Expired()) |
||||
|
} |
||||
|
|
||||
|
func TestSessionFromContext_InContext(t *testing.T) { |
||||
|
session := &models.Session{ |
||||
|
ID: "12345", |
||||
|
UserID: 0, |
||||
|
Expires: time.Now().AddDate(0, 0, 1), |
||||
|
} |
||||
|
|
||||
|
ctx := session.InContext(context.Background()) |
||||
|
|
||||
|
session2 := models.SessionFromContext(ctx) |
||||
|
|
||||
|
assert.Equal(t, session2, session) |
||||
|
} |
||||
|
|
||||
|
func TestSessionFromContext_Empty(t *testing.T) { |
||||
|
session := models.SessionFromContext(context.Background()) |
||||
|
|
||||
|
assert.Nil(t, session) |
||||
|
} |
@ -0,0 +1,42 @@ |
|||||
|
package models_test |
||||
|
|
||||
|
import ( |
||||
|
"git.aiterp.net/lucifer/lucifer/models" |
||||
|
"github.com/stretchr/testify/assert" |
||||
|
"testing" |
||||
|
) |
||||
|
|
||||
|
func TestUser_SetPassword_TooShort(t *testing.T) { |
||||
|
user := &models.User{} |
||||
|
|
||||
|
err := user.SetPassword("short") |
||||
|
|
||||
|
assert.NotNil(t, err) |
||||
|
} |
||||
|
|
||||
|
func TestUser_SetPassword_Valid(t *testing.T) { |
||||
|
user := &models.User{} |
||||
|
|
||||
|
err := user.SetPassword("longer") |
||||
|
|
||||
|
assert.Nil(t, err) |
||||
|
assert.NotEqual(t, "", user.PassHash) |
||||
|
} |
||||
|
|
||||
|
func TestUser_CheckPassword_Wrong(t *testing.T) { |
||||
|
user := &models.User{} |
||||
|
_ = user.SetPassword("longer") |
||||
|
|
||||
|
err := user.CheckPassword("wrong attempt") |
||||
|
|
||||
|
assert.NotNil(t, err) |
||||
|
} |
||||
|
|
||||
|
func TestUser_CheckPassword_Correct(t *testing.T) { |
||||
|
user := &models.User{} |
||||
|
_ = user.SetPassword("correct") |
||||
|
|
||||
|
err := user.CheckPassword("correct") |
||||
|
|
||||
|
assert.Nil(t, err) |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue