<commit_before>require 'rails_helper'

RSpec.describe MailingAddressesController, :type => :controller do
	let(:user) {FactoryGirl.create(:user, :donor) }
	let(:ryr) { FactoryGirl.create(:rider_year_registration, :with_valid_associations) }
	let(:prp_params) { FactoryGirl.attributes_for(:persistent_rider_profile) }
	let(:m_a_params) { FactoryGirl.attributes_for(:mailing_address) }
	let(:second_m_a_params) { FactoryGirl.attributes_for(:mailing_address, :second) }
	# let(:donation) {FactoryGirl.create(:donation, :with_valid_associations_before_fee_processed)}


	before do |example|

		@prp = ryr.user.build_persistent_rider_profile(user: ryr.user)
	  @prp.update_attributes(prp_params)
		@prp.mailing_addresses.create(m_a_params)
		
	end

	context 'access + permissions' do
		it 'redirects non-logged in users away to log in' do 
			get :new
			expect(response).to redirect_to(new_user_session_path)
		end

		it 'allows logged in users to access ' do 
			sign_in user
			get :new#, persistent_rider_profile_id: @prp.id
			expect(response).to render_template(:new)
		end

		it 'only allows associated user to edit' do 
			sign_in @prp.user
			get :edit, id: @prp.mailing_addresses.first.id

			# p '@prp'
			# p "#{@prp.inspect}"
			# p 'prp mailing_addresses'
			# p "#{@prp.mailing_addresses.first.inspect}"

			expect(response).to render_template(:edit)
			expect( assigns(:m_a) ).to eq(@prp.mailing_addresses.first)

		end
	end

	# context 'new' do
	# 	it 'assigns appropriately and renders form' do
	# 		get :new, persistent_rider_profile_id: @prp.id<commit_msg>add another access test<commit_after>require 'rails_helper'

RSpec.describe MailingAddressesController, :type => :controller do
	let(:user) {FactoryGirl.create(:user, :donor) }
	let(:ryr) { FactoryGirl.create(:rider_year_registration, :with_valid_associations) }
	let(:prp_params) { FactoryGirl.attributes_for(:persistent_rider_profile) }
	let(:m_a_params) { FactoryGirl.attributes_for(:mailing_address) }
	let(:second_m_a_params) { FactoryGirl.attributes_for(:mailing_address, :second) }
	# let(:donation) {FactoryGirl.create(:donation, :with_valid_associations_before_fee_processed)}


	before do |example|

		@prp = ryr.user.build_persistent_rider_profile(user: ryr.user)
	  @prp.update_attributes(prp_params)
		@prp.mailing_addresses.create(m_a_params)
		
	end

	context 'access + permissions' do
		it 'redirects non-logged in users away to log in' do 
			get :new
			expect(response).to redirect_to(new_user_session_path)
		end

		it 'allows logged in users to access ' do 
			sign_in user
			get :new
			expect(response).to render_template(:new)
		end

		it 'allows associated user to edit' do 
			sign_in @prp.user
			get :edit, id: @prp.mailing_addresses.first.id
			expect(response).to render_template(:edit)
			expect( assigns(:m_a) ).to eq(@prp.mailing_addresses.first)
		end
		
		it 'redirects non-associated user trying to edit' do 
			sign_in user
			get :edit, id: @prp.mailing_addresses.first.id
			expect(response).to redirect_to(root_path)
		end
	end

	# context 'new' do
	# 	it 'assigns appropriately and renders form' do
	# 		get :new, persistent_rider_profile_id: @prp.id
{'fraction_non_alphanumeric': 0.11160290863104648, 'fraction_numerical': 0.0, 'mean_word_length': 5.14368932038835, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3310161', 'n_tokens_mistral': 1247, 'n_tokens_neox': 1118, 'n_words': 233}