summaryrefslogtreecommitdiffstats
path: root/model/authorize_test.go
blob: 3f43a4fc3953ac25640e1d79ca44e8a9d0c03745 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package model

import (
	"strings"
	"testing"
)

func TestAuthJson(t *testing.T) {
	a1 := AuthData{}
	a1.ClientId = NewId()
	a1.UserId = NewId()
	a1.Code = NewId()

	json := a1.ToJson()
	ra1 := AuthDataFromJson(strings.NewReader(json))

	if a1.Code != ra1.Code {
		t.Fatal("codes didn't match")
	}

	a2 := AuthorizeRequest{}
	a2.ClientId = NewId()
	a2.Scope = NewId()

	json = a2.ToJson()
	ra2 := AuthorizeRequestFromJson(strings.NewReader(json))

	if a2.ClientId != ra2.ClientId {
		t.Fatal("client ids didn't match")
	}
}

func TestAuthPreSave(t *testing.T) {
	a1 := AuthData{}
	a1.ClientId = NewId()
	a1.UserId = NewId()
	a1.Code = NewId()
	a1.PreSave()
	a1.IsExpired()
}

func TestAuthIsValid(t *testing.T) {

	ad := AuthData{}

	if err := ad.IsValid(); err == nil {
		t.Fatal()
	}

	ad.ClientId = NewRandomString(28)
	if err := ad.IsValid(); err == nil {
		t.Fatal("Should have failed Client Id")
	}

	ad.ClientId = NewId()
	if err := ad.IsValid(); err == nil {
		t.Fatal()
	}

	ad.UserId = NewRandomString(28)
	if err := ad.IsValid(); err == nil {
		t.Fatal("Should have failed User Id")
	}

	ad.UserId = NewId()
	if err := ad.IsValid(); err == nil {
		t.Fatal()
	}

	ad.Code = NewRandomString(129)
	if err := ad.IsValid(); err == nil {
		t.Fatal("Should have failed Code to long")
	}

	ad.Code = ""
	if err := ad.IsValid(); err == nil {
		t.Fatal("Should have failed Code not set")
	}

	ad.Code = NewId()
	if err := ad.IsValid(); err == nil {
		t.Fatal()
	}

	ad.ExpiresIn = 0
	if err := ad.IsValid(); err == nil {
		t.Fatal("Should have failed invalid ExpiresIn")
	}

	ad.ExpiresIn = 1
	if err := ad.IsValid(); err == nil {
		t.Fatal()
	}

	ad.CreateAt = 0
	if err := ad.IsValid(); err == nil {
		t.Fatal("Should have failed Invalid Create At")
	}

	ad.CreateAt = 1
	if err := ad.IsValid(); err == nil {
		t.Fatal()
	}

	ad.State = NewRandomString(129)
	if err := ad.IsValid(); err == nil {
		t.Fatal("Should have failed invalid State")
	}

	ad.State = NewRandomString(128)
	if err := ad.IsValid(); err == nil {
		t.Fatal(err)
	}

	ad.Scope = NewRandomString(129)
	if err := ad.IsValid(); err == nil {
		t.Fatal("Should have failed invalid Scope")
	}

	ad.Scope = NewRandomString(128)
	if err := ad.IsValid(); err == nil {
		t.Fatal()
	}

	ad.RedirectUri = ""
	if err := ad.IsValid(); err == nil {
		t.Fatal("Should have failed Redirect URI not set")
	}

	ad.RedirectUri = NewRandomString(28)
	if err := ad.IsValid(); err == nil {
		t.Fatal("Should have failed invalid URL")
	}

	ad.RedirectUri = NewRandomString(257)
	if err := ad.IsValid(); err == nil {
		t.Fatal("Should have failed invalid URL")
	}

	ad.RedirectUri = "http://example.com"
	if err := ad.IsValid(); err != nil {
		t.Fatal(err)
	}
}