summaryrefslogtreecommitdiffstats
path: root/model/access_test.go
blob: 0f124a1072bfc2699d4b3211725e2480d90a8073 (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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package model

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestAccessJson(t *testing.T) {
	a1 := AccessData{}
	a1.ClientId = NewId()
	a1.UserId = NewId()
	a1.Token = NewId()
	a1.RefreshToken = NewId()

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

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

func TestAccessIsValid(t *testing.T) {
	ad := AccessData{}

	require.NotNil(t, ad.IsValid())

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

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

	ad.ClientId = NewId()
	require.NotNil(t, ad.IsValid())

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

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

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

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

	ad.Token = NewId()
	require.NotNil(t, ad.IsValid())

	ad.RefreshToken = NewRandomString(28)
	if err := ad.IsValid(); err == nil {
		t.Fatal("Should have failed Refresh Token")
	}

	ad.RefreshToken = NewId()
	require.NotNil(t, ad.IsValid())

	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 = "http://example.com"
	if err := ad.IsValid(); err != nil {
		t.Fatal(err)
	}
}