summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoramWilander <jwawilander@gmail.com>2016-01-13 10:54:12 -0500
committerJoramWilander <jwawilander@gmail.com>2016-01-14 08:24:09 -0500
commit874d120535a615afddeb80599b7d2d982959ffdb (patch)
tree226c38cf4d42068733c3c661d3983963a2df4272
parent9110dd54a15f3d0fcf6f60936e01d816b667b93c (diff)
downloadchat-874d120535a615afddeb80599b7d2d982959ffdb.tar.gz
chat-874d120535a615afddeb80599b7d2d982959ffdb.tar.bz2
chat-874d120535a615afddeb80599b7d2d982959ffdb.zip
Add some unit tests
-rw-r--r--api/user.go29
-rw-r--r--model/license.go2
-rw-r--r--model/license_test.go34
-rw-r--r--utils/license_test.go50
4 files changed, 105 insertions, 10 deletions
diff --git a/api/user.go b/api/user.go
index d014ab995..786414227 100644
--- a/api/user.go
+++ b/api/user.go
@@ -122,6 +122,11 @@ func createUser(c *Context, w http.ResponseWriter, r *http.Request) {
user.EmailVerified = true
}
+ if !CheckUserDomain(user, utils.Cfg.TeamSettings.RestrictCreationToDomains) {
+ c.Err = model.NewAppError("createUser", "The email you provided does not belong to an accepted domain. Please contact your administrator or sign up with a different email.", "")
+ return
+ }
+
ruser, err := CreateUser(team, user)
if err != nil {
c.Err = err
@@ -136,19 +141,25 @@ func createUser(c *Context, w http.ResponseWriter, r *http.Request) {
}
+func CheckUserDomain(user *model.User, domains string) bool {
+ domainArray := strings.Fields(strings.TrimSpace(strings.ToLower(strings.Replace(strings.Replace(domains, "@", " ", -1), ",", " ", -1))))
+
+ matched := false
+ for _, d := range domainArray {
+ if strings.HasSuffix(user.Email, "@"+d) {
+ matched = true
+ break
+ }
+ }
+
+ return matched
+}
+
func IsVerifyHashRequired(user *model.User, team *model.Team, hash string) bool {
shouldVerifyHash := true
if team.Type == model.TEAM_INVITE && len(team.AllowedDomains) > 0 && len(hash) == 0 && user != nil {
- domains := strings.Fields(strings.TrimSpace(strings.ToLower(strings.Replace(strings.Replace(team.AllowedDomains, "@", " ", -1), ",", " ", -1))))
-
- matched := false
- for _, d := range domains {
- if strings.HasSuffix(user.Email, "@"+d) {
- matched = true
- break
- }
- }
+ matched := CheckUserDomain(user, team.AllowedDomains)
if matched {
shouldVerifyHash = false
diff --git a/model/license.go b/model/license.go
index 7a955e1f8..20e49d668 100644
--- a/model/license.go
+++ b/model/license.go
@@ -1,4 +1,4 @@
-// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
+// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package model
diff --git a/model/license_test.go b/model/license_test.go
new file mode 100644
index 000000000..25c74a2e3
--- /dev/null
+++ b/model/license_test.go
@@ -0,0 +1,34 @@
+// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package model
+
+import (
+ "testing"
+)
+
+func TestLicenseExpired(t *testing.T) {
+ l1 := License{}
+ l1.ExpiresAt = GetMillis() - 1000
+ if !l1.IsExpired() {
+ t.Fatal("license should be expired")
+ }
+
+ l1.ExpiresAt = GetMillis() + 10000
+ if l1.IsExpired() {
+ t.Fatal("license should not be expired")
+ }
+}
+
+func TestLicenseStarted(t *testing.T) {
+ l1 := License{}
+ l1.StartsAt = GetMillis() - 1000
+ if !l1.IsStarted() {
+ t.Fatal("license should be started")
+ }
+
+ l1.StartsAt = GetMillis() + 10000
+ if l1.IsStarted() {
+ t.Fatal("license should not be started")
+ }
+}
diff --git a/utils/license_test.go b/utils/license_test.go
new file mode 100644
index 000000000..826107032
--- /dev/null
+++ b/utils/license_test.go
@@ -0,0 +1,50 @@
+// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package utils
+
+import (
+ "github.com/mattermost/platform/model"
+ "testing"
+)
+
+func TestSetLicense(t *testing.T) {
+ l1 := &model.License{}
+ l1.Features = &model.Features{}
+ l1.Customer = &model.Customer{}
+ l1.StartsAt = model.GetMillis() - 1000
+ l1.ExpiresAt = model.GetMillis() + 100000
+ if ok := SetLicense(l1); !ok {
+ t.Fatal("license should have worked")
+ }
+
+ l2 := &model.License{}
+ l2.Features = &model.Features{}
+ l2.Customer = &model.Customer{}
+ l2.StartsAt = model.GetMillis() - 1000
+ l2.ExpiresAt = model.GetMillis() - 100
+ if ok := SetLicense(l2); ok {
+ t.Fatal("license should have failed")
+ }
+
+ l3 := &model.License{}
+ l3.Features = &model.Features{}
+ l3.Customer = &model.Customer{}
+ l3.StartsAt = model.GetMillis() + 10000
+ l3.ExpiresAt = model.GetMillis() + 100000
+ if ok := SetLicense(l3); ok {
+ t.Fatal("license should have failed")
+ }
+}
+
+func TestValidateLicense(t *testing.T) {
+ b1 := []byte("junk")
+ if ok, _ := ValidateLicense(b1); ok {
+ t.Fatal("should have failed - bad license")
+ }
+
+ b2 := []byte("junkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunk")
+ if ok, _ := ValidateLicense(b2); ok {
+ t.Fatal("should have failed - bad license")
+ }
+}