From a1314f3bc4c054719ccdaeabebe1c1e9abb35d5c Mon Sep 17 00:00:00 2001 From: Tyranja Date: Tue, 6 Feb 2024 09:37:21 +0100 Subject: [PATCH 1/5] add new test --- spec/controllers/profiles_controller_spec.rb | 23 ++++++++++++++++---- spec/factories.rb | 1 + 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/spec/controllers/profiles_controller_spec.rb b/spec/controllers/profiles_controller_spec.rb index 4480a82e7..e7f58dd38 100644 --- a/spec/controllers/profiles_controller_spec.rb +++ b/spec/controllers/profiles_controller_spec.rb @@ -3,10 +3,10 @@ describe ProfilesController, type: :controller do include AuthHelper - let!(:profile_published) { create(:published_profile, topic_list: %w[ruby algorithms]) } - let!(:profile_unpublished) { create(:unpublished_profile) } - let!(:ada) { create(:published_profile, email: "ada@mail.org", main_topic_en: 'math') } - let!(:admin) { create(:admin) } + # let!(:profile_published) { create(:published_profile, topic_list: %w[ruby algorithms]) } + # let!(:profile_unpublished) { create(:unpublished_profile) } + # let!(:ada) { create(:published_profile, email: "ada@mail.org", main_topic_en: 'math') } + # let!(:admin) { create(:admin) } describe 'test index action' do before do @@ -28,6 +28,21 @@ end end + context "pagination" do + let!(:ada) { create(:published_profile, email: "ada@mail.org", main_topic_en: 'math') } + it 'displays index page 2' do + create_list(:published_profile, 23) + + get :index, params: { page: 1 } + # byebug + expect(assigns(:records).count).to eq 24 + expect(assigns(:records)).to include ada + get :index, params: { page: 2 } + expect(assigns(:records)).not_to include ada + expect(assigns(:records).count).to eq 0 + end + end + describe 'category id search' do it 'stores the correct category when params has category id' do category = FactoryBot.create(:category, name: 'Seasons', name_en: 'Seasons') diff --git a/spec/factories.rb b/spec/factories.rb index ed2c90d50..c3f6394cd 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -10,6 +10,7 @@ password { '123foobar' } password_confirmation { '123foobar' } confirmed_at { Time.now } + main_topic_en { 'math' } factory :admin do admin { true } From 7d882ebf6a3da138491041f306c692eefed87abf Mon Sep 17 00:00:00 2001 From: Tyranja Date: Tue, 6 Feb 2024 09:37:38 +0100 Subject: [PATCH 2/5] add tool-versions --- .tool-versions | 1 + 1 file changed, 1 insertion(+) create mode 100644 .tool-versions diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 000000000..e7f4c1126 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +ruby 3.1.1 From 57ce53e3add69d3c28f778308f55ebe36cd74536 Mon Sep 17 00:00:00 2001 From: Tyranja Date: Tue, 6 Feb 2024 22:10:05 +0100 Subject: [PATCH 3/5] Fix showing profiles more than once --- app/controllers/profiles_controller.rb | 6 ++--- config/initializers/pagy.rb | 2 +- spec/controllers/profiles_controller_spec.rb | 28 +++++++++++++------- spec/factories.rb | 1 - 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb index 868bfaa03..c5420abd3 100644 --- a/app/controllers/profiles_controller.rb +++ b/app/controllers/profiles_controller.rb @@ -18,9 +18,9 @@ def index elsif params[:category_id] search_with_category_id elsif params[:tag_filter] - if params[:tag_filter].empty? + if params[:tag_filter].empty? redirect_to profiles_url(anchor: "top"), notice: I18n.t('flash.profiles.no_tags_selected') - return + return end search_with_tags else @@ -223,7 +223,7 @@ def profiles_for_index .by_region(current_region) .includes(:translations) .main_topic_translated_in(I18n.locale) - .random + # .random ) end diff --git a/config/initializers/pagy.rb b/config/initializers/pagy.rb index 1d881ba66..554164825 100644 --- a/config/initializers/pagy.rb +++ b/config/initializers/pagy.rb @@ -180,7 +180,7 @@ # Overflow extra: Allow for easy handling of overflowing pages # See https://ddnexus.github.io/pagy/docs/extras/overflow require 'pagy/extras/overflow' -Pagy::DEFAULT[:overflow] = :last_page # (other options: :empty_page and :exception) +Pagy::DEFAULT[:overflow] = :empty_page # (other options: :empty_page and :exception) # Support extra: Extra support for features like: incremental, infinite, auto-scroll pagination # See https://ddnexus.github.io/pagy/docs/extras/support diff --git a/spec/controllers/profiles_controller_spec.rb b/spec/controllers/profiles_controller_spec.rb index e7f58dd38..13a8809f6 100644 --- a/spec/controllers/profiles_controller_spec.rb +++ b/spec/controllers/profiles_controller_spec.rb @@ -3,10 +3,10 @@ describe ProfilesController, type: :controller do include AuthHelper - # let!(:profile_published) { create(:published_profile, topic_list: %w[ruby algorithms]) } - # let!(:profile_unpublished) { create(:unpublished_profile) } - # let!(:ada) { create(:published_profile, email: "ada@mail.org", main_topic_en: 'math') } - # let!(:admin) { create(:admin) } + let!(:profile_published) { create(:published_profile, topic_list: %w[ruby algorithms]) } + let!(:profile_unpublished) { create(:unpublished_profile) } + let!(:ada) { create(:published_profile, email: "ada@mail.org", main_topic_en: 'math') } + let!(:admin) { create(:admin) } describe 'test index action' do before do @@ -29,18 +29,28 @@ end context "pagination" do - let!(:ada) { create(:published_profile, email: "ada@mail.org", main_topic_en: 'math') } - it 'displays index page 2' do - create_list(:published_profile, 23) + it 'displays no profiles on index page 2' do + create_list(:published_profile, 23, main_topic_en: 'math') get :index, params: { page: 1 } - # byebug expect(assigns(:records).count).to eq 24 expect(assigns(:records)).to include ada get :index, params: { page: 2 } - expect(assigns(:records)).not_to include ada expect(assigns(:records).count).to eq 0 end + + it 'displays no profiles twice on index page 2' do + create_list(:published_profile, 25, main_topic_en: 'math') + + get :index, params: { page: 1 } + expect(assigns(:records).count).to eq 24 + profiles_page_1 = assigns(:records) + + get :index, params: { page: 2 } + expect(assigns(:records).count).to eq 2 + profiles_page_2 = assigns(:records) + expect(profiles_page_2 & profiles_page_1).to eq [] + end end describe 'category id search' do diff --git a/spec/factories.rb b/spec/factories.rb index c3f6394cd..ed2c90d50 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -10,7 +10,6 @@ password { '123foobar' } password_confirmation { '123foobar' } confirmed_at { Time.now } - main_topic_en { 'math' } factory :admin do admin { true } From 2b9008a5c67f6cb70ce17548646ee40eafdded7f Mon Sep 17 00:00:00 2001 From: Tyranja Date: Wed, 7 Feb 2024 22:16:29 +0100 Subject: [PATCH 4/5] nicer test --- app/controllers/profiles_controller.rb | 1 - spec/controllers/profiles_controller_spec.rb | 480 +++++++++---------- 2 files changed, 239 insertions(+), 242 deletions(-) diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb index c5420abd3..3600999f0 100644 --- a/app/controllers/profiles_controller.rb +++ b/app/controllers/profiles_controller.rb @@ -223,7 +223,6 @@ def profiles_for_index .by_region(current_region) .includes(:translations) .main_topic_translated_in(I18n.locale) - # .random ) end diff --git a/spec/controllers/profiles_controller_spec.rb b/spec/controllers/profiles_controller_spec.rb index 13a8809f6..f6ba760aa 100644 --- a/spec/controllers/profiles_controller_spec.rb +++ b/spec/controllers/profiles_controller_spec.rb @@ -3,306 +3,304 @@ describe ProfilesController, type: :controller do include AuthHelper - let!(:profile_published) { create(:published_profile, topic_list: %w[ruby algorithms]) } - let!(:profile_unpublished) { create(:unpublished_profile) } - let!(:ada) { create(:published_profile, email: "ada@mail.org", main_topic_en: 'math') } - let!(:admin) { create(:admin) } - - describe 'test index action' do - before do - get :index - end - - it 'displays index' do - expect(response).to be_successful - expect(response.response_code).to eq(200) - expect(response).to render_template('index') - end - - it 'displays published profiles' do - expect(assigns(:records).pluck(:id)).to eq ([ada.id]) - end - - it 'does not include unpublished profiles' do - expect(assigns(:profiles)).not_to include(profile_unpublished) - end - end - - context "pagination" do - it 'displays no profiles on index page 2' do - create_list(:published_profile, 23, main_topic_en: 'math') - - get :index, params: { page: 1 } - expect(assigns(:records).count).to eq 24 - expect(assigns(:records)).to include ada - get :index, params: { page: 2 } - expect(assigns(:records).count).to eq 0 - end - - it 'displays no profiles twice on index page 2' do - create_list(:published_profile, 25, main_topic_en: 'math') - - get :index, params: { page: 1 } - expect(assigns(:records).count).to eq 24 - profiles_page_1 = assigns(:records) - - get :index, params: { page: 2 } - expect(assigns(:records).count).to eq 2 - profiles_page_2 = assigns(:records) - expect(profiles_page_2 & profiles_page_1).to eq [] - end - end - - describe 'category id search' do - it 'stores the correct category when params has category id' do - category = FactoryBot.create(:category, name: 'Seasons', name_en: 'Seasons') - get :index, params: { category_id: category.id } - expect(assigns(:category)).to eq(category) - end - end - - describe 'tag filter search' do - it 'redirects to profile index when tags filter is empty' do - get :index, params: { tag_filter: "" } - expect(response).to redirect_to("/#{I18n.locale}/profiles#top") - end - end - - describe 'search action' do - it 'displays search results if search term is present' do - sleep 1 - get :index, params: { search: 'ruby' } - expect(response).to be_successful - end - - it 'should store aggregations in aggs variables' do - get :index, params: { search: 'ruby' } - expect(assigns(:aggs_cities)).to eq({}) - expect(assigns(:aggs_languages)).to eq({}) - expect(assigns(:aggs_countries)).to eq nil - expect(assigns(:aggs_states)).to eq nil - end - end + context 'single profile tests' do + let!(:profile_published) { create(:published_profile, topic_list: %w[ruby algorithms]) } + let!(:profile_unpublished) { create(:unpublished_profile) } + let!(:ada) { create(:published_profile, email: "ada@mail.org", main_topic_en: 'math') } + let!(:admin) { create(:admin) } - describe 'show profile' do - describe 'of unpublished profile' do - it 'is not permitted for unauthorized not signed in profile' do - get :show, params: { id: profile_unpublished.id } - expect(response).to redirect_to("/#{I18n.locale}/profiles") + describe 'test index action' do + before do + get :index end - it 'is not permitted for unauthorized signed in profile' do - sign_in ada - get :show, params: { id: profile_unpublished.id } - expect(response).to redirect_to("/#{I18n.locale}/profiles") + it 'displays index' do + expect(response).to be_successful + expect(response.response_code).to eq(200) + expect(response).to render_template('index') end - it 'is permitted for own profile' do - sign_in profile_unpublished - get :show, params: { id: profile_unpublished.id } - expect(response).to render_template(:show) + it 'displays published profiles' do + expect(assigns(:records).pluck(:id)).to eq ([ada.id]) end - it 'is permitted for admin' do - sign_in admin - get :show, params: { id: profile_unpublished.id } - expect(response).to render_template(:show) + it 'does not include unpublished profiles' do + expect(assigns(:profiles)).not_to include(profile_unpublished) end end - describe 'of published profile' do - it 'should be seen by all profiles' do - get :show, params: { id: profile_published.id } - expect(response).to render_template(:show) + describe 'category id search' do + it 'stores the correct category when params has category id' do + category = FactoryBot.create(:category, name: 'Seasons', name_en: 'Seasons') + get :index, params: { category_id: category.id } + expect(assigns(:category)).to eq(category) end end - end - - describe 'edit profile' do - - context 'when editing own profile' do - before do - sign_in ada - get :edit, params: { locale: 'de', id: ada.id } - end - it 'renders edit view' do - expect(response).to render_template(:edit) + describe 'tag filter search' do + it 'redirects to profile index when tags filter is empty' do + get :index, params: { tag_filter: "" } + expect(response).to redirect_to("/#{I18n.locale}/profiles#top") end end - context 'when trying to edit a different profile' do - before do - sign_in ada - get :edit, params: { locale: 'de', id: profile_published.id } - end - - it 'does not render edit view' do - expect(response).to_not render_template(:edit) + describe 'search action' do + it 'displays search results if search term is present' do + sleep 1 + get :index, params: { search: 'ruby' } + expect(response).to be_successful end - it 'redirects to profiles overview' do - expect(response).to redirect_to("/#{I18n.locale}/profiles") + it 'should store aggregations in aggs variables' do + get :index, params: { search: 'ruby' } + expect(assigns(:aggs_cities)).to eq({}) + expect(assigns(:aggs_languages)).to eq({}) + expect(assigns(:aggs_countries)).to eq nil + expect(assigns(:aggs_states)).to eq nil end end - context 'when trying edit profile if user is not signed in' do - before do - get :edit, params: { locale: 'de', id: ada.id } - end - - it 'does not render edit view' do - expect(response).to_not render_template(:edit) - end - - it 'redirects to profiles overview' do - expect(response).to redirect_to("/#{I18n.locale}/profiles") + describe 'show profile' do + describe 'of unpublished profile' do + it 'is not permitted for unauthorized not signed in profile' do + get :show, params: { id: profile_unpublished.id } + expect(response).to redirect_to("/#{I18n.locale}/profiles") + end + + it 'is not permitted for unauthorized signed in profile' do + sign_in ada + get :show, params: { id: profile_unpublished.id } + expect(response).to redirect_to("/#{I18n.locale}/profiles") + end + + it 'is permitted for own profile' do + sign_in profile_unpublished + get :show, params: { id: profile_unpublished.id } + expect(response).to render_template(:show) + end + + it 'is permitted for admin' do + sign_in admin + get :show, params: { id: profile_unpublished.id } + expect(response).to render_template(:show) + end + end + + describe 'of published profile' do + it 'should be seen by all profiles' do + get :show, params: { id: profile_published.id } + expect(response).to render_template(:show) + end end end - xit "doesn't create extra translations" do - de_translation = ada.translations.create!('locale' => 'de', 'main_topic' => 'Hauptthema') - en_translation = ada.translations.create!('locale' => 'en', 'main_topic' => 'Main topic') - - profile_params = { - translations_attributes: - { '0': - { - 'locale': 'de', - 'main_topic': 'Algorithmen', - 'bio': 'Deutsche Biografie', - 'id': de_translation.id - }, - '1': - { - 'locale': 'en', - 'main_topic': 'algorithms', - 'bio': 'English Bio', - 'id': en_translation.id - } } - } - patch :update, params: { id: ada.id }.merge(profile: profile_params) - - expect(ada.reload.translations.size).to eq(2) + describe 'edit profile' do + context 'when editing own profile' do + before do + sign_in ada + get :edit, params: { locale: 'de', id: ada.id } + end + + it 'renders edit view' do + expect(response).to render_template(:edit) + end + end + + context 'when trying to edit a different profile' do + before do + sign_in ada + get :edit, params: { locale: 'de', id: profile_published.id } + end + + it 'does not render edit view' do + expect(response).to_not render_template(:edit) + end + + it 'redirects to profiles overview' do + expect(response).to redirect_to("/#{I18n.locale}/profiles") + end + end + + context 'when trying edit profile if user is not signed in' do + before do + get :edit, params: { locale: 'de', id: ada.id } + end + + it 'does not render edit view' do + expect(response).to_not render_template(:edit) + end + + it 'redirects to profiles overview' do + expect(response).to redirect_to("/#{I18n.locale}/profiles") + end + end + + xit "doesn't create extra translations" do + de_translation = ada.translations.create!('locale' => 'de', 'main_topic' => 'Hauptthema') + en_translation = ada.translations.create!('locale' => 'en', 'main_topic' => 'Main topic') + + profile_params = { + translations_attributes: + { '0': + { + 'locale': 'de', + 'main_topic': 'Algorithmen', + 'bio': 'Deutsche Biografie', + 'id': de_translation.id + }, + '1': + { + 'locale': 'en', + 'main_topic': 'algorithms', + 'bio': 'English Bio', + 'id': en_translation.id + } } + } + patch :update, params: { id: ada.id }.merge(profile: profile_params) + + expect(ada.reload.translations.size).to eq(2) + end end - end - - describe 'update profile action' do + describe 'update profile action' do context 'when updating own profile with valid params' do - before do - sign_in ada - put :update, params: { id: ada.id, profile: { firstname: 'marie', lastname: 'curie' } } - end + before do + sign_in ada + put :update, params: { id: ada.id, profile: { firstname: 'marie', lastname: 'curie' } } + end - it 'updates the requested Profile' do - ada.reload - expect(ada.firstname).to eq 'marie' - end + it 'updates the requested Profile' do + ada.reload + expect(ada.firstname).to eq 'marie' + end - it 'redirects to the updated profile' do - expect(response).to redirect_to("/#{I18n.locale}/profiles/marie-curie") + it 'redirects to the updated profile' do + expect(response).to redirect_to("/#{I18n.locale}/profiles/marie-curie") + end end - end - context 'when invalid params are supplied' do - before do - sign_in ada - put :update, params: { id: ada.id, profile: { email: ' ' } } - end + context 'when invalid params are supplied' do + before do + sign_in ada + put :update, params: { id: ada.id, profile: { email: ' ' } } + end - it 'does not update the requested Profile' do - expect(ada.email).to eq('ada@mail.org') - end + it 'does not update the requested Profile' do + expect(ada.email).to eq('ada@mail.org') + end - it 'renders the edit template' do - expect(response).to render_template(:edit) + it 'renders the edit template' do + expect(response).to render_template(:edit) + end end - end - context 'when trying to update a different profile' do - before do - sign_in ada - put :update, params: { id: profile_published.id, profile: { firstname: 'marie', lastname: 'curie' } } - end + context 'when trying to update a different profile' do + before do + sign_in ada + put :update, params: { id: profile_published.id, profile: { firstname: 'marie', lastname: 'curie' } } + end - it 'does not update the requested profile' do - profile_published.reload - expect(profile_published.firstname).to eq('Susi') - end + it 'does not update the requested profile' do + profile_published.reload + expect(profile_published.firstname).to eq('Susi') + end - it 'redirects to profiles overview' do - expect(response).to redirect_to("/#{I18n.locale}/profiles") + it 'redirects to profiles overview' do + expect(response).to redirect_to("/#{I18n.locale}/profiles") + end end - end - context 'when trying update profile if user is not signed in' do - before do - put :update, params: { id: profile_published.id, profile: { firstname: 'marie', lastname: 'curie' } } - end + context 'when trying update profile if user is not signed in' do + before do + put :update, params: { id: profile_published.id, profile: { firstname: 'marie', lastname: 'curie' } } + end - it 'does not update the requested profile' do - profile_published.reload - expect(profile_published.firstname).to eq('Susi') - end + it 'does not update the requested profile' do + profile_published.reload + expect(profile_published.firstname).to eq('Susi') + end - it 'should redirect to profiles overview' do - expect(response).to redirect_to("/#{I18n.locale}/profiles") + it 'should redirect to profiles overview' do + expect(response).to redirect_to("/#{I18n.locale}/profiles") + end end end - end - describe 'destroy profile action' do + describe 'destroy profile action' do + context 'when destroying own profile' do + before do + sign_in ada + end - context 'when destroying own profile' do - before do - sign_in ada - end + it 'should destroy requested profile' do + expect do + delete :destroy, params: { id: ada.id } + end.to change(Profile, :count).by(-1) + end - it 'should destroy requested profile' do - expect do + it 'should not find the destroyed user' do delete :destroy, params: { id: ada.id } - end.to change(Profile, :count).by(-1) - end + expect { Profile.find(ada.id) }.to raise_exception(ActiveRecord::RecordNotFound) + end - it 'should not find the destroyed user' do - delete :destroy, params: { id: ada.id } - expect { Profile.find(ada.id) }.to raise_exception(ActiveRecord::RecordNotFound) + it 'should redirect to profiles overview' do + delete :destroy, params: { id: ada.id } + expect(response).to redirect_to("/#{I18n.locale}/profiles") + end end - it 'should redirect to profiles overview' do - delete :destroy, params: { id: ada.id } - expect(response).to redirect_to("/#{I18n.locale}/profiles") - end - end + context 'when trying to destroy a different profile' do + before do + sign_in ada + delete :destroy, params: { id: profile_published.id } + end - context 'when trying to destroy a different profile' do - before do - sign_in ada - delete :destroy, params: { id: profile_published.id } - end + it 'should not destroy the requested profile' do + expect(Profile.where(id: profile_published.id).count).to eq 1 + end - it 'should not destroy the requested profile' do - expect(Profile.where(id: profile_published.id).count).to eq 1 + it 'should redirect to profiles overview' do + expect(response).to redirect_to("/#{I18n.locale}/profiles") + end end - it 'should redirect to profiles overview' do - expect(response).to redirect_to("/#{I18n.locale}/profiles") + context 'when trying destroy profile if user is not signed in' do + before do + delete :destroy, params: { id: ada.id } + end + + it 'should not destroy the requested profile' do + expect(Profile.where(id: ada.id).count).to eq 1 + end + + it 'should redirect to profiles overview' do + expect(response).to redirect_to("/#{I18n.locale}/profiles") + end end end + end - context 'when trying destroy profile if user is not signed in' do - before do - delete :destroy, params: { id: ada.id } - end + context "pagination with multiple profiles" do + it 'displays no profiles on index page 2' do + published_profiles = create_list(:published_profile, 24, main_topic_en: 'math') + get :index, params: { page: 1 } + expect(assigns(:records).count).to eq 24 + expect(assigns(:records)).to eq published_profiles + get :index, params: { page: 2 } + expect(assigns(:records).count).to eq 0 + end - it 'should not destroy the requested profile' do - expect(Profile.where(id: ada.id).count).to eq 1 - end + it 'displays no profiles twice on index page 2' do + create_list(:published_profile, 26, main_topic_en: 'math') - it 'should redirect to profiles overview' do - expect(response).to redirect_to("/#{I18n.locale}/profiles") - end + get :index, params: { page: 1 } + expect(assigns(:records).count).to eq 24 + profiles_page_1 = assigns(:records) + + get :index, params: { page: 2 } + expect(assigns(:records).count).to eq 2 + profiles_page_2 = assigns(:records) + expect(profiles_page_2 & profiles_page_1).to eq [] end end end From 7ea602e3cf8552cce372c5ec6665e3a945a98a46 Mon Sep 17 00:00:00 2001 From: Tyranja Date: Sat, 17 Feb 2024 17:25:50 +0100 Subject: [PATCH 5/5] rewrite test and new order the profiles --- app/controllers/profiles_controller.rb | 1 + spec/controllers/profiles_controller_spec.rb | 449 ++++++++++--------- 2 files changed, 228 insertions(+), 222 deletions(-) diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb index 3600999f0..f76c7253d 100644 --- a/app/controllers/profiles_controller.rb +++ b/app/controllers/profiles_controller.rb @@ -223,6 +223,7 @@ def profiles_for_index .by_region(current_region) .includes(:translations) .main_topic_translated_in(I18n.locale) + .order(created_at: :desc) ) end diff --git a/spec/controllers/profiles_controller_spec.rb b/spec/controllers/profiles_controller_spec.rb index f6ba760aa..5d8bc016e 100644 --- a/spec/controllers/profiles_controller_spec.rb +++ b/spec/controllers/profiles_controller_spec.rb @@ -2,296 +2,294 @@ describe ProfilesController, type: :controller do include AuthHelper + let!(:profile_published) { create(:published_profile, topic_list: %w[ruby algorithms]) } + let!(:profile_unpublished) { create(:unpublished_profile) } + let!(:ada) { create(:published_profile, email: "ada@mail.org", main_topic_en: 'math') } + let!(:admin) { create(:admin) } + + describe 'index' do + before do + get :index + end - context 'single profile tests' do - let!(:profile_published) { create(:published_profile, topic_list: %w[ruby algorithms]) } - let!(:profile_unpublished) { create(:unpublished_profile) } - let!(:ada) { create(:published_profile, email: "ada@mail.org", main_topic_en: 'math') } - let!(:admin) { create(:admin) } + it 'displays index template' do + expect(response).to be_successful + expect(response.response_code).to eq(200) + expect(response).to render_template('index') + end - describe 'test index action' do - before do - get :index + it 'displays published profiles' do + expect(assigns(:records).pluck(:id)).to eq ([ada.id]) + end + + it 'does not include unpublished profiles' do + expect(assigns(:profiles)).not_to include(profile_unpublished) + end + end + + describe '#search_with_category_id' do + it 'stores the correct category when params has category id' do + category = FactoryBot.create(:category, name: 'Seasons', name_en: 'Seasons') + get :index, params: { category_id: category.id } + expect(assigns(:category)).to eq(category) + end + end + + describe '#search_with_tags' do + it 'redirects to profile index when tags filter is empty' do + get :index, params: { tag_filter: "" } + expect(response).to redirect_to("/#{I18n.locale}/profiles#top") + end + end + + describe '#search_with_search_params' do + it 'displays search results if search term is present' do + sleep 1 + get :index, params: { search: 'ruby' } + expect(response).to be_successful + end + + it 'should store aggregations in aggs variables' do + get :index, params: { search: 'ruby' } + expect(assigns(:aggs_cities)).to eq({}) + expect(assigns(:aggs_languages)).to eq({}) + expect(assigns(:aggs_countries)).to eq nil + expect(assigns(:aggs_states)).to eq nil + end + end + + describe 'show' do + describe 'of unpublished profile' do + it 'is not permitted for unauthorized not signed in profile' do + get :show, params: { id: profile_unpublished.id } + expect(response).to redirect_to("/#{I18n.locale}/profiles") end - it 'displays index' do - expect(response).to be_successful - expect(response.response_code).to eq(200) - expect(response).to render_template('index') + it 'is not permitted for unauthorized signed in profile' do + sign_in ada + get :show, params: { id: profile_unpublished.id } + expect(response).to redirect_to("/#{I18n.locale}/profiles") end - it 'displays published profiles' do - expect(assigns(:records).pluck(:id)).to eq ([ada.id]) + it 'is permitted for own profile' do + sign_in profile_unpublished + get :show, params: { id: profile_unpublished.id } + expect(response).to render_template(:show) end - it 'does not include unpublished profiles' do - expect(assigns(:profiles)).not_to include(profile_unpublished) + it 'is permitted for admin' do + sign_in admin + get :show, params: { id: profile_unpublished.id } + expect(response).to render_template(:show) end end - describe 'category id search' do - it 'stores the correct category when params has category id' do - category = FactoryBot.create(:category, name: 'Seasons', name_en: 'Seasons') - get :index, params: { category_id: category.id } - expect(assigns(:category)).to eq(category) + describe 'of published profile' do + it 'should be seen by all profiles' do + get :show, params: { id: profile_published.id } + expect(response).to render_template(:show) end end + end - describe 'tag filter search' do - it 'redirects to profile index when tags filter is empty' do - get :index, params: { tag_filter: "" } - expect(response).to redirect_to("/#{I18n.locale}/profiles#top") + describe '#edit' do + context 'own profile' do + before do + sign_in ada + get :edit, params: { locale: 'de', id: ada.id } + end + + it 'renders edit view' do + expect(response).to render_template(:edit) end end - describe 'search action' do - it 'displays search results if search term is present' do - sleep 1 - get :index, params: { search: 'ruby' } - expect(response).to be_successful + context "other people's profile" do + before do + sign_in ada + get :edit, params: { locale: 'de', id: profile_published.id } end - it 'should store aggregations in aggs variables' do - get :index, params: { search: 'ruby' } - expect(assigns(:aggs_cities)).to eq({}) - expect(assigns(:aggs_languages)).to eq({}) - expect(assigns(:aggs_countries)).to eq nil - expect(assigns(:aggs_states)).to eq nil + it 'does not render edit view' do + expect(response).to_not render_template(:edit) end - end - describe 'show profile' do - describe 'of unpublished profile' do - it 'is not permitted for unauthorized not signed in profile' do - get :show, params: { id: profile_unpublished.id } - expect(response).to redirect_to("/#{I18n.locale}/profiles") - end - - it 'is not permitted for unauthorized signed in profile' do - sign_in ada - get :show, params: { id: profile_unpublished.id } - expect(response).to redirect_to("/#{I18n.locale}/profiles") - end - - it 'is permitted for own profile' do - sign_in profile_unpublished - get :show, params: { id: profile_unpublished.id } - expect(response).to render_template(:show) - end - - it 'is permitted for admin' do - sign_in admin - get :show, params: { id: profile_unpublished.id } - expect(response).to render_template(:show) - end - end - - describe 'of published profile' do - it 'should be seen by all profiles' do - get :show, params: { id: profile_published.id } - expect(response).to render_template(:show) - end + it 'redirects to profiles overview' do + expect(response).to redirect_to("/#{I18n.locale}/profiles") end end - describe 'edit profile' do - context 'when editing own profile' do - before do - sign_in ada - get :edit, params: { locale: 'de', id: ada.id } - end - - it 'renders edit view' do - expect(response).to render_template(:edit) - end - end - - context 'when trying to edit a different profile' do - before do - sign_in ada - get :edit, params: { locale: 'de', id: profile_published.id } - end - - it 'does not render edit view' do - expect(response).to_not render_template(:edit) - end - - it 'redirects to profiles overview' do - expect(response).to redirect_to("/#{I18n.locale}/profiles") - end - end - - context 'when trying edit profile if user is not signed in' do - before do - get :edit, params: { locale: 'de', id: ada.id } - end - - it 'does not render edit view' do - expect(response).to_not render_template(:edit) - end - - it 'redirects to profiles overview' do - expect(response).to redirect_to("/#{I18n.locale}/profiles") - end - end - - xit "doesn't create extra translations" do - de_translation = ada.translations.create!('locale' => 'de', 'main_topic' => 'Hauptthema') - en_translation = ada.translations.create!('locale' => 'en', 'main_topic' => 'Main topic') - - profile_params = { - translations_attributes: - { '0': - { - 'locale': 'de', - 'main_topic': 'Algorithmen', - 'bio': 'Deutsche Biografie', - 'id': de_translation.id - }, - '1': - { - 'locale': 'en', - 'main_topic': 'algorithms', - 'bio': 'English Bio', - 'id': en_translation.id - } } - } - patch :update, params: { id: ada.id }.merge(profile: profile_params) - - expect(ada.reload.translations.size).to eq(2) + context 'own profile but not signed in' do + before do + get :edit, params: { locale: 'de', id: ada.id } + end + + it 'does not render edit view' do + expect(response).to_not render_template(:edit) + end + + it 'redirects to profiles overview' do + expect(response).to redirect_to("/#{I18n.locale}/profiles") end end - describe 'update profile action' do - context 'when updating own profile with valid params' do - before do - sign_in ada - put :update, params: { id: ada.id, profile: { firstname: 'marie', lastname: 'curie' } } - end + xit "doesn't create extra translations" do + de_translation = ada.translations.create!('locale' => 'de', 'main_topic' => 'Hauptthema') + en_translation = ada.translations.create!('locale' => 'en', 'main_topic' => 'Main topic') + + profile_params = { + translations_attributes: + { '0': + { + 'locale': 'de', + 'main_topic': 'Algorithmen', + 'bio': 'Deutsche Biografie', + 'id': de_translation.id + }, + '1': + { + 'locale': 'en', + 'main_topic': 'algorithms', + 'bio': 'English Bio', + 'id': en_translation.id + } } + } + patch :update, params: { id: ada.id }.merge(profile: profile_params) + + expect(ada.reload.translations.size).to eq(2) + end + end + + describe '#update' do + context 'with valid params' do + before do + sign_in ada + put :update, params: { id: ada.id, profile: { firstname: 'marie', lastname: 'curie' } } + end - it 'updates the requested Profile' do - ada.reload - expect(ada.firstname).to eq 'marie' - end + it 'updates the requested Profile' do + ada.reload + expect(ada.firstname).to eq 'marie' + end - it 'redirects to the updated profile' do - expect(response).to redirect_to("/#{I18n.locale}/profiles/marie-curie") - end + it 'redirects to the updated profile' do + expect(response).to redirect_to("/#{I18n.locale}/profiles/marie-curie") end + end - context 'when invalid params are supplied' do - before do - sign_in ada - put :update, params: { id: ada.id, profile: { email: ' ' } } - end + context 'with invalid params' do + before do + sign_in ada + put :update, params: { id: ada.id, profile: { email: ' ' } } + end - it 'does not update the requested Profile' do - expect(ada.email).to eq('ada@mail.org') - end + it 'does not update the requested Profile' do + expect(ada.email).to eq('ada@mail.org') + end - it 'renders the edit template' do - expect(response).to render_template(:edit) - end + it 'renders the edit template' do + expect(response).to render_template(:edit) end + end - context 'when trying to update a different profile' do - before do - sign_in ada - put :update, params: { id: profile_published.id, profile: { firstname: 'marie', lastname: 'curie' } } - end + context "other people's profile" do + before do + sign_in ada + put :update, params: { id: profile_published.id, profile: { firstname: 'marie', lastname: 'curie' } } + end - it 'does not update the requested profile' do - profile_published.reload - expect(profile_published.firstname).to eq('Susi') - end + it 'does not update' do + profile_published.reload + expect(profile_published.firstname).to eq('Susi') + end - it 'redirects to profiles overview' do - expect(response).to redirect_to("/#{I18n.locale}/profiles") - end + it 'redirects to profiles overview' do + expect(response).to redirect_to("/#{I18n.locale}/profiles") end + end - context 'when trying update profile if user is not signed in' do - before do - put :update, params: { id: profile_published.id, profile: { firstname: 'marie', lastname: 'curie' } } - end + context 'when user is not signed in' do + before do + put :update, params: { id: profile_published.id, profile: { firstname: 'marie', lastname: 'curie' } } + end - it 'does not update the requested profile' do - profile_published.reload - expect(profile_published.firstname).to eq('Susi') - end + it 'does not update' do + profile_published.reload + expect(profile_published.firstname).to eq('Susi') + end - it 'should redirect to profiles overview' do - expect(response).to redirect_to("/#{I18n.locale}/profiles") - end + it 'redirects to profiles overview' do + expect(response).to redirect_to("/#{I18n.locale}/profiles") end end + end - describe 'destroy profile action' do - context 'when destroying own profile' do - before do - sign_in ada - end - - it 'should destroy requested profile' do - expect do - delete :destroy, params: { id: ada.id } - end.to change(Profile, :count).by(-1) - end + describe '#destroy' do + context 'own profile' do + before do + sign_in ada + end - it 'should not find the destroyed user' do + it 'should destroy profile' do + expect do delete :destroy, params: { id: ada.id } - expect { Profile.find(ada.id) }.to raise_exception(ActiveRecord::RecordNotFound) - end + end.to change(Profile, :count).by(-1) + end - it 'should redirect to profiles overview' do - delete :destroy, params: { id: ada.id } - expect(response).to redirect_to("/#{I18n.locale}/profiles") - end + it 'should not find the destroyed profile' do + delete :destroy, params: { id: ada.id } + expect { Profile.find(ada.id) }.to raise_exception(ActiveRecord::RecordNotFound) end - context 'when trying to destroy a different profile' do - before do - sign_in ada - delete :destroy, params: { id: profile_published.id } - end + it 'should redirect to profiles overview' do + delete :destroy, params: { id: ada.id } + expect(response).to redirect_to("/#{I18n.locale}/profiles") + end + end - it 'should not destroy the requested profile' do - expect(Profile.where(id: profile_published.id).count).to eq 1 - end + context "other people's profile" do + before do + sign_in ada + delete :destroy, params: { id: profile_published.id } + end - it 'should redirect to profiles overview' do - expect(response).to redirect_to("/#{I18n.locale}/profiles") - end + it 'should not destroy' do + expect(Profile.where(id: profile_published.id).count).to eq 1 end - context 'when trying destroy profile if user is not signed in' do - before do - delete :destroy, params: { id: ada.id } - end + it 'should redirect to profiles overview' do + expect(response).to redirect_to("/#{I18n.locale}/profiles") + end + end + + context 'when trying destroy profile if user is not signed in' do + before do + delete :destroy, params: { id: ada.id } + end - it 'should not destroy the requested profile' do - expect(Profile.where(id: ada.id).count).to eq 1 - end + it 'should not destroy the requested profile' do + expect(Profile.where(id: ada.id).count).to eq 1 + end - it 'should redirect to profiles overview' do - expect(response).to redirect_to("/#{I18n.locale}/profiles") - end + it 'should redirect to profiles overview' do + expect(response).to redirect_to("/#{I18n.locale}/profiles") end end end context "pagination with multiple profiles" do it 'displays no profiles on index page 2' do - published_profiles = create_list(:published_profile, 24, main_topic_en: 'math') + published_profiles = create_list(:published_profile, 23, main_topic_en: 'math') get :index, params: { page: 1 } + # we have with previous created profile 24 in total expect(assigns(:records).count).to eq 24 - expect(assigns(:records)).to eq published_profiles + expect(assigns(:records).to_a).to match_array(published_profiles << ada) get :index, params: { page: 2 } expect(assigns(:records).count).to eq 0 end it 'displays no profiles twice on index page 2' do - create_list(:published_profile, 26, main_topic_en: 'math') + create_list(:published_profile, 25, main_topic_en: 'math') get :index, params: { page: 1 } expect(assigns(:records).count).to eq 24 @@ -302,5 +300,12 @@ profiles_page_2 = assigns(:records) expect(profiles_page_2 & profiles_page_1).to eq [] end + + it 'order by last created' do + published_profiles = create_list(:published_profile, 23, main_topic_en: 'math') + last_created_profile = published_profiles.last + get :index, params: { page: 1 } + expect(assigns(:records).first).to eq last_created_profile + end end end