github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/model/authorize_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 "github.com/stretchr/testify/require" 11 ) 12 13 func TestAuthJson(t *testing.T) { 14 a1 := AuthData{} 15 a1.ClientId = NewId() 16 a1.UserId = NewId() 17 a1.Code = NewId() 18 19 json := a1.ToJson() 20 ra1 := AuthDataFromJson(strings.NewReader(json)) 21 require.Equal(t, a1.Code, ra1.Code, "codes didn't match") 22 23 a2 := AuthorizeRequest{} 24 a2.ClientId = NewId() 25 a2.Scope = NewId() 26 27 json = a2.ToJson() 28 ra2 := AuthorizeRequestFromJson(strings.NewReader(json)) 29 30 require.Equal(t, a2.ClientId, ra2.ClientId, "client ids didn't match") 31 } 32 33 func TestAuthPreSave(t *testing.T) { 34 a1 := AuthData{} 35 a1.ClientId = NewId() 36 a1.UserId = NewId() 37 a1.Code = NewId() 38 a1.PreSave() 39 a1.IsExpired() 40 } 41 42 func TestAuthIsValid(t *testing.T) { 43 44 ad := AuthData{} 45 46 require.NotNil(t, ad.IsValid()) 47 48 ad.ClientId = NewRandomString(28) 49 require.NotNil(t, ad.IsValid(), "Should have failed Client Id") 50 51 ad.ClientId = NewId() 52 require.NotNil(t, ad.IsValid()) 53 54 ad.UserId = NewRandomString(28) 55 require.NotNil(t, ad.IsValid(), "Should have failed User Id") 56 57 ad.UserId = NewId() 58 require.NotNil(t, ad.IsValid()) 59 60 ad.Code = NewRandomString(129) 61 require.NotNil(t, ad.IsValid(), "Should have failed Code to long") 62 63 ad.Code = "" 64 require.NotNil(t, ad.IsValid(), "Should have failed Code not set") 65 66 ad.Code = NewId() 67 require.NotNil(t, ad.IsValid()) 68 69 ad.ExpiresIn = 0 70 require.NotNil(t, ad.IsValid(), "Should have failed invalid ExpiresIn") 71 72 ad.ExpiresIn = 1 73 require.NotNil(t, ad.IsValid()) 74 75 ad.CreateAt = 0 76 require.NotNil(t, ad.IsValid(), "Should have failed Invalid Create At") 77 78 ad.CreateAt = 1 79 require.NotNil(t, ad.IsValid()) 80 81 ad.State = NewRandomString(129) 82 require.NotNil(t, ad.IsValid(), "Should have failed invalid State") 83 84 ad.State = NewRandomString(128) 85 require.NotNil(t, ad.IsValid()) 86 87 ad.Scope = NewRandomString(1025) 88 require.NotNil(t, ad.IsValid(), "Should have failed invalid Scope") 89 90 ad.Scope = NewRandomString(128) 91 require.NotNil(t, ad.IsValid()) 92 93 ad.RedirectUri = "" 94 require.NotNil(t, ad.IsValid(), "Should have failed Redirect URI not set") 95 96 ad.RedirectUri = NewRandomString(28) 97 require.NotNil(t, ad.IsValid(), "Should have failed invalid URL") 98 99 ad.RedirectUri = NewRandomString(257) 100 require.NotNil(t, ad.IsValid(), "Should have failed invalid URL") 101 102 ad.RedirectUri = "http://example.com" 103 require.Nil(t, ad.IsValid()) 104 }