master
 1require "rails_helper"
 2
 3describe RegistrationsController do
 4  describe "#new" do
 5    let(:gon) { RequestStore.store[:gon].gon }
 6
 7    it "loads a new user" do
 8      get :new
 9      expect(assigns(:user)).to be_new_record
10    end
11
12    it 'loads the used usernames' do
13      user = create(:user)
14      get :new
15      expect(gon).to match_array([["usernames", [user.username]]])
16    end
17  end
18
19  describe "#create" do
20    let(:username) {  "username" }
21    let(:password) {  "password" }
22    let(:email) { "email@example.com" }
23
24    context "when valid params are provided" do
25      let(:mailer) { double(deliver_later: true) }
26
27      before :each do
28        allow(UserMailer).to receive(:registration_email).and_return(mailer)
29        post :create, params: {
30          user: {
31            username: username,
32            password: password,
33            email: email,
34            terms_and_conditions: "1"
35          }
36        }
37      end
38
39      it "creates a new user account" do
40        expect(User.count).to eql(1)
41        first_user = User.first
42        expect(first_user.username).to eql(username)
43        expect(first_user.email).to eql(email)
44      end
45
46      it "redirects them to edit their profile" do
47        expect(response).to redirect_to(edit_profile_path(username))
48      end
49
50      it "logs them in" do
51        expect(session[:user_id]).to be_present
52        expect(session[:user_id]).to eql(UserSession.last.id)
53      end
54
55      it "does not display any errors" do
56        expect(flash[:error]).to be_nil
57      end
58
59      it "shows a flash alert" do
60        expect(flash[:notice]).to_not be_empty
61        translation = I18n.translate("registrations.create.success")
62        expect(flash[:notice]).to eql(translation)
63      end
64
65      it "sends a user registration email" do
66        expect(mailer).to have_received(:deliver_later)
67      end
68    end
69
70    context "when the parameters provided are invalid" do
71      before :each do
72        post :create, params: {
73          user: {
74            username: "",
75            password: password,
76            email: email,
77            terms_and_conditions: "1"
78          }
79        }
80      end
81
82      it "adds an error to the flash for missing usernames" do
83        expect(flash[:error]).to_not be_nil
84        expect(flash[:error]).to_not be_empty
85      end
86
87      it "does not log them in" do
88        expect(session[:user_id]).to be_nil
89      end
90
91      it "renders the registration page" do
92        expect(response).to redirect_to(new_registration_path)
93      end
94    end
95  end
96end