Skip to content
Extraits de code Groupes Projets
registrations_controller_spec.rb 4,79 ko
Newer Older
  • Learn to ignore specific revisions
  • danielgrippi's avatar
    danielgrippi a validé
    #   Copyright (c) 2010-2011, Diaspora Inc.  This file is
    
    #   licensed under the Affero General Public License version 3 or later.  See
    #   the COPYRIGHT file.
    
    
    cmrd Senya's avatar
    cmrd Senya a validé
    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
    
    Jonne Haß's avatar
    Jonne Haß a validé
          AppConfig.settings.enable_registrations = false
    
        it "redirects #new to the login page" do
    
          expect(flash[:error]).to eq(I18n.t("registrations.closed"))
    
          expect(response).to redirect_to new_user_session_path
    
        it "redirects #create to the login page" do
    
          expect(flash[:error]).to eq(I18n.t("registrations.closed"))
    
          expect(response).to redirect_to new_user_session_path
    
        it "does not redirect if there is a valid invite token" do
          code = InvitationCode.create(user: bob)
          get :new, invite: {token: code.token}
    
          expect(response).not_to be_redirect
    
        it "does redirect if there is an invalid invite token" do
          get :new, invite: {token: "fssdfsd"}
          expect(response).to redirect_to new_user_session_path
        end
    
        it "does redirect if there are no invites available with this code" do
          code = InvitationCode.create(user: bob)
          code.update_attributes(count: 0)
    
          get :new, invite: {token: code.token}
          expect(response).to redirect_to new_user_session_path
        end
    
        it "does not redirect when the registration is open" do
          AppConfig.settings.enable_registrations = true
    
          code = InvitationCode.create(user: bob)
          code.update_attributes(count: 0)
    
          get :new, invite: {token: code.token}
          expect(response).not_to be_redirect
    
      describe "#create" do
    
        context "with valid parameters" do
          it "creates a user" do
    
            }.to change(User, :count).by(1)
    
          it "assigns @user" do
    
            expect(assigns(:user)).to be_truthy
    
          it "sets the flash" do
    
            expect(flash[:notice]).not_to be_blank
    
          it "redirects to the home path" do
    
            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, 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, 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, valid_params.merge(invite: {token: code.token})
    
              expect(User.find_by(username: "jdoe").invited_by).to eq(bob)
            end
    
          let(:invalid_params) { valid_params.deep_merge(user: {password_confirmation: "baddword"}) }
    
            expect { get :create, invalid_params }.not_to change(User, :count)
    
            expect { get :create, invalid_params }.not_to change(Person, :count)
    
          it "assigns @user" do
    
            expect(assigns(:user)).not_to be_nil
    
            expect(flash[:error]).not_to be_blank
    
          it "doesn't reduce number of available invites" do
    
            AppConfig.settings.enable_registrations = false
    
    
            code = InvitationCode.create(user: bob)
    
            expect {
              get :create, invalid_params.merge(invite: {token: code.token})
            }.not_to change { code.reload.count }
          end
    
    
            expect(response).to render_template("registrations/new")
          end
    
            expect(response.body).to match /jdoe@example.com/m