2015-03-16 23:42:45 +02:00
|
|
|
// Package mocks defines implemented interfaces for testing modules
|
2015-01-24 02:24:34 +02:00
|
|
|
package mocks
|
|
|
|
|
2015-01-25 08:19:22 +02:00
|
|
|
import (
|
2015-02-12 02:38:09 +02:00
|
|
|
"errors"
|
2015-08-02 20:51:35 +02:00
|
|
|
"io"
|
2015-01-25 08:19:22 +02:00
|
|
|
"net/http"
|
2015-08-02 20:51:35 +02:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2015-02-11 09:03:02 +02:00
|
|
|
"time"
|
2015-01-25 08:19:22 +02:00
|
|
|
|
|
|
|
"gopkg.in/authboss.v0"
|
|
|
|
)
|
2015-01-24 02:24:34 +02:00
|
|
|
|
2015-02-12 02:38:09 +02:00
|
|
|
// MockUser represents all possible fields a authboss User may have
|
2015-01-24 02:24:34 +02:00
|
|
|
type MockUser struct {
|
2015-02-11 09:03:02 +02:00
|
|
|
Username string
|
|
|
|
Email string
|
|
|
|
Password string
|
|
|
|
RecoverToken string
|
|
|
|
RecoverTokenExpiry time.Time
|
2015-02-16 23:27:29 +02:00
|
|
|
ConfirmToken string
|
|
|
|
Confirmed bool
|
2015-02-12 02:38:09 +02:00
|
|
|
Locked bool
|
2015-02-22 22:43:28 +02:00
|
|
|
AttemptNumber int
|
|
|
|
AttemptTime time.Time
|
2015-03-13 11:15:58 +02:00
|
|
|
OauthToken string
|
|
|
|
OauthRefresh string
|
|
|
|
OauthExpiry time.Time
|
2015-01-24 02:24:34 +02:00
|
|
|
}
|
|
|
|
|
2015-02-12 02:38:09 +02:00
|
|
|
// MockStorer should be valid for any module storer defined in authboss.
|
2015-01-24 02:24:34 +02:00
|
|
|
type MockStorer struct {
|
2015-02-10 10:43:45 +02:00
|
|
|
Users map[string]authboss.Attributes
|
|
|
|
Tokens map[string][]string
|
|
|
|
CreateErr string
|
|
|
|
PutErr string
|
|
|
|
GetErr string
|
|
|
|
AddTokenErr string
|
|
|
|
DelTokensErr string
|
|
|
|
UseTokenErr string
|
|
|
|
RecoverUserErr string
|
2015-02-16 06:35:32 +02:00
|
|
|
ConfirmUserErr string
|
2015-01-24 02:24:34 +02:00
|
|
|
}
|
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// NewMockStorer constructor
|
2015-01-24 02:24:34 +02:00
|
|
|
func NewMockStorer() *MockStorer {
|
|
|
|
return &MockStorer{
|
|
|
|
Users: make(map[string]authboss.Attributes),
|
|
|
|
Tokens: make(map[string][]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// Create a new user
|
2015-01-24 02:24:34 +02:00
|
|
|
func (m *MockStorer) Create(key string, attr authboss.Attributes) error {
|
2015-02-12 02:38:09 +02:00
|
|
|
if len(m.CreateErr) > 0 {
|
|
|
|
return errors.New(m.CreateErr)
|
|
|
|
}
|
|
|
|
|
2015-01-24 02:24:34 +02:00
|
|
|
m.Users[key] = attr
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// Put updates to a user
|
2015-01-24 02:24:34 +02:00
|
|
|
func (m *MockStorer) Put(key string, attr authboss.Attributes) error {
|
2015-02-12 02:38:09 +02:00
|
|
|
if len(m.PutErr) > 0 {
|
|
|
|
return errors.New(m.PutErr)
|
|
|
|
}
|
|
|
|
|
2015-01-25 08:19:22 +02:00
|
|
|
if _, ok := m.Users[key]; !ok {
|
|
|
|
m.Users[key] = attr
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for k, v := range attr {
|
|
|
|
m.Users[key][k] = v
|
|
|
|
}
|
2015-01-24 02:24:34 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// Get a user
|
2015-03-15 17:31:48 +02:00
|
|
|
func (m *MockStorer) Get(key string) (result interface{}, err error) {
|
2015-02-12 02:38:09 +02:00
|
|
|
if len(m.GetErr) > 0 {
|
|
|
|
return nil, errors.New(m.GetErr)
|
2015-02-09 09:08:33 +02:00
|
|
|
}
|
|
|
|
|
2015-02-12 02:38:09 +02:00
|
|
|
userAttrs, ok := m.Users[key]
|
|
|
|
if !ok {
|
|
|
|
return nil, authboss.ErrUserNotFound
|
2015-02-09 09:08:33 +02:00
|
|
|
}
|
|
|
|
|
2015-02-12 02:38:09 +02:00
|
|
|
u := &MockUser{}
|
2015-02-26 22:15:33 +02:00
|
|
|
if err := userAttrs.Bind(u, true); err != nil {
|
2015-02-12 02:38:09 +02:00
|
|
|
panic(err)
|
2015-02-09 09:08:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
2015-01-24 02:24:34 +02:00
|
|
|
}
|
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// PutOAuth user
|
2015-03-14 01:23:43 +02:00
|
|
|
func (m *MockStorer) PutOAuth(uid, provider string, attr authboss.Attributes) error {
|
|
|
|
if len(m.PutErr) > 0 {
|
|
|
|
return errors.New(m.PutErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := m.Users[uid+provider]; !ok {
|
|
|
|
m.Users[uid+provider] = attr
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for k, v := range attr {
|
|
|
|
m.Users[uid+provider][k] = v
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// GetOAuth user
|
2015-03-15 17:31:48 +02:00
|
|
|
func (m *MockStorer) GetOAuth(uid, provider string) (result interface{}, err error) {
|
2015-03-14 01:23:43 +02:00
|
|
|
if len(m.GetErr) > 0 {
|
|
|
|
return nil, errors.New(m.GetErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
userAttrs, ok := m.Users[uid+provider]
|
|
|
|
if !ok {
|
|
|
|
return nil, authboss.ErrUserNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
u := &MockUser{}
|
|
|
|
if err := userAttrs.Bind(u, true); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// AddToken for remember me
|
2015-01-24 02:24:34 +02:00
|
|
|
func (m *MockStorer) AddToken(key, token string) error {
|
2015-02-12 02:38:09 +02:00
|
|
|
if len(m.AddTokenErr) > 0 {
|
|
|
|
return errors.New(m.AddTokenErr)
|
|
|
|
}
|
|
|
|
|
2015-01-24 02:24:34 +02:00
|
|
|
arr := m.Tokens[key]
|
|
|
|
m.Tokens[key] = append(arr, token)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// DelTokens for a user
|
2015-01-24 02:24:34 +02:00
|
|
|
func (m *MockStorer) DelTokens(key string) error {
|
2015-02-12 02:38:09 +02:00
|
|
|
if len(m.DelTokensErr) > 0 {
|
|
|
|
return errors.New(m.DelTokensErr)
|
|
|
|
}
|
|
|
|
|
2015-01-24 02:24:34 +02:00
|
|
|
delete(m.Tokens, key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// UseToken if it exists, deleting it in the process
|
2015-03-16 00:00:37 +02:00
|
|
|
func (m *MockStorer) UseToken(givenKey, token string) (err error) {
|
2015-02-12 02:38:09 +02:00
|
|
|
if len(m.UseTokenErr) > 0 {
|
2015-03-16 00:00:37 +02:00
|
|
|
return errors.New(m.UseTokenErr)
|
2015-02-12 02:38:09 +02:00
|
|
|
}
|
|
|
|
|
2015-01-24 02:24:34 +02:00
|
|
|
if arr, ok := m.Tokens[givenKey]; ok {
|
|
|
|
for _, tok := range arr {
|
|
|
|
if tok == token {
|
2015-03-16 00:00:37 +02:00
|
|
|
return nil
|
2015-01-24 02:24:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 00:00:37 +02:00
|
|
|
return authboss.ErrTokenNotFound
|
2015-01-24 02:24:34 +02:00
|
|
|
}
|
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// RecoverUser by the token.
|
2015-02-11 09:03:02 +02:00
|
|
|
func (m *MockStorer) RecoverUser(token string) (result interface{}, err error) {
|
2015-02-12 02:38:09 +02:00
|
|
|
if len(m.RecoverUserErr) > 0 {
|
|
|
|
return nil, errors.New(m.RecoverUserErr)
|
|
|
|
}
|
|
|
|
|
2015-02-11 09:03:02 +02:00
|
|
|
for _, user := range m.Users {
|
|
|
|
if user["recover_token"] == token {
|
|
|
|
|
2015-02-12 02:38:09 +02:00
|
|
|
u := &MockUser{}
|
2015-02-26 09:39:55 +02:00
|
|
|
if err = user.Bind(u, false); err != nil {
|
2015-02-12 02:38:09 +02:00
|
|
|
panic(err)
|
2015-02-11 09:03:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-16 06:35:32 +02:00
|
|
|
return nil, authboss.ErrUserNotFound
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// ConfirmUser via their token
|
2015-02-16 06:35:32 +02:00
|
|
|
func (m *MockStorer) ConfirmUser(confirmToken string) (result interface{}, err error) {
|
|
|
|
if len(m.ConfirmUserErr) > 0 {
|
|
|
|
return nil, errors.New(m.ConfirmUserErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, user := range m.Users {
|
|
|
|
if user["confirm_token"] == confirmToken {
|
|
|
|
|
|
|
|
u := &MockUser{}
|
2015-02-26 09:39:55 +02:00
|
|
|
if err = user.Bind(u, false); err != nil {
|
2015-02-16 06:35:32 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 09:03:02 +02:00
|
|
|
return nil, authboss.ErrUserNotFound
|
2015-02-05 20:30:57 +02:00
|
|
|
}
|
|
|
|
|
2015-02-12 02:38:09 +02:00
|
|
|
// MockFailStorer is used for testing module initialize functions that recover more than the base storer
|
|
|
|
type MockFailStorer struct{}
|
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// Create fails
|
2015-02-12 02:38:09 +02:00
|
|
|
func (_ MockFailStorer) Create(_ string, _ authboss.Attributes) error {
|
|
|
|
return errors.New("fail storer: create")
|
|
|
|
}
|
2015-03-16 23:42:45 +02:00
|
|
|
|
|
|
|
// Put fails
|
2015-02-12 02:38:09 +02:00
|
|
|
func (_ MockFailStorer) Put(_ string, _ authboss.Attributes) error {
|
|
|
|
return errors.New("fail storer: put")
|
|
|
|
}
|
2015-03-16 23:42:45 +02:00
|
|
|
|
|
|
|
// Get fails
|
2015-03-15 17:31:48 +02:00
|
|
|
func (_ MockFailStorer) Get(_ string) (interface{}, error) {
|
2015-02-12 02:38:09 +02:00
|
|
|
return nil, errors.New("fail storer: get")
|
|
|
|
}
|
|
|
|
|
|
|
|
// MockClientStorer is used for testing the client stores on context
|
|
|
|
type MockClientStorer struct {
|
|
|
|
Values map[string]string
|
|
|
|
GetShouldFail bool
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// NewMockClientStorer constructs a MockClientStorer
|
2015-02-25 01:01:56 +02:00
|
|
|
func NewMockClientStorer(data ...string) *MockClientStorer {
|
|
|
|
if len(data)%2 != 0 {
|
|
|
|
panic("It should be a key value list of arguments.")
|
2015-02-12 02:38:09 +02:00
|
|
|
}
|
2015-02-25 01:01:56 +02:00
|
|
|
|
|
|
|
values := make(map[string]string)
|
|
|
|
|
|
|
|
for i := 0; i < len(data)-1; i += 2 {
|
|
|
|
values[data[i]] = data[i+1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return &MockClientStorer{Values: values}
|
2015-02-12 02:38:09 +02:00
|
|
|
}
|
2015-01-24 02:24:34 +02:00
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// Get a key's value
|
2015-02-12 02:38:09 +02:00
|
|
|
func (m *MockClientStorer) Get(key string) (string, bool) {
|
|
|
|
if m.GetShouldFail {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
v, ok := m.Values[key]
|
2015-01-24 02:24:34 +02:00
|
|
|
return v, ok
|
|
|
|
}
|
2015-03-16 23:42:45 +02:00
|
|
|
|
|
|
|
// GetErr gets a key's value or err if not exist
|
2015-02-21 08:02:55 +02:00
|
|
|
func (m *MockClientStorer) GetErr(key string) (string, error) {
|
|
|
|
if m.GetShouldFail {
|
|
|
|
return "", authboss.ClientDataErr{key}
|
|
|
|
}
|
|
|
|
|
|
|
|
v, ok := m.Values[key]
|
|
|
|
if !ok {
|
|
|
|
return v, authboss.ClientDataErr{key}
|
|
|
|
}
|
2015-02-21 21:07:32 +02:00
|
|
|
return v, nil
|
2015-02-21 08:02:55 +02:00
|
|
|
}
|
2015-03-16 23:42:45 +02:00
|
|
|
|
|
|
|
// Put a value
|
2015-02-12 02:38:09 +02:00
|
|
|
func (m *MockClientStorer) Put(key, val string) { m.Values[key] = val }
|
2015-03-16 23:42:45 +02:00
|
|
|
|
|
|
|
// Del a key/value pair
|
|
|
|
func (m *MockClientStorer) Del(key string) { delete(m.Values, key) }
|
2015-01-25 08:19:22 +02:00
|
|
|
|
2015-08-02 20:51:35 +02:00
|
|
|
// MockRequest returns a new mock request with optional key-value body (form-post)
|
|
|
|
func MockRequest(method string, postKeyValues ...string) *http.Request {
|
|
|
|
var body io.Reader
|
|
|
|
|
|
|
|
if len(postKeyValues) > 0 {
|
|
|
|
urlValues := make(url.Values)
|
|
|
|
for i := 0; i < len(postKeyValues); i += 2 {
|
|
|
|
urlValues.Set(postKeyValues[i], postKeyValues[i+1])
|
2015-01-25 08:19:22 +02:00
|
|
|
}
|
2015-08-02 20:51:35 +02:00
|
|
|
body = strings.NewReader(urlValues.Encode())
|
2015-01-25 08:19:22 +02:00
|
|
|
}
|
|
|
|
|
2015-08-02 20:51:35 +02:00
|
|
|
req, err := http.NewRequest(method, "http://localhost", body)
|
2015-01-25 08:19:22 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
|
2015-08-02 20:51:35 +02:00
|
|
|
if len(postKeyValues) > 0 {
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
2015-01-25 08:19:22 +02:00
|
|
|
}
|
|
|
|
|
2015-08-02 20:51:35 +02:00
|
|
|
return req
|
2015-01-25 08:19:22 +02:00
|
|
|
}
|
2015-02-09 09:08:33 +02:00
|
|
|
|
2015-02-12 02:38:09 +02:00
|
|
|
// MockMailer helps simplify mailer testing by storing the last sent email
|
2015-02-09 09:08:33 +02:00
|
|
|
type MockMailer struct {
|
2015-02-12 02:38:09 +02:00
|
|
|
Last authboss.Email
|
|
|
|
SendErr string
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// NewMockMailer constructs a mock mailer
|
2015-02-12 02:38:09 +02:00
|
|
|
func NewMockMailer() *MockMailer {
|
|
|
|
return &MockMailer{}
|
2015-02-09 09:08:33 +02:00
|
|
|
}
|
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// Send an e-mail
|
2015-02-09 09:08:33 +02:00
|
|
|
func (m *MockMailer) Send(email authboss.Email) error {
|
2015-02-12 02:38:09 +02:00
|
|
|
if len(m.SendErr) > 0 {
|
|
|
|
return errors.New(m.SendErr)
|
|
|
|
}
|
|
|
|
|
2015-02-09 09:08:33 +02:00
|
|
|
m.Last = email
|
|
|
|
return nil
|
|
|
|
}
|
2015-02-25 01:01:56 +02:00
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// MockAfterCallback is a callback that knows if it was called
|
2015-02-25 01:01:56 +02:00
|
|
|
type MockAfterCallback struct {
|
|
|
|
HasBeenCalled bool
|
|
|
|
Fn authboss.After
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// NewMockAfterCallback constructs a new mockaftercallback.
|
2015-02-25 01:01:56 +02:00
|
|
|
func NewMockAfterCallback() *MockAfterCallback {
|
|
|
|
m := MockAfterCallback{}
|
|
|
|
|
|
|
|
m.Fn = func(_ *authboss.Context) error {
|
|
|
|
m.HasBeenCalled = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &m
|
|
|
|
}
|