示例#1
0
func TestUserLogged(t *testing.T) {
	initTestDB()
	defer closeTestDB()

	req, err := http.NewRequest("GET", "/", nil)
	if err != nil {
		t.Fatalf("Request was not successful: %v", err)
	}

	if val := userLogged(req, nil); val {
		t.Fatalf("Expected to be false: %v", val)
	}

	w := httptest.NewRecorder()
	lib.SetCookie(w, req, "userId", "1")

	if val := userLogged(req, nil); val {
		t.Fatalf("Expected to be false: %v", val)
	}

	u := &app.User{ID: uuid.Generate().String(), Name: "user", PasswordHash: "1234"}
	app.Db.Insert(u)

	var user app.User
	if err := app.Db.SelectOne(&user, "select * from users"); err != nil {
		t.Fatalf("Expected to be nil: %v", err)
	}

	w = httptest.NewRecorder()
	lib.SetCookie(w, req, "userId", user.ID)

	if val := userLogged(req, nil); !val {
		t.Fatalf("Expected to be true: %v", val)
	}
}
示例#2
0
func login(res http.ResponseWriter, req *http.Request) {
	var user User
	err := Db.SelectOne(&user, "select * from users")
	if err != nil {
		panic("There are no users...")
	}
	lib.SetCookie(res, req, "userId", user.ID)
}
示例#3
0
文件: sessions.go 项目: mssola/todo
// Login a user. It expects the "name" and "password" form values. Regardless
// if it was successful or not, it will redirect the user to the root path.
func Login(res http.ResponseWriter, req *http.Request) {
	// Check if the user exists and that the password is spot on.
	n, password := getNamePassword(req)
	id, err := matchPassword(n, password)
	if lib.CheckError(res, req, err) {
		return
	}

	// It's ok to login this user.
	if lib.JSONEncoding(req) {
		b, _ := json.Marshal(User{ID: id})
		fmt.Fprint(res, string(b))
	} else {
		lib.SetCookie(res, req, "userId", id)
		http.Redirect(res, req, "/", http.StatusFound)
	}
}
示例#4
0
文件: topics.go 项目: mssola/todo
// TopicsShow responds to: GET /topics/:id
func TopicsShow(res http.ResponseWriter, req *http.Request) {
	if lib.JSONEncoding(req) {
		TopicsShowJSON(res, req)
		return
	}

	var t Topic

	p := mux.Vars(req)
	err := Db.SelectOne(&t, "select * from topics where id=$1", p["id"])
	if err != nil {
		log.Printf("Could not select topic: %v", err)
	}
	if t.ID != "" {
		lib.SetCookie(res, req, "topic", t.ID)
	}
	print := req.URL.Query().Get("print") == "1"
	renderShow(res, &t, print)
}
示例#5
0
func TestTopicsIndexWithCookie(t *testing.T) {
	initTestDB()
	defer closeTestDB()

	_, err := createTopic("topic1")
	if err != nil {
		t.Fatalf("Expected to be nil: %v", err)
	}
	_, err = createTopic("topic2")
	if err != nil {
		t.Fatalf("Expected to be nil: %v", err)
	}

	var t1, t2 Topic
	err = Db.SelectOne(&t1, "select * from topics where name=$1", "topic1")
	if err != nil {
		t.Fatalf("Expected to be nil: %v", err)
	}
	err = Db.SelectOne(&t2, "select * from topics where name=$1", "topic2")
	if err != nil {
		t.Fatalf("Expected to be nil: %v", err)
	}

	req, err := http.NewRequest("POST", "/", nil)
	if err != nil {
		t.Fatalf("Expected to be nil: %v", err)
	}
	w := httptest.NewRecorder()
	lib.SetCookie(w, req, "topic", t2.ID)
	TopicsIndex(w, req)

	str, _ := ioutil.ReadAll(w.Body)
	s := string(str)
	s1 := "<li><a href=\"/topics/%v\">topic1</a></li>"
	s2 := "<li class=\"selected\"><a href=\"/topics/%v\">topic2</a></li>"
	if !strings.Contains(s, fmt.Sprintf(s1, t1.ID)) {
		t.Fatalf("S: %v; Should've containerd: %v", s, fmt.Sprintf(s1, t1.ID))
	}
	if !strings.Contains(s, fmt.Sprintf(s2, t2.ID)) {
		t.Fatalf("S: %v; Should've containerd: %v", s, fmt.Sprintf(s2, t2.ID))
	}
}