github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/model/access_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package model 5 6 import ( 7 "strings" 8 "testing" 9 ) 10 11 func TestAccessJson(t *testing.T) { 12 a1 := AccessData{} 13 a1.ClientId = NewId() 14 a1.UserId = NewId() 15 a1.Token = NewId() 16 a1.RefreshToken = NewId() 17 18 json := a1.ToJson() 19 ra1 := AccessDataFromJson(strings.NewReader(json)) 20 21 if a1.Token != ra1.Token { 22 t.Fatal("tokens didn't match") 23 } 24 } 25 26 func TestAccessIsValid(t *testing.T) { 27 ad := AccessData{} 28 29 if err := ad.IsValid(); err == nil { 30 t.Fatal() 31 } 32 33 ad.ClientId = NewRandomString(28) 34 if err := ad.IsValid(); err == nil { 35 t.Fatal("Should have failed Client Id") 36 } 37 38 ad.ClientId = "" 39 if err := ad.IsValid(); err == nil { 40 t.Fatal("Should have failed Client Id") 41 } 42 43 ad.ClientId = NewId() 44 if err := ad.IsValid(); err == nil { 45 t.Fatal() 46 } 47 48 ad.UserId = NewRandomString(28) 49 if err := ad.IsValid(); err == nil { 50 t.Fatal("Should have failed User Id") 51 } 52 53 ad.UserId = "" 54 if err := ad.IsValid(); err == nil { 55 t.Fatal("Should have failed User Id") 56 } 57 58 ad.UserId = NewId() 59 if err := ad.IsValid(); err == nil { 60 t.Fatal("should have failed") 61 } 62 63 ad.Token = NewRandomString(22) 64 if err := ad.IsValid(); err == nil { 65 t.Fatal("Should have failed Token") 66 } 67 68 ad.Token = NewId() 69 if err := ad.IsValid(); err == nil { 70 t.Fatal() 71 } 72 73 ad.RefreshToken = NewRandomString(28) 74 if err := ad.IsValid(); err == nil { 75 t.Fatal("Should have failed Refresh Token") 76 } 77 78 ad.RefreshToken = NewId() 79 if err := ad.IsValid(); err == nil { 80 t.Fatal() 81 } 82 83 ad.RedirectUri = "" 84 if err := ad.IsValid(); err == nil { 85 t.Fatal("Should have failed Redirect URI not set") 86 } 87 88 ad.RedirectUri = NewRandomString(28) 89 if err := ad.IsValid(); err == nil { 90 t.Fatal("Should have failed invalid URL") 91 } 92 93 ad.RedirectUri = "http://example.com" 94 if err := ad.IsValid(); err != nil { 95 t.Fatal(err) 96 } 97 }