summaryrefslogtreecommitdiffstats
path: root/model
diff options
context:
space:
mode:
authorGeorge Goldberg <george@gberg.me>2017-04-20 14:58:54 +0100
committerJoram Wilander <jwawilander@gmail.com>2017-04-20 09:58:54 -0400
commit55bbf15fc7a83d3cda1fe5bc037823cbbc4fc023 (patch)
tree312f6a9c42c8f153168df8f960b613666777f64a /model
parent8723f61f4540c74d5c755d7f9532f8fe199ccb6f (diff)
downloadchat-55bbf15fc7a83d3cda1fe5bc037823cbbc4fc023.tar.gz
chat-55bbf15fc7a83d3cda1fe5bc037823cbbc4fc023.tar.bz2
chat-55bbf15fc7a83d3cda1fe5bc037823cbbc4fc023.zip
PLT-6112: Add some more unit tests to the model package (#6142)
* Unit Tests for model/push_response.go * Unit tests for security_bulletin.go * Unit tests for webrtc.go * Unit tests for model/password_recovery.go * Add missing headers. * Unit tests for model/license.go * Tidy up existing tests. * Simplify JSON to/from tests. * Fix gofmt
Diffstat (limited to 'model')
-rw-r--r--model/license_test.go179
-rw-r--r--model/modeltestlib.go51
-rw-r--r--model/password_recovery_test.go53
-rw-r--r--model/push_response_test.go34
-rw-r--r--model/security_bulletin_test.go55
-rw-r--r--model/webrtc_test.go27
6 files changed, 394 insertions, 5 deletions
diff --git a/model/license_test.go b/model/license_test.go
index 94a6ad130..1c6a277e8 100644
--- a/model/license_test.go
+++ b/model/license_test.go
@@ -4,10 +4,83 @@
package model
import (
+ "strings"
"testing"
)
-func TestLicenseExpired(t *testing.T) {
+func TestLicenseFeaturesToMap(t *testing.T) {
+ f := Features{}
+ f.SetDefaults()
+
+ m := f.ToMap()
+
+ CheckTrue(t, m["ldap"].(bool))
+ CheckTrue(t, m["mfa"].(bool))
+ CheckTrue(t, m["google"].(bool))
+ CheckTrue(t, m["office365"].(bool))
+ CheckTrue(t, m["compliance"].(bool))
+ CheckTrue(t, m["cluster"].(bool))
+ CheckTrue(t, m["metrics"].(bool))
+ CheckTrue(t, m["custom_brand"].(bool))
+ CheckTrue(t, m["mhpns"].(bool))
+ CheckTrue(t, m["saml"].(bool))
+ CheckTrue(t, m["password"].(bool))
+ CheckTrue(t, m["future"].(bool))
+}
+
+func TestLicenseFeaturesSetDefaults(t *testing.T) {
+ f := Features{}
+ f.SetDefaults()
+
+ CheckInt(t, *f.Users, 0)
+ CheckTrue(t, *f.LDAP)
+ CheckTrue(t, *f.MFA)
+ CheckTrue(t, *f.GoogleOAuth)
+ CheckTrue(t, *f.Office365OAuth)
+ CheckTrue(t, *f.Compliance)
+ CheckTrue(t, *f.Cluster)
+ CheckTrue(t, *f.Metrics)
+ CheckTrue(t, *f.CustomBrand)
+ CheckTrue(t, *f.MHPNS)
+ CheckTrue(t, *f.SAML)
+ CheckTrue(t, *f.PasswordRequirements)
+ CheckTrue(t, *f.FutureFeatures)
+
+ f = Features{}
+ f.SetDefaults()
+
+ *f.Users = 300
+ *f.FutureFeatures = false
+ *f.LDAP = true
+ *f.MFA = true
+ *f.GoogleOAuth = true
+ *f.Office365OAuth = true
+ *f.Compliance = true
+ *f.Cluster = true
+ *f.Metrics = true
+ *f.CustomBrand = true
+ *f.MHPNS = true
+ *f.SAML = true
+ *f.PasswordRequirements = true
+
+ f.SetDefaults()
+
+ CheckInt(t, *f.Users, 300)
+ CheckTrue(t, *f.LDAP)
+ CheckTrue(t, *f.MFA)
+ CheckTrue(t, *f.GoogleOAuth)
+ CheckTrue(t, *f.Office365OAuth)
+ CheckTrue(t, *f.Compliance)
+ CheckTrue(t, *f.Cluster)
+ CheckTrue(t, *f.Metrics)
+ CheckTrue(t, *f.CustomBrand)
+ CheckTrue(t, *f.MHPNS)
+ CheckTrue(t, *f.SAML)
+ CheckTrue(t, *f.PasswordRequirements)
+ CheckFalse(t, *f.FutureFeatures)
+}
+
+func TestLicenseIsExpired(t *testing.T) {
l1 := License{}
l1.ExpiresAt = GetMillis() - 1000
if !l1.IsExpired() {
@@ -20,7 +93,7 @@ func TestLicenseExpired(t *testing.T) {
}
}
-func TestLicenseStarted(t *testing.T) {
+func TestLicenseIsStarted(t *testing.T) {
l1 := License{}
l1.StartsAt = GetMillis() - 1000
if !l1.IsStarted() {
@@ -32,3 +105,105 @@ func TestLicenseStarted(t *testing.T) {
t.Fatal("license should not be started")
}
}
+
+func TestLicenseToFromJson(t *testing.T) {
+ f := Features{}
+ f.SetDefaults()
+
+ l := License{
+ Id: NewId(),
+ IssuedAt: GetMillis(),
+ StartsAt: GetMillis(),
+ ExpiresAt: GetMillis(),
+ Customer: &Customer{
+ Id: NewId(),
+ Name: NewId(),
+ Email: NewId(),
+ Company: NewId(),
+ PhoneNumber: NewId(),
+ },
+ Features: &f,
+ }
+
+ j := l.ToJson()
+
+ l1 := LicenseFromJson(strings.NewReader(j))
+ if l1 == nil {
+ t.Fatalf("Decoding failed but should have passed.")
+ }
+
+ CheckString(t, l1.Id, l.Id)
+ CheckInt64(t, l1.IssuedAt, l.IssuedAt)
+ CheckInt64(t, l1.StartsAt, l.StartsAt)
+ CheckInt64(t, l1.ExpiresAt, l.ExpiresAt)
+
+ CheckString(t, l1.Customer.Id, l.Customer.Id)
+ CheckString(t, l1.Customer.Name, l.Customer.Name)
+ CheckString(t, l1.Customer.Email, l.Customer.Email)
+ CheckString(t, l1.Customer.Company, l.Customer.Company)
+ CheckString(t, l1.Customer.PhoneNumber, l.Customer.PhoneNumber)
+
+ f1 := l1.Features
+
+ CheckInt(t, *f1.Users, *f.Users)
+ CheckBool(t, *f1.LDAP, *f.LDAP)
+ CheckBool(t, *f1.MFA, *f.MFA)
+ CheckBool(t, *f1.GoogleOAuth, *f.GoogleOAuth)
+ CheckBool(t, *f1.Office365OAuth, *f.Office365OAuth)
+ CheckBool(t, *f1.Compliance, *f.Compliance)
+ CheckBool(t, *f1.Cluster, *f.Cluster)
+ CheckBool(t, *f1.Metrics, *f.Metrics)
+ CheckBool(t, *f1.CustomBrand, *f.CustomBrand)
+ CheckBool(t, *f1.MHPNS, *f.MHPNS)
+ CheckBool(t, *f1.SAML, *f.SAML)
+ CheckBool(t, *f1.PasswordRequirements, *f.PasswordRequirements)
+ CheckBool(t, *f1.FutureFeatures, *f.FutureFeatures)
+
+ invalid := `{"asdf`
+ l2 := LicenseFromJson(strings.NewReader(invalid))
+ if l2 != nil {
+ t.Fatalf("Should have failed but didn't")
+ }
+}
+
+func TestLicenseRecordIsValid(t *testing.T) {
+ lr := LicenseRecord{
+ CreateAt: GetMillis(),
+ Bytes: "asdfghjkl;",
+ }
+
+ if err := lr.IsValid(); err == nil {
+ t.Fatalf("Should have been invalid")
+ }
+
+ lr.Id = NewId()
+ lr.CreateAt = 0
+ if err := lr.IsValid(); err == nil {
+ t.Fatalf("Should have been invalid")
+ }
+
+ lr.CreateAt = GetMillis()
+ lr.Bytes = ""
+ if err := lr.IsValid(); err == nil {
+ t.Fatalf("Should have been invalid")
+ }
+
+ lr.Bytes = strings.Repeat("0123456789", 1001)
+ if err := lr.IsValid(); err == nil {
+ t.Fatalf("Should have been invalid")
+ }
+
+ lr.Bytes = "ASDFGHJKL;"
+ if err := lr.IsValid(); err != nil {
+ t.Fatal(err)
+ }
+}
+
+func TestLicenseRecordPreSave(t *testing.T) {
+ lr := LicenseRecord{}
+ lr.PreSave()
+
+ if lr.CreateAt == 0 {
+ t.Fatal("CreateAt should not be zero")
+ }
+}
diff --git a/model/modeltestlib.go b/model/modeltestlib.go
new file mode 100644
index 000000000..2734b3ead
--- /dev/null
+++ b/model/modeltestlib.go
@@ -0,0 +1,51 @@
+// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package model
+
+import (
+ "runtime/debug"
+ "testing"
+)
+
+func CheckInt(t *testing.T, got int, expected int) {
+ if got != expected {
+ debug.PrintStack()
+ t.Fatalf("Got: %v, Expected: %v", got, expected)
+ }
+}
+
+func CheckInt64(t *testing.T, got int64, expected int64) {
+ if got != expected {
+ debug.PrintStack()
+ t.Fatalf("Got: %v, Expected: %v", got, expected)
+ }
+}
+
+func CheckString(t *testing.T, got string, expected string) {
+ if got != expected {
+ debug.PrintStack()
+ t.Fatalf("Got: %v, Expected: %v", got, expected)
+ }
+}
+
+func CheckTrue(t *testing.T, test bool) {
+ if !test {
+ debug.PrintStack()
+ t.Fatal("Expected true")
+ }
+}
+
+func CheckFalse(t *testing.T, test bool) {
+ if test {
+ debug.PrintStack()
+ t.Fatal("Expected true")
+ }
+}
+
+func CheckBool(t *testing.T, got bool, expected bool) {
+ if got != expected {
+ debug.PrintStack()
+ t.Fatalf("Got: %v, Expected: %v", got, expected)
+ }
+}
diff --git a/model/password_recovery_test.go b/model/password_recovery_test.go
new file mode 100644
index 000000000..d64f430fc
--- /dev/null
+++ b/model/password_recovery_test.go
@@ -0,0 +1,53 @@
+// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package model
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestPasswordRecoveryIsValid(t *testing.T) {
+ // Valid example.
+ p := PasswordRecovery{
+ UserId: NewId(),
+ Code: strings.Repeat("a", 128),
+ CreateAt: GetMillis(),
+ }
+
+ if err := p.IsValid(); err != nil {
+ t.Fatal(err)
+ }
+
+ // Various invalid ones.
+ p.UserId = "abc"
+ if err := p.IsValid(); err == nil {
+ t.Fatal("Should have failed validation")
+ }
+
+ p.UserId = NewId()
+ p.Code = "abc"
+ if err := p.IsValid(); err == nil {
+ t.Fatal("Should have failed validation")
+ }
+
+ p.Code = strings.Repeat("a", 128)
+ p.CreateAt = 0
+ if err := p.IsValid(); err == nil {
+ t.Fatal("Should have failed validation")
+ }
+}
+
+func TestPasswordRecoveryPreSave(t *testing.T) {
+ p := PasswordRecovery{
+ UserId: NewId(),
+ }
+
+ // Check it's valid after running PreSave
+ p.PreSave()
+
+ if err := p.IsValid(); err != nil {
+ t.Fatal(err)
+ }
+}
diff --git a/model/push_response_test.go b/model/push_response_test.go
new file mode 100644
index 000000000..6f9315550
--- /dev/null
+++ b/model/push_response_test.go
@@ -0,0 +1,34 @@
+// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package model
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestNewOkPushResponse(t *testing.T) {
+ r := NewOkPushResponse()
+ CheckString(t, r["status"], "OK")
+}
+
+func TestNewRemovePushResponse(t *testing.T) {
+ r := NewRemovePushResponse()
+ CheckString(t, r["status"], "REMOVE")
+}
+
+func TestNewErrorPushResponse(t *testing.T) {
+ r := NewErrorPushResponse("error message")
+ CheckString(t, r["status"], "FAIL")
+ CheckString(t, r["error"], "error message")
+}
+
+func TestPushResponseToFromJson(t *testing.T) {
+ r := NewErrorPushResponse("error message")
+ j := r.ToJson()
+ r1 := PushResponseFromJson(strings.NewReader(j))
+
+ CheckString(t, r1["status"], r["status"])
+ CheckString(t, r1["error"], r["error"])
+}
diff --git a/model/security_bulletin_test.go b/model/security_bulletin_test.go
new file mode 100644
index 000000000..a6e55fe1c
--- /dev/null
+++ b/model/security_bulletin_test.go
@@ -0,0 +1,55 @@
+// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package model
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestSecurityBulletinToFromJson(t *testing.T) {
+ b := SecurityBulletin{
+ Id: NewId(),
+ AppliesToVersion: NewId(),
+ }
+
+ j := b.ToJson()
+ b1 := SecurityBulletinFromJson(strings.NewReader(j))
+
+ CheckString(t, b1.AppliesToVersion, b.AppliesToVersion)
+ CheckString(t, b1.Id, b.Id)
+
+ // Malformed JSON
+ s2 := `{"wat"`
+ b2 := SecurityBulletinFromJson(strings.NewReader(s2))
+
+ if b2 != nil {
+ t.Fatal("expected nil")
+ }
+}
+
+func TestSecurityBulletinsToFromJson(t *testing.T) {
+ b := SecurityBulletins{
+ {
+ Id: NewId(),
+ AppliesToVersion: NewId(),
+ },
+ {
+ Id: NewId(),
+ AppliesToVersion: NewId(),
+ },
+ }
+
+ j := b.ToJson()
+
+ b1 := SecurityBulletinsFromJson(strings.NewReader(j))
+
+ CheckInt(t, len(b1), 2)
+
+ // Malformed JSON
+ s2 := `{"wat"`
+ b2 := SecurityBulletinsFromJson(strings.NewReader(s2))
+
+ CheckInt(t, len(b2), 0)
+}
diff --git a/model/webrtc_test.go b/model/webrtc_test.go
index 2418bd53a..7ec6605f8 100644
--- a/model/webrtc_test.go
+++ b/model/webrtc_test.go
@@ -8,12 +8,33 @@ import (
"testing"
)
-func TestWebrtcJson(t *testing.T) {
+func TestWebrtcInfoResponseToFromJson(t *testing.T) {
o := WebrtcInfoResponse{Token: NewId(), GatewayUrl: NewId()}
json := o.ToJson()
ro := WebrtcInfoResponseFromJson(strings.NewReader(json))
- if o.Token != ro.Token {
- t.Fatal("Tokens do not match")
+ CheckString(t, ro.Token, o.Token)
+ CheckString(t, ro.GatewayUrl, o.GatewayUrl)
+
+ invalidJson := `{"wat"`
+ r := WebrtcInfoResponseFromJson(strings.NewReader(invalidJson))
+ if r != nil {
+ t.Fatalf("Should have failed")
+ }
+}
+
+func TestGatewayResponseFromJson(t *testing.T) {
+ // Valid Gateway Response
+ s1 := `{"janus": "something"}`
+ g1 := GatewayResponseFromJson(strings.NewReader(s1))
+
+ CheckString(t, g1.Status, "something")
+
+ // Malformed JSON
+ s2 := `{"wat"`
+ g2 := GatewayResponseFromJson(strings.NewReader(s2))
+
+ if g2 != nil {
+ t.Fatal("expected nil")
}
}