2015-01-17 12:42:42 +02:00
|
|
|
package authboss
|
|
|
|
|
2015-01-25 02:07:41 +02:00
|
|
|
import (
|
2017-02-25 02:45:47 +02:00
|
|
|
"bytes"
|
2017-02-21 02:08:19 +02:00
|
|
|
"context"
|
2017-02-24 02:13:25 +02:00
|
|
|
"encoding/json"
|
2017-03-05 20:01:46 +02:00
|
|
|
"fmt"
|
2015-01-25 02:07:41 +02:00
|
|
|
"net/http"
|
2015-08-02 22:00:16 +02:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2017-02-22 01:04:30 +02:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2015-01-25 02:07:41 +02:00
|
|
|
)
|
|
|
|
|
2015-01-17 12:42:42 +02:00
|
|
|
type mockUser struct {
|
|
|
|
Email string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
2017-02-21 01:56:26 +02:00
|
|
|
type mockStoredUser struct {
|
|
|
|
mockUser
|
|
|
|
mockStoreLoader
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockStoreLoader map[string]mockUser
|
2015-01-17 12:42:42 +02:00
|
|
|
|
2017-02-22 01:04:30 +02:00
|
|
|
func (m mockStoreLoader) Load(ctx context.Context, key string) (Storer, error) {
|
|
|
|
u, ok := m[key]
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrUserNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return mockStoredUser{
|
|
|
|
mockUser: u,
|
|
|
|
mockStoreLoader: m,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockStoredUser) Load(ctx context.Context) error {
|
|
|
|
u, ok := m.mockStoreLoader[m.Email]
|
|
|
|
if !ok {
|
|
|
|
return ErrUserNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Email = u.Email
|
|
|
|
m.Password = u.Password
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockStoredUser) Save(ctx context.Context) error {
|
|
|
|
m.mockStoreLoader[m.Email] = m.mockUser
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockStoredUser) PutEmail(ctx context.Context, email string) error {
|
2017-02-21 02:08:19 +02:00
|
|
|
m.Email = email
|
2015-01-17 12:42:42 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-22 01:04:30 +02:00
|
|
|
func (m mockStoredUser) PutUsername(ctx context.Context, username string) error {
|
2017-02-21 02:08:19 +02:00
|
|
|
return errors.New("not impl")
|
2015-01-17 12:42:42 +02:00
|
|
|
}
|
|
|
|
|
2017-02-22 01:04:30 +02:00
|
|
|
func (m mockStoredUser) PutPassword(ctx context.Context, password string) error {
|
2017-02-21 02:08:19 +02:00
|
|
|
m.Password = password
|
|
|
|
return nil
|
2015-01-17 12:42:42 +02:00
|
|
|
}
|
|
|
|
|
2017-02-22 01:04:30 +02:00
|
|
|
func (m mockStoredUser) GetEmail(ctx context.Context) (email string, err error) {
|
|
|
|
return m.Email, nil
|
2015-03-14 01:23:43 +02:00
|
|
|
}
|
|
|
|
|
2017-02-22 01:04:30 +02:00
|
|
|
func (m mockStoredUser) GetUsername(ctx context.Context) (username string, err error) {
|
|
|
|
return "", errors.New("not impl")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockStoredUser) GetPassword(ctx context.Context) (password string, err error) {
|
|
|
|
return m.Password, nil
|
2015-03-14 01:23:43 +02:00
|
|
|
}
|
|
|
|
|
2017-02-25 02:45:47 +02:00
|
|
|
type mockClientStateReadWriter struct {
|
|
|
|
state mockClientState
|
2017-02-24 02:13:25 +02:00
|
|
|
}
|
2015-01-17 12:42:42 +02:00
|
|
|
|
2017-02-25 02:45:47 +02:00
|
|
|
type mockClientState map[string]string
|
|
|
|
|
|
|
|
func newMockClientStateRW(keyValue ...string) mockClientStateReadWriter {
|
|
|
|
state := mockClientState{}
|
|
|
|
for i := 0; i < len(keyValue); i += 2 {
|
|
|
|
key, value := keyValue[i], keyValue[i+1]
|
|
|
|
state[key] = value
|
2017-02-24 02:13:25 +02:00
|
|
|
}
|
2017-02-25 02:45:47 +02:00
|
|
|
|
|
|
|
return mockClientStateReadWriter{state}
|
2017-02-24 02:13:25 +02:00
|
|
|
}
|
|
|
|
|
2017-02-25 02:45:47 +02:00
|
|
|
func (m mockClientStateReadWriter) ReadState(w http.ResponseWriter, r *http.Request) (ClientState, error) {
|
|
|
|
return m.state, nil
|
2015-01-17 12:42:42 +02:00
|
|
|
}
|
2017-02-25 02:45:47 +02:00
|
|
|
|
|
|
|
func (m mockClientStateReadWriter) WriteState(w http.ResponseWriter, cs ClientState, evs []ClientStateEvent) error {
|
|
|
|
var state mockClientState
|
|
|
|
|
|
|
|
if cs != nil {
|
|
|
|
state = cs.(mockClientState)
|
|
|
|
} else {
|
|
|
|
state = mockClientState{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ev := range evs {
|
|
|
|
switch ev.Kind {
|
|
|
|
case ClientStateEventPut:
|
|
|
|
state[ev.Key] = ev.Value
|
|
|
|
case ClientStateEventDel:
|
|
|
|
delete(state, ev.Key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(state)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-02-21 08:02:55 +02:00
|
|
|
}
|
2017-02-25 02:45:47 +02:00
|
|
|
|
|
|
|
w.Header().Set("test_session", string(b))
|
|
|
|
return nil
|
2015-02-21 08:02:55 +02:00
|
|
|
}
|
2015-01-25 02:07:41 +02:00
|
|
|
|
2017-02-25 02:45:47 +02:00
|
|
|
func (m mockClientState) Get(key string) (string, bool) {
|
|
|
|
val, ok := m[key]
|
|
|
|
return val, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockRequest(postKeyValues ...string) *http.Request {
|
2015-08-02 22:00:16 +02:00
|
|
|
urlValues := make(url.Values)
|
2015-01-25 02:07:41 +02:00
|
|
|
for i := 0; i < len(postKeyValues); i += 2 {
|
2015-08-02 22:00:16 +02:00
|
|
|
urlValues.Set(postKeyValues[i], postKeyValues[i+1])
|
2015-01-25 02:07:41 +02:00
|
|
|
}
|
|
|
|
|
2015-08-02 22:00:16 +02:00
|
|
|
req, err := http.NewRequest("POST", "http://localhost", strings.NewReader(urlValues.Encode()))
|
2015-01-25 02:07:41 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
2015-08-02 22:00:16 +02:00
|
|
|
return req
|
2015-01-25 02:07:41 +02:00
|
|
|
}
|
|
|
|
|
2017-02-25 02:45:47 +02:00
|
|
|
func newMockAPIRequest(postKeyValues ...string) *http.Request {
|
|
|
|
kv := map[string]string{}
|
|
|
|
for i := 0; i < len(postKeyValues); i += 2 {
|
|
|
|
key, value := postKeyValues[i], postKeyValues[i+1]
|
|
|
|
kv[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(kv)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "http://localhost", bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2017-02-24 02:13:25 +02:00
|
|
|
type mockRenderLoader struct{}
|
|
|
|
|
|
|
|
func (m mockRenderLoader) Init(names []string) (Renderer, error) {
|
|
|
|
return mockRenderer{}, nil
|
|
|
|
}
|
|
|
|
|
2017-03-05 20:01:46 +02:00
|
|
|
type mockRenderer struct {
|
|
|
|
expectName string
|
|
|
|
}
|
2017-02-24 02:13:25 +02:00
|
|
|
|
|
|
|
func (m mockRenderer) Render(ctx context.Context, name string, data HTMLData) ([]byte, string, error) {
|
2017-03-05 20:01:46 +02:00
|
|
|
if len(m.expectName) != 0 && m.expectName != name {
|
|
|
|
panic(fmt.Sprintf("want template name: %s, but got: %s", m.expectName, name))
|
2017-02-24 02:13:25 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 20:01:46 +02:00
|
|
|
b, err := json.Marshal(data)
|
|
|
|
return b, "application/json", err
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockEmailRenderer struct{}
|
|
|
|
|
|
|
|
func (m mockEmailRenderer) Render(ctx context.Context, name string, data HTMLData) ([]byte, string, error) {
|
|
|
|
switch name {
|
|
|
|
case "text":
|
|
|
|
return []byte("a development text e-mail template"), "text/plain", nil
|
|
|
|
case "html":
|
|
|
|
return []byte("a development html e-mail template"), "text/html", nil
|
|
|
|
default:
|
|
|
|
panic("shouldn't get here")
|
|
|
|
}
|
2017-02-24 02:13:25 +02:00
|
|
|
}
|