Skip to content
Snippets Groups Projects
Commit c598d076 authored by Niall Sheridan's avatar Niall Sheridan
Browse files

Update authprovider tests

parent 17b17fc8
Branches
Tags
No related merge requests found
...@@ -25,8 +25,10 @@ type Config struct { ...@@ -25,8 +25,10 @@ type Config struct {
whitelist map[string]bool whitelist map[string]bool
} }
var _ auth.Provider = (*Config)(nil)
// New creates a new Github provider from a configuration. // New creates a new Github provider from a configuration.
func New(c *config.Auth) (auth.Provider, error) { func New(c *config.Auth) (*Config, error) {
uw := make(map[string]bool) uw := make(map[string]bool)
for _, u := range c.UsersWhitelist { for _, u := range c.UsersWhitelist {
uw[u] = true uw[u] = true
......
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"testing" "testing"
"github.com/nsheridan/cashier/server/auth"
"github.com/nsheridan/cashier/server/config" "github.com/nsheridan/cashier/server/config"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
...@@ -14,25 +13,48 @@ var ( ...@@ -14,25 +13,48 @@ var (
oauthClientSecret = "secret" oauthClientSecret = "secret"
oauthCallbackURL = "url" oauthCallbackURL = "url"
organization = "exampleorg" organization = "exampleorg"
users = []string{"user"}
) )
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
a := assert.New(t) a := assert.New(t)
p, _ := newGithub() p, _ := New(&config.Auth{
g := p.(*Config) OauthClientID: oauthClientID,
a.Equal(g.config.ClientID, oauthClientID) OauthClientSecret: oauthClientSecret,
a.Equal(g.config.ClientSecret, oauthClientSecret) OauthCallbackURL: oauthCallbackURL,
a.Equal(g.config.RedirectURL, oauthCallbackURL) ProviderOpts: map[string]string{"organization": organization},
a.Equal(g.organization, organization) UsersWhitelist: users,
})
a.Equal(p.config.ClientID, oauthClientID)
a.Equal(p.config.ClientSecret, oauthClientSecret)
a.Equal(p.config.RedirectURL, oauthCallbackURL)
a.Equal(p.organization, organization)
a.Equal(p.whitelist, map[string]bool{"user": true})
} }
func TestNewEmptyOrganization(t *testing.T) { func TestWhitelist(t *testing.T) {
organization = "" c := &config.Auth{
if _, err := newGithub(); err == nil { OauthClientID: oauthClientID,
OauthClientSecret: oauthClientSecret,
OauthCallbackURL: oauthCallbackURL,
ProviderOpts: map[string]string{"organization": ""},
UsersWhitelist: []string{},
}
if _, err := New(c); err == nil {
t.Error("creating a provider without an organization set should return an error") t.Error("creating a provider without an organization set should return an error")
} }
organization = "exampleorg" // Set a user whitelist but no domain
c.UsersWhitelist = users
if _, err := New(c); err != nil {
t.Error("creating a provider with users but no organization should not return an error")
}
// Unset the user whitelist and set a domain
c.UsersWhitelist = []string{}
c.ProviderOpts = map[string]string{"organization": organization}
if _, err := New(c); err != nil {
t.Error("creating a provider with an organization set but without a user whitelist should not return an error")
}
} }
func TestStartSession(t *testing.T) { func TestStartSession(t *testing.T) {
...@@ -45,7 +67,7 @@ func TestStartSession(t *testing.T) { ...@@ -45,7 +67,7 @@ func TestStartSession(t *testing.T) {
a.Contains(s.AuthURL, fmt.Sprintf("client_id=%s", oauthClientID)) a.Contains(s.AuthURL, fmt.Sprintf("client_id=%s", oauthClientID))
} }
func newGithub() (auth.Provider, error) { func newGithub() (*Config, error) {
c := &config.Auth{ c := &config.Auth{
OauthClientID: oauthClientID, OauthClientID: oauthClientID,
OauthClientSecret: oauthClientSecret, OauthClientSecret: oauthClientSecret,
......
...@@ -27,8 +27,10 @@ type Config struct { ...@@ -27,8 +27,10 @@ type Config struct {
whitelist map[string]bool whitelist map[string]bool
} }
var _ auth.Provider = (*Config)(nil)
// New creates a new Google provider from a configuration. // New creates a new Google provider from a configuration.
func New(c *config.Auth) (auth.Provider, error) { func New(c *config.Auth) (*Config, error) {
uw := make(map[string]bool) uw := make(map[string]bool)
for _, u := range c.UsersWhitelist { for _, u := range c.UsersWhitelist {
uw[u] = true uw[u] = true
......
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"testing" "testing"
"github.com/nsheridan/cashier/server/auth"
"github.com/nsheridan/cashier/server/config" "github.com/nsheridan/cashier/server/config"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
...@@ -14,27 +13,42 @@ var ( ...@@ -14,27 +13,42 @@ var (
oauthClientSecret = "secret" oauthClientSecret = "secret"
oauthCallbackURL = "url" oauthCallbackURL = "url"
domain = "example.com" domain = "example.com"
users = []string{"user"}
) )
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
a := assert.New(t) a := assert.New(t)
p, err := newGoogle()
p, _ := newGoogle() a.NoError(err)
g := p.(*Config) a.Equal(p.config.ClientID, oauthClientID)
a.Equal(g.config.ClientID, oauthClientID) a.Equal(p.config.ClientSecret, oauthClientSecret)
a.Equal(g.config.ClientSecret, oauthClientSecret) a.Equal(p.config.RedirectURL, oauthCallbackURL)
a.Equal(g.config.RedirectURL, oauthCallbackURL) a.Equal(p.domain, domain)
a.Equal(g.domain, domain) a.Equal(p.whitelist, map[string]bool{"user": true})
} }
func TestNewWithoutDomain(t *testing.T) { func TestWhitelist(t *testing.T) {
domain = "" c := &config.Auth{
OauthClientID: oauthClientID,
if _, err := newGoogle(); err == nil { OauthClientSecret: oauthClientSecret,
OauthCallbackURL: oauthCallbackURL,
ProviderOpts: map[string]string{"domain": ""},
UsersWhitelist: []string{},
}
if _, err := New(c); err == nil {
t.Error("creating a provider without a domain set should return an error") t.Error("creating a provider without a domain set should return an error")
} }
// Set a user whitelist but no domain
domain = "example.com" c.UsersWhitelist = users
if _, err := New(c); err != nil {
t.Error("creating a provider with users but no domain should not return an error")
}
// Unset the user whitelist and set a domain
c.UsersWhitelist = []string{}
c.ProviderOpts = map[string]string{"domain": domain}
if _, err := New(c); err != nil {
t.Error("creating a provider with a domain set but without a user whitelist should not return an error")
}
} }
func TestStartSession(t *testing.T) { func TestStartSession(t *testing.T) {
...@@ -49,12 +63,13 @@ func TestStartSession(t *testing.T) { ...@@ -49,12 +63,13 @@ func TestStartSession(t *testing.T) {
a.Contains(s.AuthURL, fmt.Sprintf("client_id=%s", oauthClientID)) a.Contains(s.AuthURL, fmt.Sprintf("client_id=%s", oauthClientID))
} }
func newGoogle() (auth.Provider, error) { func newGoogle() (*Config, error) {
c := &config.Auth{ c := &config.Auth{
OauthClientID: oauthClientID, OauthClientID: oauthClientID,
OauthClientSecret: oauthClientSecret, OauthClientSecret: oauthClientSecret,
OauthCallbackURL: oauthCallbackURL, OauthCallbackURL: oauthCallbackURL,
ProviderOpts: map[string]string{"domain": domain}, ProviderOpts: map[string]string{"domain": domain},
UsersWhitelist: users,
} }
return New(c) return New(c)
} }
...@@ -15,8 +15,10 @@ const ( ...@@ -15,8 +15,10 @@ const (
// Config is an implementation of `auth.Provider` for testing. // Config is an implementation of `auth.Provider` for testing.
type Config struct{} type Config struct{}
var _ auth.Provider = (*Config)(nil)
// New creates a new provider. // New creates a new provider.
func New() auth.Provider { func New() *Config {
return &Config{} return &Config{}
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment