1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-01-12 04:23:16 +02:00
authboss/lock/lock_test.go

229 lines
5.6 KiB
Go
Raw Normal View History

package lock
import (
"testing"
"time"
2016-12-20 08:43:51 +02:00
"github.com/go-authboss/authboss"
"github.com/go-authboss/authboss/internal/mocks"
)
func TestStorage(t *testing.T) {
2015-04-01 00:27:47 +02:00
t.Parallel()
l := &Lock{authboss.New()}
storage := l.Storage()
if _, ok := storage[StoreAttemptNumber]; !ok {
t.Error("Expected attempt number storage option.")
}
if _, ok := storage[StoreAttemptTime]; !ok {
t.Error("Expected attempt number time option.")
}
if _, ok := storage[StoreLocked]; !ok {
t.Error("Expected locked storage option.")
}
}
func TestBeforeAuth(t *testing.T) {
2015-04-01 00:27:47 +02:00
t.Parallel()
l := &Lock{}
2015-04-01 00:27:47 +02:00
ab := authboss.New()
ctx := ab.NewContext()
2015-03-16 23:42:45 +02:00
if interrupt, err := l.beforeAuth(ctx); err != errUserMissing {
2015-02-22 22:55:09 +02:00
t.Error("Expected an error because of missing user:", err)
2015-02-22 22:43:28 +02:00
} else if interrupt != authboss.InterruptNone {
t.Error("Interrupt should not be set:", interrupt)
}
ctx.User = authboss.Attributes{"locked": time.Now().Add(1 * time.Hour)}
2015-03-16 23:42:45 +02:00
if interrupt, err := l.beforeAuth(ctx); err != nil {
2015-02-22 22:43:28 +02:00
t.Error(err)
} else if interrupt != authboss.InterruptAccountLocked {
t.Error("Expected a locked interrupt:", interrupt)
}
}
func TestAfterAuth(t *testing.T) {
2015-04-01 00:27:47 +02:00
t.Parallel()
ab := authboss.New()
lock := Lock{}
2015-04-01 00:27:47 +02:00
ctx := ab.NewContext()
2015-03-16 23:42:45 +02:00
if err := lock.afterAuth(ctx); err != errUserMissing {
2015-02-22 22:55:09 +02:00
t.Error("Expected an error because of missing user:", err)
}
storer := mocks.NewMockStorer()
2015-04-01 00:27:47 +02:00
ab.Storer = storer
ctx.User = authboss.Attributes{ab.PrimaryID: "john@john.com"}
2015-03-16 23:42:45 +02:00
if err := lock.afterAuth(ctx); err != nil {
2015-02-22 22:43:28 +02:00
t.Error(err)
}
if storer.Users["john@john.com"][StoreAttemptNumber].(int64) != int64(0) {
t.Error("StoreAttemptNumber set incorrectly.")
}
2015-02-22 23:16:11 +02:00
if _, ok := storer.Users["john@john.com"][StoreAttemptTime].(time.Time); !ok {
t.Error("StoreAttemptTime not set.")
}
}
func TestAfterAuthFail_Lock(t *testing.T) {
2015-04-01 00:27:47 +02:00
t.Parallel()
ab := authboss.New()
var old, current time.Time
var ok bool
2015-04-01 00:27:47 +02:00
ctx := ab.NewContext()
storer := mocks.NewMockStorer()
2015-04-01 00:27:47 +02:00
ab.Storer = storer
lock := Lock{ab}
ab.LockWindow = 30 * time.Minute
ab.LockDuration = 30 * time.Minute
ab.LockAfter = 3
2015-02-22 23:16:11 +02:00
email := "john@john.com"
2015-04-01 00:27:47 +02:00
ctx.User = map[string]interface{}{ab.PrimaryID: email}
old = time.Now().UTC().Add(-1 * time.Hour)
for i := 0; i < 3; i++ {
2015-02-22 23:16:11 +02:00
if lockedIntf, ok := storer.Users["john@john.com"][StoreLocked]; ok && lockedIntf.(bool) {
t.Errorf("%d: User should not be locked.", i)
}
2015-03-16 23:42:45 +02:00
if err := lock.afterAuthFail(ctx); err != nil {
2015-02-22 22:43:28 +02:00
t.Error(err)
}
if val := storer.Users[email][StoreAttemptNumber].(int64); val != int64(i+1) {
t.Errorf("%d: StoreAttemptNumber set incorrectly: %v", i, val)
}
2015-02-22 23:16:11 +02:00
if current, ok = storer.Users[email][StoreAttemptTime].(time.Time); !ok || old.After(current) {
2015-03-16 23:51:44 +02:00
t.Errorf("%d: StoreAttemptTime not set correctly: %v", i, current)
}
current = old
}
if locked := storer.Users[email][StoreLocked].(time.Time); !locked.After(time.Now()) {
t.Error("User should be locked for some duration:", locked)
}
if val := storer.Users[email][StoreAttemptNumber].(int64); val != int64(3) {
t.Error("StoreAttemptNumber set incorrectly:", val)
}
2015-02-22 23:16:11 +02:00
if _, ok = storer.Users[email][StoreAttemptTime].(time.Time); !ok {
t.Error("StoreAttemptTime not set correctly.")
}
}
func TestAfterAuthFail_Reset(t *testing.T) {
2015-04-01 00:27:47 +02:00
t.Parallel()
ab := authboss.New()
var old, current time.Time
var ok bool
2015-04-01 00:27:47 +02:00
ctx := ab.NewContext()
storer := mocks.NewMockStorer()
2015-04-01 00:27:47 +02:00
lock := Lock{ab}
ab.LockWindow = 30 * time.Minute
ab.Storer = storer
old = time.Now().UTC().Add(-time.Hour)
2015-02-22 23:16:11 +02:00
email := "john@john.com"
ctx.User = map[string]interface{}{
2015-04-01 00:27:47 +02:00
ab.PrimaryID: email,
StoreAttemptNumber: int64(2),
StoreAttemptTime: old,
StoreLocked: old,
}
2015-03-16 23:42:45 +02:00
lock.afterAuthFail(ctx)
if val := storer.Users[email][StoreAttemptNumber].(int64); val != int64(1) {
t.Error("StoreAttemptNumber set incorrectly:", val)
}
2015-02-22 23:16:11 +02:00
if current, ok = storer.Users[email][StoreAttemptTime].(time.Time); !ok || current.Before(old) {
t.Error("StoreAttemptTime not set correctly.")
}
if locked := storer.Users[email][StoreLocked].(time.Time); locked.After(time.Now()) {
t.Error("StoreLocked not set correctly:", locked)
}
}
func TestAfterAuthFail_Errors(t *testing.T) {
2015-04-01 00:27:47 +02:00
t.Parallel()
ab := authboss.New()
lock := Lock{ab}
ctx := ab.NewContext()
2015-03-16 23:42:45 +02:00
lock.afterAuthFail(ctx)
if _, ok := ctx.User[StoreAttemptNumber]; ok {
t.Error("Expected nothing to be set, missing user.")
}
}
func TestLock(t *testing.T) {
2015-04-01 00:27:47 +02:00
t.Parallel()
ab := authboss.New()
storer := mocks.NewMockStorer()
2015-04-01 00:27:47 +02:00
ab.Storer = storer
lock := Lock{ab}
2015-02-22 23:16:11 +02:00
email := "john@john.com"
storer.Users[email] = map[string]interface{}{
2015-04-01 00:27:47 +02:00
ab.PrimaryID: email,
"password": "password",
}
2015-02-22 23:16:11 +02:00
err := lock.Lock(email)
if err != nil {
t.Error(err)
}
if locked := storer.Users[email][StoreLocked].(time.Time); !locked.After(time.Now()) {
t.Error("User should be locked.")
}
}
func TestUnlock(t *testing.T) {
2015-04-01 00:27:47 +02:00
t.Parallel()
ab := authboss.New()
storer := mocks.NewMockStorer()
2015-04-01 00:27:47 +02:00
ab.Storer = storer
lock := Lock{ab}
ab.LockWindow = 1 * time.Hour
2015-02-22 23:16:11 +02:00
email := "john@john.com"
storer.Users[email] = map[string]interface{}{
2015-04-01 00:27:47 +02:00
ab.PrimaryID: email,
"password": "password",
"locked": true,
}
2015-02-22 23:16:11 +02:00
err := lock.Unlock(email)
if err != nil {
t.Error(err)
}
2015-02-22 23:16:11 +02:00
attemptTime := storer.Users[email][StoreAttemptTime].(time.Time)
2015-04-01 00:27:47 +02:00
if attemptTime.After(time.Now().UTC().Add(-ab.LockWindow)) {
t.Error("StoreLocked not set correctly:", attemptTime)
}
if number := storer.Users[email][StoreAttemptNumber].(int64); number != int64(0) {
t.Error("StoreLocked not set correctly:", number)
}
if locked := storer.Users[email][StoreLocked].(time.Time); locked.After(time.Now()) {
t.Error("User should not be locked.")
}
}