aboutsummaryrefslogtreecommitdiff
path: root/server/auth/gitlab/gitlab_test.go
blob: 39c2701245372b0775778a20e9afce28ab36706b (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
package gitlab

import (
	"fmt"
	"testing"

	"github.com/nsheridan/cashier/server/auth"
	"github.com/nsheridan/cashier/server/config"
	"github.com/stretchr/testify/assert"
)

var (
	oauthClientID     = "id"
	oauthClientSecret = "secret"
	oauthCallbackURL  = "url"
	allusers          = ""
	siteurl           = "https://exampleorg/"
	group             = "exampleorg"
)

func TestNew(t *testing.T) {
	a := assert.New(t)

	p, _ := newGitlab()
	g := p.(*Config)
	a.Equal(g.config.ClientID, oauthClientID)
	a.Equal(g.config.ClientSecret, oauthClientSecret)
	a.Equal(g.config.RedirectURL, oauthCallbackURL)
}

func TestNewBrokenSiteURL(t *testing.T) {
	siteurl = "https://exampleorg"
	a := assert.New(t)

	_, err := newGitlab()
	a.EqualError(err, "gitlab_opts siteurl must end in /")

	siteurl = "https://exampleorg/"
}

func TestBadAllUsers(t *testing.T) {
	allusers = "true"
	siteurl = ""
	a := assert.New(t)

	_, err := newGitlab()
	a.EqualError(err, "gitlab_opts if allusers is set, siteurl must be set")

	allusers = ""
	siteurl = "https://exampleorg/"
}

func TestGoodAllUsers(t *testing.T) {
	allusers = "true"
	a := assert.New(t)

	p, _ := newGitlab()
	s := p.StartSession("test_state")
	a.Contains(s.AuthURL, "exampleorg/oauth/authorize")
	a.Contains(s.AuthURL, "state=test_state")
	a.Contains(s.AuthURL, fmt.Sprintf("client_id=%s", oauthClientID))

	allusers = ""
}

func TestNewEmptyGroupList(t *testing.T) {
	group = ""
	a := assert.New(t)

	_, err := newGitlab()
	a.EqualError(err, "gitlab_opts group and the users whitelist must not be both empty if allusers isn't true")

	group = "exampleorg"
}

func TestStartSession(t *testing.T) {
	a := assert.New(t)

	p, _ := newGitlab()
	s := p.StartSession("test_state")
	a.Contains(s.AuthURL, "exampleorg/oauth/authorize")
	a.Contains(s.AuthURL, "state=test_state")
	a.Contains(s.AuthURL, fmt.Sprintf("client_id=%s", oauthClientID))
}

func newGitlab() (auth.Provider, error) {
	c := &config.Auth{
		OauthClientID:     oauthClientID,
		OauthClientSecret: oauthClientSecret,
		OauthCallbackURL:  oauthCallbackURL,
		ProviderOpts: map[string]string{
			"group":    group,
			"siteurl":  siteurl,
			"allusers": allusers,
		},
	}
	return New(c)
}