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

}
示例#2
0
func Test_UpdateHandler(t *testing.T) {
	db := initTestDB(t)
	tasks, _, _ := fillTestDB(t, db)

	r := gin.New()
	r.PUT("/:id", gumtest.MockAuther(gumwrap.Gorp(Update, db), "1"))

	newTask := Task{
		ID:      tasks[0].ID,
		Name:    "Kill spider behind the mirror",
		Desc:    "buy poisen",
		Expires: tasks[0].Expires,
		Done:    false,
	}
	body, err := json.Marshal(newTask)
	if err != nil {
		t.Fatal(err)
	}
	resp := gumtest.NewRouter(r).ServeHTTP("PUT", "/1", string(body))
	expectResp := gumtest.JSONResponse{200, newTask}

	if err := gumtest.EqualJSONResponse(expectResp, resp); err != nil {
		t.Fatal(err)
	}

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

}
示例#4
0
func Test_ReadOneHandler(t *testing.T) {
	db := initTestDB(t)
	tasks, _, _ := fillTestDB(t, db)

	r := gin.New()
	r.GET("/:id", gumtest.MockAuther(gumwrap.Gorp(ReadOne, db), "1"))

	resp := gumtest.NewRouter(r).ServeHTTP("GET", "/2", "")

	expectResp := gumtest.JSONResponse{200, *tasks[1]}
	if err := gumtest.EqualJSONResponse(expectResp, resp); err != nil {
		t.Fatal(err)
	}
}
示例#5
0
func Test_DeleteHandler(t *testing.T) {
	db := initTestDB(t)
	fillTestDB(t, db)

	r := gin.New()
	r.DELETE("/:id", gumtest.MockAuther(gumwrap.Gorp(Delete, db), "1"))

	resp := gumtest.NewRouter(r).ServeHTTP("DELETE", "/1", "")
	expectResp := gumtest.JSONResponse{200, nil}

	if err := gumtest.EqualJSONResponse(expectResp, resp); err != nil {
		t.Fatal(err)
	}

}
示例#6
0
func Test_SignedIn_Fail(t *testing.T) {
	sessStore := TestSessionStore{gumtest.TestSession{}}

	afterAuth := func(c *gin.Context) {
		c.JSON(http.StatusOK, nil)
	}

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

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

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

}
示例#7
0
func Test_SignIn_OK(t *testing.T) {
	user := User{
		ID:       1,
		Username: "******",
		Password: NewSha512Password("123"),
	}

	userStore := TestUserStore{user}
	sessStore := TestSessionStore{}

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

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

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

	respSess := Session{}
	if err := json.Unmarshal(resp.Body.Bytes(), &respSess); err != nil {
		t.Fatal(err)
	}

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

	if respSess.Token != "1234" {
		t.Fatalf("Expect %v was %v", "1234", respSess.Token)
	}
	if respSess.UserID != "1" {
		t.Fatalf("Expect %v was %v", "1", respSess.UserID)
	}

	if err := validSignInCookie(resp); err != nil {
		t.Fatal(err)
	}

}
示例#8
0
func TestErrorResponse(t *testing.T) {
	r := gin.New()
	r.GET("/", func(c *gin.Context) {
		err := errors.New("f**k off")
		ErrorResponse(c, 404, err)
	})

	resp := gumtest.NewRouter(r).ServeHTTP("GET", "/", "")

	expect, err := json.Marshal(ErrorMessage{
		Message: "f**k off",
	})
	if err != nil {
		t.Fatal(err)
	}

	result := strings.Replace(resp.Body.String(), "\n", "", -1)
	if string(expect) != result {
		t.Fatalf("Expect (%v) was (%v)", string(expect), result)
	}
}
示例#9
0
func Test_CreateHandler(t *testing.T) {
	db := initTestDB(t)

	r := gin.New()
	r.POST("/", gumtest.MockAuther(gumwrap.Gorp(Create, db), "1"))

	task := Task{
		Name: "Help to breakout some f*****g candyman out of prison",
	}

	body, err := json.Marshal(task)
	if err != nil {
		t.Fatal(err)
	}

	resp := gumtest.NewRouter(r).ServeHTTP("POST", "/", string(body))

	task.ID = 1
	expectResp := gumtest.JSONResponse{201, task}
	if err := gumtest.EqualJSONResponse(expectResp, resp); err != nil {
		t.Fatal(err)
	}
}
示例#10
0
func Test_POST_User_OK(t *testing.T) {
	db := initTestDB(t)
	user := newTestUser()

	r := gin.New()
	r.POST("/", CreateUserSQL(db.Db))

	body := `
	{
		"username": "******",
		"password": "******",
		"first_name": "Dare",
		"last_name": "Devil",
		"email": "*****@*****.**"
	}
	`

	resp := gumtest.NewRouter(r).ServeHTTP("POST", "/", body)
	if resp.Code != http.StatusCreated {
		t.Fatalf("Expect %v was %v", http.StatusCreated, resp.Code)
	}

	respUser := User{}
	err := json.Unmarshal(resp.Body.Bytes(), &respUser)
	if err != nil {
		t.Fatal(err)
	}

	if user.Username != respUser.Username ||
		user.Email != respUser.Email ||
		user.FirstName != respUser.FirstName ||
		user.LastName != respUser.LastName {
		t.Fatalf("Expect %v was %v", user, respUser)
	}

}