예제 #1
0
func (s TestSessionStore) NewSession(id string, e time.Time) (s2tore.Session, error) {
	return gumtest.NewTestSession(
		"1234",
		id,
		e,
	), nil
}
예제 #2
0
func Test_SignIn_Fail(t *testing.T) {
	user := User{
		ID:       1,
		Username: "******",
		Password: "******",
	}
	userStore := TestUserStore{user}

	sess := gumtest.NewTestSession(
		"444",
		user.Username,
		time.Now().Add(1*time.Hour),
	)
	sessionStore := TestSessionStore{sess}

	r := gin.New()
	h := SignIn(sessionStore, userStore)
	r.GET("/:name/:password", h)

	name := base64.StdEncoding.EncodeToString([]byte(user.Username))
	pass := base64.StdEncoding.EncodeToString([]byte("wrong"))

	url := fmt.Sprintf("/%v/%v", name, pass)
	resp := gumtest.NewRouter(r).ServeHTTP("GET", url, "")

	if resp.Code != http.StatusUnauthorized {
		t.Fatalf("Expect %v was %v", http.StatusUnauthorized, resp.Code)
	}

}
예제 #3
0
func Test_SignedIn_OK(t *testing.T) {
	testSess := gumtest.NewTestSession(
		"123",
		"lovemaster_XXX",
		time.Now().Add(1*time.Hour),
	)
	sessStore := TestSessionStore{testSess}

	// Test if session key in gin context
	afterAuth := func(c *gin.Context) {
		sess, err := ReadSession(c)
		if err != nil {
			t.Fatal(err)
		}

		if !reflect.DeepEqual(testSess, sess) {
			t.Fatalf("Expect %v was %v", testSess, sess)
		}

		c.JSON(http.StatusOK, nil)
	}

	gin.SetMode(gin.ReleaseMode)
	r := gin.New()
	signedIn := SignedIn(sessStore)
	r.GET("/", signedIn(afterAuth))

	head := TokenHeader("123")
	resp := gumtest.NewRouter(r).ServeHTTPWithHeader("GET", "/", "", head)

	if resp.Code != http.StatusOK {
		t.Fatalf("Expect %v was %v", http.StatusOK, resp.Code)
	}

}