• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang mocks.NewMockStorer函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中gopkg/in/authboss/v0/internal/mocks.NewMockStorer函数的典型用法代码示例。如果您正苦于以下问题:Golang NewMockStorer函数的具体用法?Golang NewMockStorer怎么用?Golang NewMockStorer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了NewMockStorer函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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.")
	}
}
开发者ID:voiid,项目名称:authboss,代码行数:32,代码来源:lock_test.go


示例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)
	}
}
开发者ID:voiid,项目名称:authboss,代码行数:34,代码来源:lock_test.go


示例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.")
	}
}
开发者ID:caghan,项目名称:qor-example,代码行数:26,代码来源:remember_test.go


示例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.")
	}
}
开发者ID:caghan,项目名称:qor-example,代码行数:31,代码来源:remember_test.go


示例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)
	}

}
开发者ID:caghan,项目名称:qor-example,代码行数:27,代码来源:auth_test.go


示例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.")
	}
}
开发者ID:caghan,项目名称:qor-example,代码行数:29,代码来源:remember_test.go


示例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.")
	}
}
开发者ID:voiid,项目名称:authboss,代码行数:25,代码来源:lock_test.go


示例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.")
	}
}
开发者ID:caghan,项目名称:qor-example,代码行数:31,代码来源:remember_test.go


示例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.")
	}
}
开发者ID:voiid,项目名称:authboss,代码行数:58,代码来源:confirm_test.go


示例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)
	}
}
开发者ID:voiid,项目名称:authboss,代码行数:10,代码来源:oauth2_test.go


示例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
}
开发者ID:voiid,项目名称:authboss,代码行数:12,代码来源:confirm_test.go


示例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.")
	}
}
开发者ID:voiid,项目名称:authboss,代码行数:49,代码来源:lock_test.go


示例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 &reg
}
开发者ID:caghan,项目名称:qor-example,代码行数:18,代码来源:register_test.go


示例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)
	}
}
开发者ID:caghan,项目名称:qor-example,代码行数:42,代码来源:remember_test.go


示例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
}
开发者ID:caghan,项目名称:qor-example,代码行数:20,代码来源:auth_test.go


示例16: TestRegister

func TestRegister(t *testing.T) {
	ab := authboss.New()
	ab.Storer = mocks.NewMockStorer()
	r := Register{}
	if err := r.Initialize(ab); err != nil {
		t.Error(err)
	}

	if r.Routes()["/register"] == nil {
		t.Error("Expected a register handler at /register.")
	}

	sto := r.Storage()
	if sto[r.PrimaryID] != authboss.String {
		t.Error("Wanted primary ID to be a string.")
	}
	if sto[authboss.StorePassword] != authboss.String {
		t.Error("Wanted password to be a string.")
	}
}
开发者ID:caghan,项目名称:qor-example,代码行数:20,代码来源:register_test.go


示例17: testSetup

func testSetup() (r *Recover, s *mocks.MockStorer, l *bytes.Buffer) {
	s = mocks.NewMockStorer()
	l = &bytes.Buffer{}

	ab := authboss.New()
	ab.Layout = template.Must(template.New("").Parse(`{{template "authboss" .}}`))
	ab.LayoutHTMLEmail = template.Must(template.New("").Parse(`<strong>{{template "authboss" .}}</strong>`))
	ab.LayoutTextEmail = 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
	ab.LogWriter = l

	ab.Policies = []authboss.Validator{
		authboss.Rules{
			FieldName:       "username",
			Required:        true,
			MinLength:       2,
			MaxLength:       4,
			AllowWhitespace: false,
		},
		authboss.Rules{
			FieldName:       "password",
			Required:        true,
			MinLength:       4,
			MaxLength:       8,
			AllowWhitespace: false,
		},
	}

	r = &Recover{}
	if err := r.Initialize(ab); err != nil {
		panic(err)
	}

	return r, s, l
}
开发者ID:voiid,项目名称:authboss,代码行数:40,代码来源:recover_test.go


示例18: TestInitialize

func TestInitialize(t *testing.T) {
	t.Parallel()

	ab := authboss.New()
	r := &Remember{}
	err := r.Initialize(ab)
	if err == nil {
		t.Error("Expected error about token storers.")
	}

	ab.Storer = mocks.MockFailStorer{}
	err = r.Initialize(ab)
	if err == nil {
		t.Error("Expected error about token storers.")
	}

	ab.Storer = mocks.NewMockStorer()
	err = r.Initialize(ab)
	if err != nil {
		t.Error("Unexpected error:", err)
	}
}
开发者ID:caghan,项目名称:qor-example,代码行数:22,代码来源:remember_test.go


示例19: TestLock

func TestLock(t *testing.T) {
	t.Parallel()

	ab := authboss.New()
	storer := mocks.NewMockStorer()
	ab.Storer = storer
	lock := Lock{ab}

	email := "[email protected]"
	storer.Users[email] = map[string]interface{}{
		ab.PrimaryID: email,
		"password":   "password",
	}

	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.")
	}
}
开发者ID:voiid,项目名称:authboss,代码行数:23,代码来源:lock_test.go


示例20: 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"}`)

	uri := fmt.Sprintf("%s?state=%s", "localhost/oauthed", "xsrf")
	req, err := http.NewRequest("GET", uri, nil)
	if err != nil {
		t.Error("Unexpected Error:", err)
	}

	ctx, err := r.ContextFromRequest(req)
	if err != nil {
		t.Error("Unexpected error:", err)
	}

	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.")
	}
}
开发者ID:guilherme-santos,项目名称:authboss,代码行数:36,代码来源:remember_test.go



注:本文中的gopkg/in/authboss/v0/internal/mocks.NewMockStorer函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang v1.Commentf函数代码示例发布时间:2022-05-28
下一篇:
Golang mocks.NewMockClientStorer函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap