summaryrefslogtreecommitdiff
path: root/spec/controllers/registrations_controller_spec.rb
blob: 4ae3b35eaba729a13f62c47d900d73dc81bff5be (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require "rails_helper"

describe RegistrationsController do
  describe "#new" do
    let(:gon) { RequestStore.store[:gon].gon }

    it "loads a new user" do
      get :new
      expect(assigns(:user)).to be_new_record
    end

    it 'loads the used usernames' do
      user = create(:user)
      get :new
      expect(gon).to match_array([["usernames", [user.username]]])
    end
  end

  describe "#create" do
    let(:username) {  "username" }
    let(:password) {  "password" }
    let(:email) { "email@example.com" }

    context "when valid params are provided" do
      let(:mailer) { double(deliver_later: true) }

      before :each do
        allow(UserMailer).to receive(:registration_email).and_return(mailer)
        post :create, params: {
          user: {
            username: username,
            password: password,
            email: email,
            terms_and_conditions: "1"
          }
        }
      end

      it "creates a new user account" do
        expect(User.count).to eql(1)
        first_user = User.first
        expect(first_user.username).to eql(username)
        expect(first_user.email).to eql(email)
      end

      it "redirects them to edit their profile" do
        expect(response).to redirect_to(edit_profile_path(username))
      end

      it "logs them in" do
        expect(session[:user_id]).to be_present
        expect(session[:user_id]).to eql(UserSession.last.id)
      end

      it "does not display any errors" do
        expect(flash[:error]).to be_nil
      end

      it "shows a flash alert" do
        expect(flash[:notice]).to_not be_empty
        translation = I18n.translate("registrations.create.success")
        expect(flash[:notice]).to eql(translation)
      end

      it "sends a user registration email" do
        expect(mailer).to have_received(:deliver_later)
      end
    end

    context "when the parameters provided are invalid" do
      before :each do
        post :create, params: {
          user: {
            username: "",
            password: password,
            email: email,
            terms_and_conditions: "1"
          }
        }
      end

      it "adds an error to the flash for missing usernames" do
        expect(flash[:error]).to_not be_nil
        expect(flash[:error]).to_not be_empty
      end

      it "does not log them in" do
        expect(session[:user_id]).to be_nil
      end

      it "renders the registration page" do
        expect(response).to redirect_to(new_registration_path)
      end
    end
  end
end