示例#1
0
func TestUserCreate(t *testing.T) {
	record := request.TestServer{t, TestHandler}

	Describe("when the request is malformed", func() {
		body := url.Values{}
		body.Set("name", "jason")
		body.Set("not a user field", "asdf")

		It("should return 400", func() {
			record.Post("/signup", body).Expect(400).Expect(`bad request`)
		})
	})

	Describe("when the request fails validation", func() {
		body := url.Values{}
		body.Set("name", "jesse")
		body.Set("email", "*****@*****.**")
		body.Set("password", "some random password")
		body.Set("confirm", "fail confirm")

		It("should return 422", func() {
			record.Post("/signup", body).Expect(422).Expect("password does not match")
		})

		It("should fill in form values after failed submission", func() {
			record.Post("/signup", body).
				Expect(`value="jesse"`).
				Expect(`value="*****@*****.**"`).
				Expect(`value="some random password"`).
				Expect(`value="fail confirm"`)
		})
	})

	Describe("when creating a user that already exists", func() {
		var (
			name  = "jason"
			email = "*****@*****.**"
		)

		BeforeEach(func() {
			user := &models.User{Name: name, Email: email}
			_, err := models.DB.Create(user)
			if err != nil {
				t.Error(err)
			}
		})

		AfterEach(func() {
			query := "TRUNCATE TABLE users; ALTER SEQUENCE users_id_seq RESTART WITH 1"
			_, err := models.DB.Exec(query)
			if err != nil {
				t.Error(err)
			}
		})

		It("should return a 409 for an existing email", func() {
			body := url.Values{}
			body.Set("name", name)
			body.Set("email", email)
			body.Set("password", "somepassword")
			body.Set("confirm", "somepassword")
			record.Post("/signup", body).Expect(409).Expect("email is taken")
		})
	})

	Describe("when creating a user that doesn't exist", func() {
		body := url.Values{
			"name":     {"jason"},
			"email":    {"*****@*****.**"},
			"password": {"somepassword"},
			"confirm":  {"somepassword"},
		}

		AfterEach(func() {
			query := "TRUNCATE TABLE users; ALTER SEQUENCE users_id_seq RESTART WITH 1"
			_, err := models.DB.Exec(query)
			if err != nil {
				t.Error(err)
			}
		})

		It("should sign in", func() {
			ctx := record.Post("/signup", body)
			assert.NotEmpty(t, ctx.ResponseRecorder.Header().Get("Set-Cookie"))
		})

		It("should redirect to the newly created profile", func() {
			ctx := record.Post("/signup", body).Expect(302)
			redirect := ctx.ResponseRecorder.Header().Get("Location")
			assert.Equal(t, "/user/1", redirect)
		})

		It("should work regardless of the current session cookie", func() {
			ctx := record.Post("/signup", body)

			header := ctx.ResponseRecorder.Header()
			header["Cookie"] = header["Set-Cookie"]
			header.Del("Set-Cookie")

			models.Store = sessions.NewCookieStore(
				securecookie.GenerateRandomKey(64),
				securecookie.GenerateRandomKey(16),
			)

			b2 := url.Values{
				"name":     {"jesse"},
				"email":    {"coolstory@bro"},
				"password": {"somepassword"},
				"confirm":  {"somepassword"},
			}

			record.PostWithHeaders("/signup", header, b2).Expect(302)
		})

		It("regression :: should set the password and remember digest", func() {
			record.Post("/signup", body)

			user := &models.User{ID: 1}
			err := models.DB.Read(user)
			if err != nil {
				t.Error(err)
			}

			assert.NotEmpty(t, user.PasswordDigest)
			assert.NotEmpty(t, user.RememberDigest)
		})

		It("should show a welcome flash message", func() {
			ctx := record.Post("/signup", body)
			h := ctx.ResponseRecorder.Header()
			h["Cookie"] = h["Set-Cookie"]
			h.Del("Set-Cookie")
			record.GetWithHeaders("/user/1", h).Expect("welcome jason!")
		})
	})

	Describe("when redirected after successful signup", func() {
		ctx := record.Post("/signup", url.Values{
			"name":     {"jason"},
			"email":    {"*****@*****.**"},
			"password": {"somepassword"},
			"confirm":  {"somepassword"},
		})

		h := ctx.ResponseRecorder.Header()
		h["Cookie"] = h["Set-Cookie"]
		h.Del("Set-Cookie")

		ctx = record.GetWithHeaders("/user/1", h)

		reader := strings.NewReader(ctx.ResponseRecorder.Body.String())
		doc, err := goquery.NewDocumentFromReader(reader)
		if err != nil {
			t.Error(err)
		}

		It("should not show signup and login links", func() {
			doc.Find(".nav-item").Each(func(_ int, sel *goquery.Selection) {
				text := sel.Text()
				if strings.Contains(text, "Signup") || strings.Contains(text, "Login") {
					t.Errorf("whoops, you're logged in but signup and login links are present")
				}
			})
		})
	})
}