<commit_before>require 'rails_helper'

module DiabeticToolbox
  RSpec.describe Member, type: :model do
    #region Definitions
    let(:member_params) { {first_name: 'Frodo', last_name: 'Baggins',
        email: '<EMAIL>', username: 'Ring Bearer',
        password: 'password', password_confirmation: 'password',
        accepted_tos: true } }
    let(:create_success_message) { 'Member Frodo Created' }
    let(:create_failure_message) { 'Create Member Failed' }
    let(:safe_model_data) { { first_name: 'Frodo', last_name: 'Baggins', username: 'Ring Bearer', slug: 'ring-bearer' } }
    let(:validations_password_empty) { { password: ['Required', 'Between 8 and 64 characters'] } }
    let(:validations_password_length) { { password: ['Between <PASSWORD>'] } }
    let(:validations_password_mismatch) { { password_confirmation: ['Password<PASSWORD>'] } }
    let(:validations_first_name_format) { { first_name: ['Only letters and spaces allowed'] } }
    let(:validations_last_name_format) { { last_name: ['Only letters and hyphens allowed'] } }
    let(:validations_username_format) { { username: ['Only letters, spaces and numbers allowed'] } }
    let(:validations_accepted_tos_required) { { accepted_tos: ['Required'] } }
    #endregion

    #region Stories
    describe 'a member being created' do
      context 'using action class' do
        #region Success Conditions
        it 'should save with appropriate parameters' do
          create_member = CreateMember.new(member_params).call

          expect(create_member.actual.slug).to eq safe_model_data[:slug]
          expect(create_member.successful?).to eq true
          expect(create_member.actual.authenticate!(member_params[:password])).to eq true
          expect(create_member.response).to eq [create_success_message, {}, safe_model_data]
        end
        #endregion

        #region Password
        it 'should not save without a password' do
          params = member_params
          params.delete(:password)
          params.delete(:password_confirmation)

          create_member = CreateMember.new(params).call

          expect(create_member.successful?).to eq false
          expect(create_member.actual.slug).to eq safe_model_data[:slug]
          expect(create_member.actual.new_record?).to eq true
          expect(create_member.actual.errors.messages.size).to be >= 1
          expect(create_member.response).to eq [create_failure_message, validations_password_empty, safe_model_data]
        end

        it 'should not save with a password that is too short' do
          params = member_params
          params[:password] = '<PASSWORD>'
          params[:password_confirmation] = '<PASSWORD>'
          short = CreateMember.new(params).call


          expect(short.successful?).to eq false
          expect(short.actual.slug).to eq safe_model_data[:slug]
          expect(short.actual.new_record?).to eq true
          expect(short.actual.errors.messages.size).to be >= 1
          expect(short.response).to eq [ create_failure_message, validations_password_length, safe_model_data ]
        end

        it 'should not save with a password that is too long' do
          params = member_params
          pass   = Faker::Internet.password 65
          params[:password] = params[:password_confirmation] = pass
          long = CreateMember.new(params).call

          expect(long.successful?).to eq false
          expect(long.actual.slug).to eq safe_model_data[:slug]
          expect(long.actual.new_record?).to eq true
          expect(long.actual.errors.messages.size).to be >= 1
          expect(long.response).to eq [ create_failure_message, validations_password_length, safe_model_data ]
        end

        it 'should not save unless passwords match' do
          params = member_params
          params[:password_confirmation] = '<PASSWORD>'

          create_member = CreateMember.new(params).call

          expect(create_member.successful?).to eq false
          expect(create_member.actual.slug).to eq safe_model_data[:slug]
          expect(create_member.actual.new_record?).to eq true
          expect(create_member.actual.errors.messages.size).to be >= 1
          expect(create_member.response).to eq [ create_failure_message, validations_password_mismatch, safe_model_data ]
        end
        #endregion

        #region Name First/Last
        it 'should not save with invalid first name' do
          expected = safe_model_data
          params   = member_params

          params[:first_name]   = '<NAME>'
          expected[:first_name] = '<NAME>'

          create_member = CreateMember.new(params).call

          expect(create_member.successful?).to eq false
          expect(create_member.actual.slug).to eq safe_model_data[:slug]
          expect(create_member.actual.new_record?).to eq true
          expect(create_member.response[1].size).to be >= 1
          expect(create_member.response).to eq [ create_failure_message, validations_first_name_format, expected ]
        end

        it 'should not save with invalid last name' do
          expected = safe_model_data
          params   = member_params

          params[:last_name]   = '<NAME>'
          expected[:last_name] = '<NAME>'

          create_member = CreateMember.new(params).call

          expect(create_member.successful?).to eq false
          expect(create_member.actual.slug).to eq safe_model_data[:slug]
          expect(create_member.actual.new_record?).to eq true
          expect(create_member.response[1].size).to be >= 1
          expect(create_member.response).to eq [ create_failure_message, validations_last_name_format, expected ]
        end
        #endregion

        #region Username
        it 'should not save with invalid username' do
          expected = safe_model_data
          params   = member_params

          params[:username]   = '!!Ring Bearer'
          expected[:username] = '!!Ring Bearer'
          expected[:slug]     = safe_model_data[:slug]

          create_member = CreateMember.new(params).call

          expect(create_member.successful?).to eq false
          expect(create_member.actual.slug).to eq expected[:slug]
          expect(create_member.actual.new_record?).to eq true
          expect(create_member.response[1].size).to be >= 1
          expect(create_member.response).to eq [ create_failure_message, validations_username_format, expected ]
        end
        #endregion

        #region TOS
        it 'should not save without acceptance of the terms of service' do
          params = member_params
          params.delete :accepted_tos

          create_member = CreateMember.new(member_params).call

          expect(create_member.actual.slug).to eq safe_model_data[:slug]
          expect(create_member.successful?).to eq false
          expect(create_member.response).to eq [create_failure_message, validations_accepted_tos_required, safe_model_data]
        end
        #endregion
      end
    end#describe creation<commit_msg>A change to use the DiabeticToolbox::Result::Success or DiabeticToolbox::Result::Failure as the return value of CreateMember.new(params).call<commit_after>require 'rails_helper'

module DiabeticToolbox
  RSpec.describe Member, type: :model do
    #region Definitions
    let(:member_params) { {first_name: 'Frodo', last_name: 'Baggins',
        email: '<EMAIL>', username: 'Ring Bearer',
        password: 'password', password_confirmation: 'password',
        accepted_tos: true } }
    let(:create_success_message) { 'Member Frodo Created' }
    let(:create_failure_message) { 'Create Member Failed' }
    let(:safe_model_data) { { first_name: 'Frodo', last_name: 'Baggins', username: 'Ring Bearer', slug: 'ring-bearer' } }
    let(:validations_password_empty) { { password: ['Required', 'Between 8 and 64 characters'] } }
    let(:validations_password_length) { { password: ['Between 8 and 64 characters'] } }
    let(:validations_password_mismatch) { { password_confirmation: ['Passwords must match'] } }
    let(:validations_first_name_format) { { first_name: ['Only letters and spaces allowed'] } }
    let(:validations_last_name_format) { { last_name: ['Only letters and hyphens allowed'] } }
    let(:validations_username_format) { { username: ['Only letters, spaces and numbers allowed'] } }
    let(:validations_accepted_tos_required) { { accepted_tos: ['Required'] } }
    #endregion

    #region Stories
    describe 'a member being created' do
      DiabeticToolbox.from :members, require: %w(create_member)
      context 'using action class' do
        #region Success Conditions
        it 'should save with appropriate parameters' do
          create_member = CreateMember.new(member_params).call

          expect(create_member.actual.slug).to eq safe_model_data[:slug]
          expect(create_member.success?).to eq true
          expect(create_member.actual.authenticate!(member_params[:password])).to eq true
          expect(create_member.response).to eq [create_success_message, {}, safe_model_data]
        end
        #endregion

        #region Password
        it 'should not save without a password' do
          params = member_params
          params.delete(:password)
          params.delete(:password_confirmation)

          create_member = CreateMember.new(params).call

          expect(create_member.success?).to eq false
          expect(create_member.actual.slug).to eq safe_model_data[:slug]
          expect(create_member.actual.new_record?).to eq true
          expect(create_member.actual.errors.messages.size).to be >= 1
          expect(create_member.response).to eq [create_failure_message, validations_password_empty, safe_model_data]
        end

        it 'should not save with a password that is too short' do
          params = member_params
          params[:password] = '<PASSWORD>'
          params[:password_confirmation] = '<PASSWORD>'
          short = CreateMember.new(params).call


          expect(short.success?).to eq false
          expect(short.actual.slug).to eq safe_model_data[:slug]
          expect(short.actual.new_record?).to eq true
          expect(short.actual.errors.messages.size).to be >= 1
          expect(short.response).to eq [ create_failure_message, validations_password_length, safe_model_data ]
        end

        it 'should not save with a password that is too long' do
          params = member_params
          pass   = Faker::Internet.password 65
          params[:password] = params[:password_confirmation] = pass
          long = CreateMember.new(params).call

          expect(long.success?).to eq false
          expect(long.actual.slug).to eq safe_model_data[:slug]
          expect(long.actual.new_record?).to eq true
          expect(long.actual.errors.messages.size).to be >= 1
          expect(long.response).to eq [ create_failure_message, validations_password_length, safe_model_data ]
        end

        it 'should not save unless passwords match' do
          params = member_params
          params[:password_confirmation] = '<PASSWORD>'

          create_member = CreateMember.new(params).call

          expect(create_member.success?).to eq false
          expect(create_member.actual.slug).to eq safe_model_data[:slug]
          expect(create_member.actual.new_record?).to eq true
          expect(create_member.actual.errors.messages.size).to be >= 1
          expect(create_member.response).to eq [ create_failure_message, validations_password_mismatch, safe_model_data ]
        end
        #endregion

        #region Name First/Last
        it 'should not save with invalid first name' do
          expected = safe_model_data
          params   = member_params

          params[:first_name]   = '<NAME>'
          expected[:first_name] = '<NAME>'

          create_member = CreateMember.new(params).call

          expect(create_member.success?).to eq false
          expect(create_member.actual.slug).to eq safe_model_data[:slug]
          expect(create_member.actual.new_record?).to eq true
          expect(create_member.response[1].size).to be >= 1
          expect(create_member.response).to eq [ create_failure_message, validations_first_name_format, expected ]
        end

        it 'should not save with invalid last name' do
          expected = safe_model_data
          params   = member_params

          params[:last_name]   = '<NAME>'
          expected[:last_name] = '<NAME>'

          create_member = CreateMember.new(params).call

          expect(create_member.success?).to eq false
          expect(create_member.actual.slug).to eq safe_model_data[:slug]
          expect(create_member.actual.new_record?).to eq true
          expect(create_member.response[1].size).to be >= 1
          expect(create_member.response).to eq [ create_failure_message, validations_last_name_format, expected ]
        end
        #endregion

        #region Username
        it 'should not save with invalid username' do
          expected = safe_model_data
          params   = member_params

          params[:username]   = '!!Ring Bearer'
          expected[:username] = '!!Ring Bearer'
          expected[:slug]     = safe_model_data[:slug]

          create_member = CreateMember.new(params).call

          expect(create_member.success?).to eq false
          expect(create_member.actual.slug).to eq expected[:slug]
          expect(create_member.actual.new_record?).to eq true
          expect(create_member.response[1].size).to be >= 1
          expect(create_member.response).to eq [ create_failure_message, validations_username_format, expected ]
        end
        #endregion

        #region TOS
        it 'should not save without acceptance of the terms of service' do
          params = member_params
          params.delete :accepted_tos

          create_member = CreateMember.new(member_params).call

          expect(create_member.actual.slug).to eq safe_model_data[:slug]
          expect(create_member.success?).to eq false
          expect(create_member.response).to eq [create_failure_message, validations_accepted_tos_required, safe_model_data]
        end
        #endregion
      end
    end#describe creation
{'fraction_non_alphanumeric': 0.09629262770624027, 'fraction_numerical': 0.002334795528512806, 'mean_word_length': 2.8853765805387575, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3620617', 'n_tokens_mistral': 3926, 'n_tokens_neox': 3848, 'n_words': 877}