Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test post type pk bug #392

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/assets/javascripts/application/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,11 @@ function addPost(group_id, membership_id, post_type_id) {
button.disabled = false
});
}
if(response.status == 403){
response.json().then(data => {
UIkit.notify({message: data.message, timeout: 3000, status: 'danger'})
button.disabled = false
})
}
})
}
18 changes: 13 additions & 5 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,23 @@ def create
membership = Membership.find(params[:membership_id])
post_type_id = params[:post_type_id].to_i
authorize Post.new(membership: membership, post_type_id: post_type_id)

new_post = CreatePost.call(group, membership, post_type_id)
begin
new_post = CreatePost.call(group, membership, post_type_id)
rescue CreatePost::PostAlreadyTaken
post_taken = true
end
respond_to do |format|
format.html do
return redirect_to group_path(group) if new_post.leader?
flash[:notice] = t('services.create_post.post_already_taken') if post_taken
return redirect_to group_path(group) if new_post&.leader?

redirect_to group_membership_posts_path(group, membership)
end
format.json do
return render status: :forbidden, json: { message: t('services.create_post.post_already_taken') } if post_taken

redirect_back fallback_location: group_path(group)
render json: { post_id: new_post.id }
end
format.json { render json: { post_id: new_post.id } }
end
end

Expand Down
10 changes: 10 additions & 0 deletions app/services/create_post.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class CreatePost
class InvalidPostType < StandardError; end
class PostAlreadyTaken < StandardError; end

def self.call(group, membership, post_type_id)
raise InvalidPostType unless group.has_post_type?(post_type_id)
Expand All @@ -9,6 +10,9 @@ def self.call(group, membership, post_type_id)
remove_leader(group)
remove_past_leader_post(membership)
end
if post_type_id == PostType::FINANCIAL_OFFICER_POST_ID
raise PostAlreadyTaken unless financial_officer_count_for(group).zero?
end
Post.create(membership_id: membership.id, post_type_id: post_type_id)
end
end
Expand All @@ -21,4 +25,10 @@ def self.remove_leader(group)
def self.remove_past_leader_post(membership)
Post.where(membership_id: membership.id, post_type_id: PostType::PAST_LEADER_ID).destroy_all
end

def self.financial_officer_count_for(group)
Post.where('memberships.group_id': group.id,
post_type_id: PostType::FINANCIAL_OFFICER_POST_ID)
.joins(:membership).count
end
end
3 changes: 3 additions & 0 deletions config/locales/hu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,6 @@ hu:
next: "Következő &raquo;"
last: "Utolsó"
truncate: "..."
services:
create_post:
post_already_taken: 'Ezt a posztot már kiosztottad valakinek! Vissza kell venned a jelenlegi posztot, hogy újra kiosztható legyen.'
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class RemoveFinancialOfficersIfMoreThenOne < ActiveRecord::Migration[6.0]
def up
groups_with_fo_count = Group.joins(memberships: :posts)
.where('posts.post_type_id': PostType::FINANCIAL_OFFICER_POST_ID)
.group('memberships.group_id').count('groups.id')
group_ids_with_more_fo = groups_with_fo_count.select { |k, v| v > 1 }.keys
groups_with_more_fo = Group.where(id: group_ids_with_more_fo)
groups_with_more_fo.each do |group|
fo_posts = Post.where('memberships.group_id': group.id,
post_type_id: PostType::FINANCIAL_OFFICER_POST_ID)
.joins(:membership).order('posts.id': :asc)
fo_posts[0..-2].each { |post| post.destroy! }
end
end

def down;end
end
3 changes: 2 additions & 1 deletion db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20200722173131'),
('20201223070757'),
('20210307164901'),
('20210905120558');
('20210905120558'),
('20220520160742');


2 changes: 2 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

config.before(:suite) do
FactoryBot.create(:post_type_leader)
FactoryBot.create(:post_type_financial_officer)
FactoryBot.create(:post_type_newbie)
FactoryBot.create(:post_type_evaluation_helper)
FactoryBot.create(:group_svie)
Expand All @@ -63,6 +64,7 @@
end

config.after(:suite) do
# Post.delete_all
PostType.delete_all
Group.delete_all
User.delete_all
Expand Down
1 change: 1 addition & 0 deletions spec/requests/post_type_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
let(:group) { create(:group) }
let(:user) { group.leader.user }
before(:each) { login_as(user) }
# before(:each) { PostType.destroy(PostType::FINANCIAL_OFFICER_POST_ID) }

it 'redirects back' do
post "/groups/#{group.id}/post_types", params: {
Expand Down
59 changes: 59 additions & 0 deletions spec/requests/posts_controller_spec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,65 @@
expect(response).to have_http_status :ok
expect(response).to render_template :group_posts
end

describe '#create' do
let(:membership) { group.memberships.first }
it 'creates a post and redirects' do
expect {
post "/groups/#{group.id}/memberships/#{membership.id}/posts",
params: { post_type_id: PostType::FINANCIAL_OFFICER_POST_ID }
}.to change {
membership.posts.count
}.by(1)
expect(response).to redirect_to("/groups/#{group.id}/memberships/#{membership.id}/posts")
end

it 'when the post is for the leader it redirects to the group page' do
post "/groups/#{group.id}/memberships/#{membership.id}/posts",
params: { post_type_id: PostType::LEADER_POST_ID }

expect(response).to redirect_to("/groups/#{group.id}")
end

context 'when FINANCIAL_OFFICER is present' do
before(:each) do
CreatePost::call(group, membership, PostType::FINANCIAL_OFFICER_POST_ID)
end
it 'creating another FINANCIAL_OFFICER is not processed' do
expect {
post "/groups/#{group.id}/memberships/#{membership.id}/posts",
params: { post_type_id: PostType::FINANCIAL_OFFICER_POST_ID }
}.to change {
membership.posts.count
}.by(0)

expect(flash[:notice]).to eql(I18n.t('services.create_post.post_already_taken'))
expect(response).to redirect_to("/groups/#{group.id}/memberships/#{membership.id}/posts")
end
end

context 'when format is json ' do
it 'sends back the post id' do
post "/groups/#{group.id}/memberships/#{membership.id}/posts.json",
params: { post_type_id: PostType::FINANCIAL_OFFICER_POST_ID }

expect(response.body).to eql({ post_id: Post.last.id }.to_json)
end

context 'when FINANCIAL_OFFICER is present' do
before(:each) do
CreatePost::call(group, membership, PostType::FINANCIAL_OFFICER_POST_ID)
end
it 'creating another FINANCIAL_OFFICER is forbidden' do
post "/groups/#{group.id}/memberships/#{membership.id}/posts.json",
params: { post_type_id: PostType::FINANCIAL_OFFICER_POST_ID }

expect(response).to have_http_status :forbidden
expect(response.body).to eql({ message: I18n.t('services.create_post.post_already_taken') }.to_json)
end
end
end
end
end
end
end
17 changes: 12 additions & 5 deletions test/factories/post_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@
sequence(:id, 150)
end

factory :post_type_leader, parent: :post_type do
factory :common_post_type, parent: :post_type do
group { nil }
end
factory :post_type_financial_officer, parent: :common_post_type do
id { PostType::FINANCIAL_OFFICER_POST_ID }
name { 'Gazdasagis' }
end

factory :post_type_leader, parent: :common_post_type do
id { PostType::LEADER_POST_ID }
name { 'Korvezeto' }
end
Expand All @@ -17,20 +25,19 @@
name { 'Feldolgozas alatt' }
end

factory :post_type_pek_admin, parent: :post_type do
factory :post_type_pek_admin, parent: :common_post_type do
group { Group.kirdev }
id { PostType::PEK_ADMIN_ID }
name { 'PeK admin' }
end

factory :post_type_new_member, parent: :post_type do
factory :post_type_new_member, parent: :common_post_type do
id { PostType::NEW_MEMBER_ID }
name { 'Ujonc' }
end

factory :post_type_evaluation_helper, parent: :post_type do
factory :post_type_evaluation_helper, parent: :common_post_type do
id { PostType::EVALUATION_HELPER_ID }
group { nil }
name { 'Pontozó' }
end
end