Welcome to mirror list, hosted at ThFree Co, Russian Federation.

registrations_controller_spec.rb « controllers « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8c43191a5e30dfdd31369bc66a3b094b9f881dc3 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# frozen_string_literal: true

#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

describe RegistrationsController, type: :controller do
  before do
    request.env["devise.mapping"] = Devise.mappings[:user]
  end

  let(:valid_params) {
    {
      user: {
        username:              "jdoe",
        email:                 "jdoe@example.com",
        password:              "password",
        password_confirmation: "password"
      }
    }
  }

  describe "#check_registrations_open_or_valid_invite!" do
    before do
      AppConfig.settings.enable_registrations = false
    end

    it "redirects #new to the registrations closed page" do
      get :new
      expect(response).to redirect_to registrations_closed_path
    end

    it "redirects #create to the registrations closed page" do
      post :create, params: valid_params
      expect(response).to redirect_to registrations_closed_path
    end

    it "does not redirect if there is a valid invite token" do
      code = InvitationCode.create(user: bob)
      get :new, params: {invite: {token: code.token}}
      expect(response).not_to be_redirect
    end

    it "does redirect if there is an invalid invite token" do
      get :new, params: {invite: {token: "fssdfsd"}}
      expect(flash[:error]).to eq(I18n.t("registrations.invalid_invite"))
      expect(response).to redirect_to registrations_closed_path
    end

    it "does redirect if there are no invites available with this code" do
      code = InvitationCode.create(user: bob)
      code.update(count: 0)

      get :new, params: {invite: {token: code.token}}
      expect(response).to redirect_to registrations_closed_path
    end

    it "does redirect when invitations are closed now" do
      code = InvitationCode.create(user: bob)
      AppConfig.settings.invitations.open = false

      get :new, params: {invite: {token: code.token}}
      expect(response).to redirect_to registrations_closed_path
    end

    it "does not redirect when the registration is open" do
      AppConfig.settings.enable_registrations = true

      code = InvitationCode.create(user: bob)
      code.update(count: 0)

      get :new, params: {invite: {token: code.token}}
      expect(response).not_to be_redirect
    end
  end

  describe "#create" do
    render_views

    context "with valid parameters" do
      it "creates a user" do
        expect {
          get :create, params: valid_params
        }.to change(User, :count).by(1)
      end

      it "assigns @user" do
        get :create, params: valid_params
        expect(assigns(:user)).to be_truthy
      end

      it "sets the flash" do
        get :create, params: valid_params
        expect(flash[:notice]).not_to be_blank
      end

      it "redirects to the home path" do
        get :create, params: valid_params
        expect(response).to be_redirect
        expect(response.location).to match(/^#{getting_started_url}$/)
      end

      context "with invite code" do
        it "reduces number of available invites when the registration is closed" do
          AppConfig.settings.enable_registrations = false

          code = InvitationCode.create(user: bob)

          expect {
            get :create, params: valid_params.merge(invite: {token: code.token})
          }.to change { code.reload.count }.by(-1)
        end

        it "doesn't reduce number of available invites when the registration is open" do
          code = InvitationCode.create(user: bob)

          expect {
            get :create, params: valid_params.merge(invite: {token: code.token})
          }.not_to change { code.reload.count }
        end

        it "links inviter with the user" do
          code = InvitationCode.create(user: bob)

          post :create, params: valid_params.merge(invite: {token: code.token})

          expect(User.find_by(username: "jdoe").invited_by).to eq(bob)
        end
      end
    end

    context "with invalid parameters" do
      let(:invalid_params) { valid_params.deep_merge(user: {password_confirmation: "baddword"}) }

      it "does not create a user" do
        expect { get :create, params: invalid_params }.not_to change(User, :count)
      end

      it "does not create a person" do
        expect { get :create, params: invalid_params }.not_to change(Person, :count)
      end

      it "assigns @user" do
        get :create, params: invalid_params
        expect(assigns(:user)).not_to be_nil
      end

      it "sets the flash error" do
        get :create, params: invalid_params
        expect(flash[:error]).not_to be_blank
      end

      it "doesn't reduce number of available invites" do
        AppConfig.settings.enable_registrations = false

        code = InvitationCode.create(user: bob)

        expect {
          get :create, params: invalid_params.merge(invite: {token: code.token})
        }.not_to change { code.reload.count }
      end

      it "renders new" do
        get :create, params: invalid_params
        expect(response).to render_template("registrations/new")
      end

      it "keeps invalid params in form" do
        get :create, params: invalid_params
        expect(response.body).to match /jdoe@example.com/m
      end
    end
  end
end