Skip to content

Commit

Permalink
update rspec tests adding polymorphic relations to comments and likes
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmbrasil committed Jul 8, 2021
1 parent b5de2c1 commit 7500bea
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 28 deletions.
28 changes: 7 additions & 21 deletions spec/controllers/api/v1/comments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

before do
post "/api/v1/username/posts/#{post_with_no_media.id}/comment", params: {
body: body
comment: {
body: body
}
}, headers: {
Authorization: "Token api_key=#{bot.api_key} api_secret=#{bot.api_secret}"
}
Expand All @@ -33,7 +35,9 @@
context 'When creating a comment with no body' do
before do
post "/api/v1/username/posts/#{post_with_no_media.id}/comment", params: {
body: ''
comment: {
body: ''
}
}, headers: {
Authorization: "Token api_key=#{bot.api_key} api_secret=#{bot.api_secret}"
}
Expand All @@ -49,27 +53,9 @@
end
end

context 'When fetching a comment' do
before do
get "/api/v1/username/posts/#{comment.post_id}/comment/#{comment.id}", headers: {
Authorization: "Token api_key=#{bot.api_key} api_secret=#{bot.api_secret}"
}
end

it 'returns 200' do
expect(response.status).to eq(200)
end

it 'returns success message and comment data' do
expect(json['status']).to eq('success')
expect(json['message']).to eq('Comment loaded')
expect(json['data']['body']).to eq(comment.body)
end
end

context 'When deleting another bot comment' do
before do
delete "/api/v1/username/posts/#{comment.post_id}/comment/#{comment.id}", headers: {
delete "/api/v1/username/posts/#{comment.commentable_id}/comment/#{comment.id}", headers: {
Authorization: "Token api_key=#{bot.api_key} api_secret=#{bot.api_secret}"
}
end
Expand Down
43 changes: 43 additions & 0 deletions spec/controllers/api/v1/likes_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,47 @@
expect(json['message']).to eq('Cannot unlike')
end
end

let(:comment) { create_comment }

context 'When liking a comment' do
before do
post "/api/v1/username/posts/#{post_with_no_media.id}/comment", params: {
comment: {
body: 'Comment'
}
}, headers: {
Authorization: "Token api_key=#{bot.api_key} api_secret=#{bot.api_secret}"
}
post "/api/v1/username/posts/#{post_with_no_media.id}/comments/#{comment.id}/like", headers: {
Authorization: "Token api_key=#{bot.api_key} api_secret=#{bot.api_secret}"
}
end

it 'returns 201' do
expect(response.status).to eq(201)
end
end

context 'When unliking a comment' do
before do
post "/api/v1/username/posts/#{post_with_no_media.id}/comment", params: {
comment: {
body: 'Comment'
}
}, headers: {
Authorization: "Token api_key=#{bot.api_key} api_secret=#{bot.api_secret}"
}
post "/api/v1/username/posts/#{post_with_no_media.id}/comments/#{comment.id}/like", headers: {
Authorization: "Token api_key=#{bot.api_key} api_secret=#{bot.api_secret}"
}
delete "/api/v1/username/posts/#{post_with_no_media.id}/comments/#{comment.id}/like", headers: {
Authorization: "Token api_key=#{bot.api_key} api_secret=#{bot.api_secret}"
}
end

it 'returns 202' do
expect(response.status).to eq(202)
end
end
end
4 changes: 2 additions & 2 deletions spec/factories/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

FactoryBot.define do
factory :bot do
name { Faker::Name.name[4..32] }
username { SecureRandom.hex(10) }
name { Faker::Name.name[0..32] }
username { SecureRandom.hex(10) }
bio { Faker::Book.title[1..512] }
developer_id { FactoryBot.create(:developer).id }
api_key { SecureRandom.hex(16) }
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/developer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

FactoryBot.define do
factory :developer do
name { Faker::Name.name[4..32] }
name { Faker::Name.name[0..32] }
username { SecureRandom.hex(10) }
email { Faker::Internet.unique.email[0..32] }
end
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
FactoryBot.define do
factory :post do
body { Faker::Games::Minecraft.achievement[0..32] }
bot_id { FactoryBot.create(:bot).id[0..32] }
bot_id { FactoryBot.create(:bot).id }
end
end
2 changes: 1 addition & 1 deletion spec/support/bot_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
module BotHelpers
def create_bot
FactoryBot.create(:bot,
name: Faker::Name.name[4..32],
name: Faker::Name.name[0..32],
username: SecureRandom.hex(10),
api_key: SecureRandom.hex(16),
api_secret: SecureRandom.hex(16),
Expand Down
6 changes: 4 additions & 2 deletions spec/support/comment_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ module CommentHelpers
def create_comment
FactoryBot.create(:comment,
body: Faker::Games::Fallout.quote,
post_id: FactoryBot.create(:post).id,
bot_id: FactoryBot.create(:bot).id)
commentable_id: FactoryBot.create(:post).id,
commentable_type: 'Post',
commenter_id: FactoryBot.create(:bot).id,
commenter_type: 'Bot')
end
end

0 comments on commit 7500bea

Please sign in to comment.