本文整理汇总了Golang中gopkg/in/authboss/v0/internal/mocks.NewMockStorer函数的典型用法代码示例。如果您正苦于以下问题:Golang NewMockStorer函数的具体用法?Golang NewMockStorer怎么用?Golang NewMockStorer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewMockStorer函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestUnlock
func TestUnlock(t *testing.T) {
t.Parallel()
ab := authboss.New()
storer := mocks.NewMockStorer()
ab.Storer = storer
lock := Lock{ab}
ab.LockWindow = 1 * time.Hour
email := "[email protected]"
storer.Users[email] = map[string]interface{}{
ab.PrimaryID: email,
"password": "password",
"locked": true,
}
err := lock.Unlock(email)
if err != nil {
t.Error(err)
}
attemptTime := storer.Users[email][StoreAttemptTime].(time.Time)
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.")
}
}
示例2: TestAfterAuthFail_Reset
func TestAfterAuthFail_Reset(t *testing.T) {
t.Parallel()
ab := authboss.New()
var old, current time.Time
var ok bool
ctx := ab.NewContext()
storer := mocks.NewMockStorer()
lock := Lock{ab}
ab.LockWindow = 30 * time.Minute
ab.Storer = storer
old = time.Now().UTC().Add(-time.Hour)
email := "[email protected]"
ctx.User = map[string]interface{}{
ab.PrimaryID: email,
StoreAttemptNumber: int64(2),
StoreAttemptTime: old,
StoreLocked: old,
}
lock.afterAuthFail(ctx)
if val := storer.Users[email][StoreAttemptNumber].(int64); val != int64(0) {
t.Error("StoreAttemptNumber set incorrectly:", val)
}
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)
}
}
示例3: TestAfterOAuth
func TestAfterOAuth(t *testing.T) {
t.Parallel()
r := Remember{authboss.New()}
storer := mocks.NewMockStorer()
r.Storer = storer
cookies := mocks.NewMockClientStorer()
session := mocks.NewMockClientStorer(authboss.SessionOAuth2Params, `{"rm":"true"}`)
ctx := r.NewContext()
ctx.SessionStorer = session
ctx.CookieStorer = cookies
ctx.User = authboss.Attributes{
authboss.StoreOAuth2UID: "uid",
authboss.StoreOAuth2Provider: "google",
}
if err := r.afterOAuth(ctx); err != nil {
t.Error(err)
}
if _, ok := cookies.Values[authboss.CookieRemember]; !ok {
t.Error("Expected a cookie to have been set.")
}
}
示例4: TestAfterPasswordReset
func TestAfterPasswordReset(t *testing.T) {
t.Parallel()
r := Remember{authboss.New()}
id := "[email protected]"
storer := mocks.NewMockStorer()
r.Storer = storer
session := mocks.NewMockClientStorer()
cookies := mocks.NewMockClientStorer()
storer.Tokens[id] = []string{"one", "two"}
cookies.Values[authboss.CookieRemember] = "token"
ctx := r.NewContext()
ctx.User = authboss.Attributes{r.PrimaryID: id}
ctx.SessionStorer = session
ctx.CookieStorer = cookies
if err := r.afterPassword(ctx); err != nil {
t.Error(err)
}
if _, ok := cookies.Values[authboss.CookieRemember]; ok {
t.Error("Expected the remember cookie to be deleted.")
}
if len(storer.Tokens) != 0 {
t.Error("Should have wiped out all tokens.")
}
}
示例5: TestAuth_validateCredentials
func TestAuth_validateCredentials(t *testing.T) {
t.Parallel()
ab := authboss.New()
storer := mocks.NewMockStorer()
ab.Storer = storer
ctx := ab.NewContext()
storer.Users["john"] = authboss.Attributes{"password": "$2a$10$pgFsuQwdhwOdZp/v52dvHeEi53ZaI7dGmtwK4bAzGGN5A4nT6doqm"}
if _, err := validateCredentials(ctx, "john", "a"); err != nil {
t.Error("Unexpected error:", err)
}
ctx = ab.NewContext()
if valid, err := validateCredentials(ctx, "jake", "a"); err != nil {
t.Error("Expect no error when user not found:", err)
} else if valid {
t.Error("Expect invalid when not user found")
}
ctx = ab.NewContext()
storer.GetErr = "Failed to load user"
if _, err := validateCredentials(ctx, "", ""); err.Error() != "Failed to load user" {
t.Error("Unexpected error:", err)
}
}
示例6: TestNew
func TestNew(t *testing.T) {
t.Parallel()
r := &Remember{authboss.New()}
storer := mocks.NewMockStorer()
r.Storer = storer
cookies := mocks.NewMockClientStorer()
key := "tester"
token, err := r.new(cookies, key)
if err != nil {
t.Error("Unexpected error:", err)
}
if len(token) == 0 {
t.Error("Expected a token.")
}
if tok, ok := storer.Tokens[key]; !ok {
t.Error("Expected it to store against the key:", key)
} else if len(tok) != 1 || len(tok[0]) == 0 {
t.Error("Expected a token to be saved.")
}
if token != cookies.Values[authboss.CookieRemember] {
t.Error("Expected a cookie set with the token.")
}
}
示例7: TestAfterAuth
func TestAfterAuth(t *testing.T) {
t.Parallel()
ab := authboss.New()
lock := Lock{}
ctx := ab.NewContext()
if err := lock.afterAuth(ctx); err != errUserMissing {
t.Error("Expected an error because of missing user:", err)
}
storer := mocks.NewMockStorer()
ab.Storer = storer
ctx.User = authboss.Attributes{ab.PrimaryID: "[email protected]"}
if err := lock.afterAuth(ctx); err != nil {
t.Error(err)
}
if storer.Users["[email protected]"][StoreAttemptNumber].(int64) != int64(0) {
t.Error("StoreAttemptNumber set incorrectly.")
}
if _, ok := storer.Users["[email protected]"][StoreAttemptTime].(time.Time); !ok {
t.Error("StoreAttemptTime not set.")
}
}
示例8: TestAfterAuth
func TestAfterAuth(t *testing.T) {
t.Parallel()
r := Remember{authboss.New()}
storer := mocks.NewMockStorer()
r.Storer = storer
cookies := mocks.NewMockClientStorer()
session := mocks.NewMockClientStorer()
req, err := http.NewRequest("POST", "http://localhost", bytes.NewBufferString("rm=true"))
if err != nil {
t.Error("Unexpected Error:", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
ctx := r.NewContext()
ctx.SessionStorer = session
ctx.CookieStorer = cookies
ctx.User = authboss.Attributes{r.PrimaryID: "[email protected]"}
ctx.Values = map[string]string{authboss.CookieRemember: "true"}
if err := r.afterAuth(ctx); err != nil {
t.Error(err)
}
if _, ok := cookies.Values[authboss.CookieRemember]; !ok {
t.Error("Expected a cookie to have been set.")
}
}
示例9: TestConfirm_Confirm
func TestConfirm_Confirm(t *testing.T) {
t.Parallel()
c := setup()
ctx := c.NewContext()
log := &bytes.Buffer{}
c.LogWriter = log
c.PrimaryID = authboss.StoreUsername
c.Mailer = authboss.LogMailer(log)
// Create a token
token := []byte("hi")
sum := md5.Sum(token)
// Create the "database"
storer := mocks.NewMockStorer()
c.Storer = storer
user := authboss.Attributes{
authboss.StoreUsername: "usern",
StoreConfirmToken: base64.StdEncoding.EncodeToString(sum[:]),
}
storer.Users["usern"] = user
// Make a request with session and context support.
r, _ := http.NewRequest("GET", "http://localhost?cnf="+base64.URLEncoding.EncodeToString(token), nil)
w := httptest.NewRecorder()
ctx = c.NewContext()
ctx.CookieStorer = mocks.NewMockClientStorer()
session := mocks.NewMockClientStorer()
ctx.User = user
ctx.SessionStorer = session
c.confirmHandler(ctx, w, r)
if w.Code != http.StatusFound {
t.Error("Expected a redirect after success:", w.Code)
}
if log.Len() != 0 {
t.Error("Expected a clean log on success:", log.String())
}
is, ok := ctx.User.Bool(StoreConfirmed)
if !ok || !is {
t.Error("The user should be confirmed.")
}
tok, ok := ctx.User.String(StoreConfirmToken)
if ok && len(tok) != 0 {
t.Error("Confirm token should have been wiped out.")
}
if key, ok := ctx.SessionStorer.Get(authboss.SessionKey); !ok || len(key) == 0 {
t.Error("Should have logged the user in.")
}
if success, ok := ctx.SessionStorer.Get(authboss.FlashSuccessKey); !ok || len(success) == 0 {
t.Error("Should have left a nice message.")
}
}
示例10: TestInitialize
func TestInitialize(t *testing.T) {
t.Parallel()
ab := authboss.New()
ab.OAuth2Storer = mocks.NewMockStorer()
o := OAuth2{}
if err := o.Initialize(ab); err != nil {
t.Error(err)
}
}
示例11: setup
func setup() *Confirm {
ab := authboss.New()
ab.Storer = mocks.NewMockStorer()
ab.LayoutHTMLEmail = template.Must(template.New("").Parse(`email ^_^`))
ab.LayoutTextEmail = template.Must(template.New("").Parse(`email`))
c := &Confirm{}
if err := c.Initialize(ab); err != nil {
panic(err)
}
return c
}
示例12: TestAfterAuthFail_Lock
func TestAfterAuthFail_Lock(t *testing.T) {
t.Parallel()
ab := authboss.New()
var old, current time.Time
var ok bool
ctx := ab.NewContext()
storer := mocks.NewMockStorer()
ab.Storer = storer
lock := Lock{ab}
ab.LockWindow = 30 * time.Minute
ab.LockDuration = 30 * time.Minute
ab.LockAfter = 3
email := "[email protected]"
ctx.User = map[string]interface{}{ab.PrimaryID: email}
old = time.Now().UTC().Add(-1 * time.Hour)
for i := 0; i < 3; i++ {
if lockedIntf, ok := storer.Users["[email protected]"][StoreLocked]; ok && lockedIntf.(bool) {
t.Errorf("%d: User should not be locked.", i)
}
if err := lock.afterAuthFail(ctx); err != nil {
t.Error(err)
}
if val := storer.Users[email][StoreAttemptNumber].(int64); val != int64(i+1) {
t.Errorf("%d: StoreAttemptNumber set incorrectly: %v", i, val)
}
if current, ok = storer.Users[email][StoreAttemptTime].(time.Time); !ok || old.After(current) {
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)
}
if _, ok = storer.Users[email][StoreAttemptTime].(time.Time); !ok {
t.Error("StoreAttemptTime not set correctly.")
}
}
示例13: setup
func setup() *Register {
ab := authboss.New()
ab.RegisterOKPath = "/regsuccess"
ab.Layout = template.Must(template.New("").Parse(`{{template "authboss" .}}`))
ab.XSRFName = "xsrf"
ab.XSRFMaker = func(_ http.ResponseWriter, _ *http.Request) string {
return "xsrfvalue"
}
ab.ConfirmFields = []string{"password", "confirm_password"}
ab.Storer = mocks.NewMockStorer()
reg := Register{}
if err := reg.Initialize(ab); err != nil {
panic(err)
}
return ®
}
示例14: TestAuth
func TestAuth(t *testing.T) {
t.Parallel()
r := &Remember{authboss.New()}
storer := mocks.NewMockStorer()
r.Storer = storer
cookies := mocks.NewMockClientStorer()
session := mocks.NewMockClientStorer()
ctx := r.NewContext()
ctx.CookieStorer = cookies
ctx.SessionStorer = session
key := "tester"
_, err := r.new(cookies, key)
if err != nil {
t.Error("Unexpected error:", err)
}
cookie, _ := cookies.Get(authboss.CookieRemember)
interrupt, err := r.auth(ctx)
if err != nil {
t.Error("Unexpected error:", err)
}
if session.Values[authboss.SessionHalfAuthKey] != "true" {
t.Error("The user should have been half-authed.")
}
if session.Values[authboss.SessionKey] != key {
t.Error("The user should have been logged in.")
}
if chocolateChip, _ := cookies.Get(authboss.CookieRemember); chocolateChip == cookie {
t.Error("Expected cookie to be different")
}
if authboss.InterruptNone != interrupt {
t.Error("Keys should have matched:", interrupt)
}
}
示例15: testSetup
func testSetup() (a *Auth, s *mocks.MockStorer) {
s = mocks.NewMockStorer()
ab := authboss.New()
ab.LogWriter = ioutil.Discard
ab.Layout = template.Must(template.New("").Parse(`{{template "authboss" .}}`))
ab.Storer = s
ab.XSRFName = "xsrf"
ab.XSRFMaker = func(_ http.ResponseWriter, _ *http.Request) string {
return "xsrfvalue"
}
ab.PrimaryID = authboss.StoreUsername
a = &Auth{}
if err := a.Initialize(ab); err != nil {
panic(err)
}
return a, s
}