diff --git a/go.mod b/go.mod index 125f108..867575f 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/gorilla/mux v1.6.2 github.com/jmoiron/sqlx v1.2.0 github.com/mattn/go-sqlite3 v1.10.0 + github.com/stretchr/testify v1.3.0 golang.org/x/crypto v0.0.0-20190123085648-057139ce5d2b golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 google.golang.org/appengine v1.4.0 // indirect diff --git a/go.sum b/go.sum index 8207f6b..ee96f68 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/collinux/GoHue v0.0.0-20181229002551-d259041d5eb8 h1:NOPuu1sMqBVC3iyl github.com/collinux/GoHue v0.0.0-20181229002551-d259041d5eb8/go.mod h1:ex2AEcUvgoeoE/uknn0ZUExwGhBcH9jwA5heqv2xq6w= github.com/collinux/gohue v0.0.0-20181229002551-d259041d5eb8 h1:Qhd31xZ6GUL0nEaXYP4nXOn8J6l9jqa6xEyp70qfjZE= github.com/collinux/gohue v0.0.0-20181229002551-d259041d5eb8/go.mod h1:HFm7vkh/1EJQ9ymYsKUQtK7JlG3om1r61wMAHtl+bxw= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-sql-driver/mysql v1.4.0 h1:7LxgVwFb2hIQtMm87NdgAVfXjnt4OePseqT1tKx+opk= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -16,6 +18,11 @@ github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o= github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= golang.org/x/crypto v0.0.0-20190123085648-057139ce5d2b h1:Elez2XeF2p9uyVj0yEUDqQ56NFcDtcBNkYP7yv8YbUE= golang.org/x/crypto v0.0.0-20190123085648-057139ce5d2b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= diff --git a/models/light_test.go b/models/light_test.go new file mode 100644 index 0000000..b777535 --- /dev/null +++ b/models/light_test.go @@ -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) +} diff --git a/models/session_test.go b/models/session_test.go new file mode 100644 index 0000000..b312eca --- /dev/null +++ b/models/session_test.go @@ -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) +} \ No newline at end of file diff --git a/models/user_test.go b/models/user_test.go new file mode 100644 index 0000000..4077a32 --- /dev/null +++ b/models/user_test.go @@ -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) +}