summaryrefslogtreecommitdiff
path: root/spec/features/sessions_spec.rb
blob: 5071e56411700cc55cd724faee55dc4d2d222f23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require "rails_helper"

describe "Logins" do
  subject { LoginPage.new }
  let(:error_message) { I18n.t("sessions.create.failure.invalid") }

  before :each do
    subject.visit_page
  end

  context "when an email is registered" do
    let!(:user) { create(:user, password: "password") }

    context "when the password is correct" do
      before :each do
        subject.login_with(email: user.email, password: "password")
      end

      it "logs the user in" do
        expect(page).to have_content(I18n.t("logout"))
      end

      it "has no errors" do
        expect(page).to_not have_content(error_message)
      end
    end

    context "when the password is incorrect" do
      before :each do
        subject.login_with(email: user.email, password: "wrong")
      end

      it "displays an error" do
        expect(page).to have_content(error_message)
      end
    end
  end

  context "when an email is not known" do
    before :each do
      subject.login_with(email: "test@example.com", password: "password")
    end

    it "displays an error message" do
      expect(page).to have_content(error_message)
    end
  end

  context "when clicking on the log out button" do
    let(:dashboard_page) { DashboardPage.new }
    let(:user) { create(:user, password: "password") }

    it "redirects you to the home page" do
      subject.visit_page
      subject.login_with(email: user.email, password: "password")
      dashboard_page.logout
      expect(current_path).to eql(root_path)
    end
  end
end