Adapt API tests to recent changes

This commit is contained in:
Frank Rousseau 2017-11-05 22:30:56 +01:00
parent 50e034769f
commit b00df8c2e7
7 changed files with 153 additions and 72 deletions

View file

@ -2,18 +2,26 @@
require "spec_helper" require "spec_helper"
describe Api::V0::PostsController do describe Api::V0::CommentsController do
let(:auth) { FactoryGirl.create(:auth_with_read_and_write) } let(:auth) { FactoryGirl.create(:auth_with_read_and_write) }
let!(:access_token) { auth.create_access_token.to_s } let!(:access_token) { auth.create_access_token.to_s }
before do before do
@status = auth.user.post(:status_message, text: "This is a status message", public: true, to: "all") @status = auth.user.post("Post",
status_message: {text: "This is a status message"},
public: true,
to: "all",
type: "Post"
)
end end
describe "#create" do describe "#create" do
context "valid post ID" do context "valid post ID" do
it "succeeds" do it "succeeds" do
post api_v0_post_comments_path(post_id: @status.id), text: "This is a comment", access_token: access_token post(
api_v0_post_comments_path(post_id: @status.id),
params: {text: "This is a comment", access_token: access_token}
)
expect(JSON.parse(response.body)["text"]).to eq("This is a comment") expect(JSON.parse(response.body)["text"]).to eq("This is a comment")
end end
end end
@ -22,7 +30,10 @@ describe Api::V0::PostsController do
before do before do
post( post(
api_v0_post_comments_path(post_id: @status.id), api_v0_post_comments_path(post_id: @status.id),
text: "This is a long comment" * 99_999, access_token: access_token params: {
text: "This is a long comment" * 99_999,
access_token: access_token
}
) )
end end
@ -35,23 +46,35 @@ describe Api::V0::PostsController do
describe "#delete" do describe "#delete" do
context "valid comment ID" do context "valid comment ID" do
before do before do
post api_v0_post_comments_path(post_id: @status.id), text: "This is a comment", access_token: access_token post(
api_v0_post_comments_path(post_id: @status.id),
params: {text: "This is a comment", access_token: access_token}
)
end end
it "succeeds" do it "succeeds" do
first_comment_id = JSON.parse(response.body)["id"] first_comment_id = JSON.parse(response.body)["id"]
delete api_v0_post_comment_path(id: first_comment_id), access_token: access_token delete(
expect(response.body).to eq("Comment " + first_comment_id.to_s + " has been successfully deleted") api_v0_post_comment_path(id: first_comment_id),
params: {access_token: access_token}
)
expect(response).to be_success
end end
end end
context "invalid comment ID" do context "invalid comment ID" do
before do before do
post api_v0_post_comments_path(post_id: @status.id), text: "This is a comment", access_token: access_token post(
api_v0_post_comments_path(post_id: @status.id),
params: {text: "This is a comment", access_token: access_token}
)
end end
it "fails to delete" do it "fails to delete" do
delete api_v0_post_comment_path(id: 1_234_567), access_token: access_token delete(
api_v0_post_comment_path(id: 1_234_567),
params: {access_token: access_token}
)
expect(response.body).to eq("Post or comment not found") expect(response.body).to eq("Post or comment not found")
end end
end end

View file

@ -25,7 +25,7 @@ describe Api::V0::ConversationsController do
describe "#create" do describe "#create" do
context "with valid data" do context "with valid data" do
it "creates the conversation" do it "creates the conversation" do
post api_v0_conversations_path, @conversation post api_v0_conversations_path, params: @conversation
@conversation_guid = JSON.parse(response.body)["conversation"]["guid"] @conversation_guid = JSON.parse(response.body)["conversation"]["guid"]
conversation = JSON.parse(response.body)["conversation"] conversation = JSON.parse(response.body)["conversation"]
@ -40,7 +40,7 @@ describe Api::V0::ConversationsController do
context "without valid data" do context "without valid data" do
it "fails at creating the conversation" do it "fails at creating the conversation" do
post api_v0_conversations_path, access_token: access_token post api_v0_conversations_path, params: { access_token: access_token }
expect(response.status).to eq 400 expect(response.status).to eq 400
end end
end end
@ -48,10 +48,10 @@ describe Api::V0::ConversationsController do
describe "#index" do describe "#index" do
before do before do
post api_v0_conversations_path, @conversation post api_v0_conversations_path, params: @conversation
post api_v0_conversations_path, @conversation post api_v0_conversations_path, params: @conversation
sleep(1) sleep(1)
post api_v0_conversations_path, @conversation post api_v0_conversations_path, params: @conversation
conversation_guid = JSON.parse(response.body)["conversation"]["guid"] conversation_guid = JSON.parse(response.body)["conversation"]["guid"]
conversation = conversation_service.find!(conversation_guid) conversation = conversation_service.find!(conversation_guid)
conversation.conversation_visibilities[0].unread = 1 conversation.conversation_visibilities[0].unread = 1
@ -62,13 +62,16 @@ describe Api::V0::ConversationsController do
end end
it "returns all the user conversations" do it "returns all the user conversations" do
get api_v0_conversations_path, access_token: access_token get api_v0_conversations_path, params: { access_token: access_token }
expect(response.status).to eq 200 expect(response.status).to eq 200
expect(JSON.parse(response.body).length).to eq 3 expect(JSON.parse(response.body).length).to eq 3
end end
it "returns all the user unread conversations" do it "returns all the user unread conversations" do
get api_v0_conversations_path, unread: true, access_token: access_token get(
api_v0_conversations_path,
params: { unread: true, access_token: access_token }
)
expect(response.status).to eq 200 expect(response.status).to eq 200
expect(JSON.parse(response.body).length).to eq 2 expect(JSON.parse(response.body).length).to eq 2
end end
@ -76,7 +79,7 @@ describe Api::V0::ConversationsController do
it "returns all the user conversations after a given date" do it "returns all the user conversations after a given date" do
get( get(
api_v0_conversations_path, api_v0_conversations_path,
only_after: @date, access_token: access_token params: { only_after: @date, access_token: access_token }
) )
expect(response.status).to eq 200 expect(response.status).to eq 200
expect(JSON.parse(response.body).length).to eq 1 expect(JSON.parse(response.body).length).to eq 1
@ -86,14 +89,14 @@ describe Api::V0::ConversationsController do
describe "#show" do describe "#show" do
context "valid conversation ID" do context "valid conversation ID" do
before do before do
post api_v0_conversations_path, @conversation post api_v0_conversations_path, params: @conversation
end end
it "returns the corresponding conversation" do it "returns the corresponding conversation" do
conversation_guid = JSON.parse(response.body)["conversation"]["guid"] conversation_guid = JSON.parse(response.body)["conversation"]["guid"]
get( get(
api_v0_conversation_path(conversation_guid), api_v0_conversation_path(conversation_guid),
access_token: access_token params: { access_token: access_token }
) )
expect(response.status).to eq 200 expect(response.status).to eq 200
conversation = JSON.parse(response.body)["conversation"] conversation = JSON.parse(response.body)["conversation"]
@ -108,7 +111,7 @@ describe Api::V0::ConversationsController do
it "returns a not found error (404)" do it "returns a not found error (404)" do
get( get(
api_v0_conversation_path(42), api_v0_conversation_path(42),
access_token: access_token params: { access_token: access_token }
) )
expect(response.status).to eq 404 expect(response.status).to eq 404
end end
@ -130,7 +133,7 @@ describe Api::V0::ConversationsController do
recipients: [auth_participant.user.person.id], recipients: [auth_participant.user.person.id],
access_token: access_token access_token: access_token
} }
post api_v0_conversations_path, @conversation post api_v0_conversations_path, params: @conversation
@conversation_guid = JSON.parse(response.body)["conversation"]["guid"] @conversation_guid = JSON.parse(response.body)["conversation"]["guid"]
end end
@ -138,17 +141,17 @@ describe Api::V0::ConversationsController do
it "destroys the first participant visibility" do it "destroys the first participant visibility" do
delete( delete(
api_v0_conversation_path(@conversation_guid), api_v0_conversation_path(@conversation_guid),
access_token: access_token params: { access_token: access_token }
) )
expect(response.status).to eq 204 expect(response.status).to eq 204
get api_v0_conversation_path( get api_v0_conversation_path(
@conversation_guid, @conversation_guid,
access_token: access_token params: { access_token: access_token }
) )
expect(response.status).to eq 404 expect(response.status).to eq 404
get api_v0_conversation_path( get api_v0_conversation_path(
@conversation_guid, @conversation_guid,
access_token: access_token_participant params: { access_token: access_token_participant }
) )
expect(response.status).to eq 200 expect(response.status).to eq 200
end end
@ -158,17 +161,17 @@ describe Api::V0::ConversationsController do
it "destroys the second participant visibilty and the conversation" do it "destroys the second participant visibilty and the conversation" do
delete( delete(
api_v0_conversation_path(@conversation_guid), api_v0_conversation_path(@conversation_guid),
access_token: access_token params: { access_token: access_token }
) )
delete( delete(
api_v0_conversation_path(@conversation_guid), api_v0_conversation_path(@conversation_guid),
access_token: access_token_participant params: { access_token: access_token_participant }
) )
expect(response.status).to eq 204 expect(response.status).to eq 204
get api_v0_conversation_path( get api_v0_conversation_path(
@conversation_guid, @conversation_guid,
access_token: access_token_participant params: { access_token: access_token_participant }
) )
expect(response.status).to eq 404 expect(response.status).to eq 404
@ -182,7 +185,7 @@ describe Api::V0::ConversationsController do
it "returns a not found error (404)" do it "returns a not found error (404)" do
delete( delete(
api_v0_conversation_path(42), api_v0_conversation_path(42),
access_token: access_token params: { access_token: access_token }
) )
expect(response.status).to eq 404 expect(response.status).to eq 404
end end

View file

@ -12,30 +12,42 @@ describe Api::V0::LikesController do
describe "#create" do describe "#create" do
it "returns the expected author" do it "returns the expected author" do
post api_v0_post_likes_path(post_id: @status.id), access_token: access_token post(
api_v0_post_likes_path(post_id: @status.id),
params: { access_token: access_token }
)
json = JSON.parse(response.body) json = JSON.parse(response.body)
expect(json["author"]["id"]).to eq(auth.user.person.id) expect(json["author"]["id"]).to eq(auth.user.person.id)
end end
it "fails on random post id" do it "fails on random post id" do
post api_v0_post_likes_path(post_id: 99_999_999), access_token: access_token post api_v0_post_likes_path(post_id: 99_999_999), params: {access_token: access_token}
expect(response.body).to eq("Post or like not found") expect(response.body).to eq("Post or like not found")
end end
end end
describe "#delete" do describe "#delete" do
before do before do
post api_v0_post_likes_path(post_id: @status.id), access_token: access_token post(
api_v0_post_likes_path(post_id: @status.id),
params: { access_token: access_token }
)
@like_id = JSON.parse(response.body)["id"] @like_id = JSON.parse(response.body)["id"]
end end
it "succeeds" do it "succeeds" do
delete api_v0_post_like_path(post_id: @status.id, id: @like_id), access_token: access_token delete(
api_v0_post_like_path(post_id: @status.id, id: @like_id),
params: { access_token: access_token }
)
expect(response).to be_success expect(response).to be_success
end end
it "fails on random like id" do it "fails on random like id" do
delete api_v0_post_like_path(post_id: @status.id, id: 99_999_999), access_token: access_token delete(
api_v0_post_like_path(post_id: @status.id, id: 99_999_999),
params: { access_token: access_token }
)
expect(response.body).to eq("Post or like not found") expect(response.body).to eq("Post or like not found")
end end
end end

View file

@ -27,7 +27,7 @@ describe Api::V0::MessagesController do
describe "#create " do describe "#create " do
before do before do
post api_v0_conversations_path, @conversation post api_v0_conversations_path, params: @conversation
@conversation_guid = JSON.parse(response.body)["conversation"]["guid"] @conversation_guid = JSON.parse(response.body)["conversation"]["guid"]
end end
@ -35,7 +35,7 @@ describe Api::V0::MessagesController do
it "creates the message in the conversation scope" do it "creates the message in the conversation scope" do
post( post(
api_v0_conversation_messages_path(@conversation_guid), api_v0_conversation_messages_path(@conversation_guid),
body: @message, access_token: access_token params: {body: @message, access_token: access_token}
) )
expect(response.status).to eq 201 expect(response.status).to eq 201
@ -47,7 +47,7 @@ describe Api::V0::MessagesController do
get( get(
api_v0_conversation_messages_path(@conversation_guid), api_v0_conversation_messages_path(@conversation_guid),
access_token: access_token params: {access_token: access_token}
) )
messages = JSON.parse(response.body) messages = JSON.parse(response.body)
expect(messages.length).to eq 2 expect(messages.length).to eq 2
@ -60,7 +60,7 @@ describe Api::V0::MessagesController do
it "returns a wrong parameter error (400)" do it "returns a wrong parameter error (400)" do
post( post(
api_v0_conversation_messages_path(@conversation_guid), api_v0_conversation_messages_path(@conversation_guid),
access_token: access_token params: {access_token: access_token}
) )
expect(response.status).to eq 400 expect(response.status).to eq 400
end end
@ -70,7 +70,7 @@ describe Api::V0::MessagesController do
it "returns a a not found error (404)" do it "returns a a not found error (404)" do
post( post(
api_v0_conversation_messages_path(42), api_v0_conversation_messages_path(42),
access_token: access_token params: {access_token: access_token}
) )
expect(response.status).to eq 404 expect(response.status).to eq 404
end end
@ -79,7 +79,7 @@ describe Api::V0::MessagesController do
describe "#index " do describe "#index " do
before do before do
post api_v0_conversations_path, @conversation post api_v0_conversations_path, params: @conversation
@conversation_guid = JSON.parse(response.body)["conversation"]["guid"] @conversation_guid = JSON.parse(response.body)["conversation"]["guid"]
end end
@ -87,7 +87,7 @@ describe Api::V0::MessagesController do
it "returns all messages related to conversation" do it "returns all messages related to conversation" do
get( get(
api_v0_conversation_messages_path(@conversation_guid), api_v0_conversation_messages_path(@conversation_guid),
access_token: access_token params: {access_token: access_token}
) )
messages = JSON.parse(response.body) messages = JSON.parse(response.body)
expect(messages.length).to eq 1 expect(messages.length).to eq 1

View file

@ -22,14 +22,23 @@ describe Api::V0::PostsController do
it "shows attempts to show the info and mark the user notifications" do it "shows attempts to show the info and mark the user notifications" do
expect(post_service_double).to receive(:mark_user_notifications) expect(post_service_double).to receive(:mark_user_notifications)
@status = auth_with_read.user.post(:status_message, text: "hello", public: true, to: "all") @status = auth_with_read.user.post(:status_message, text: "hello", public: true, to: "all")
get api_v0_post_path(@status.id), access_token: access_token_with_read get(
api_v0_post_path(@status.id),
params: {access_token: access_token_with_read}
)
end end
end end
context "when mark notifications is false" do context "when mark notifications is false" do
it "shows attempts to show the info" do it "shows attempts to show the info" do
@status = auth_with_read.user.post(:status_message, text: "hello", public: true, to: "all") @status = auth_with_read.user.post(:status_message, text: "hello", public: true, to: "all")
get api_v0_post_path(@status.id), access_token: access_token_with_read, mark_notifications: "false" get(
api_v0_post_path(@status.id),
params: {
access_token: access_token_with_read,
mark_notifications: "false"
}
)
end end
end end
end end
@ -37,22 +46,40 @@ describe Api::V0::PostsController do
describe "#create" do describe "#create" do
context "when given read-write access token" do context "when given read-write access token" do
it "creates a public post" do it "creates a public post" do
post api_v0_posts_path, access_token: access_token_with_read_and_write, post(
status_message: {text: "Hello this is a public post!"}, aspect_ids: "public" api_v0_posts_path,
expect(Post.find_by(text: "Hello this is a public post!").public).to eq(true) params: {
access_token: access_token_with_read_and_write,
status_message: {text: "Hello this is a public post!"},
aspect_ids: "public"
}
)
expect(Post.find_by_text("Hello this is a public post!").public).to eq(true)
end end
it "creates a private post" do it "creates a private post" do
post api_v0_posts_path, access_token: access_token_with_read_and_write, response = post(
status_message: {text: "Hello this is a post!"}, aspect_ids: "1" api_v0_posts_path,
params: {
access_token: access_token_with_read_and_write,
status_message: {text: "Hello this is a post!"},
aspect_ids: "1"
}
)
expect(Post.find_by(text: "Hello this is a post!").public).to eq(false) expect(Post.find_by(text: "Hello this is a post!").public).to eq(false)
end end
end end
context "when given read only access token" do context "when given read only access token" do
before do before do
post api_v0_posts_path, access_token: access_token_with_read, post(
status_message: {text: "Hello this is a post!"}, aspect_ids: "public" api_v0_posts_path,
params: {
access_token: access_token_with_read,
status_message: {text: "Hello this is a post!"},
aspect_ids: "public"
}
)
end end
it "doesn't create the post" do it "doesn't create the post" do
@ -67,14 +94,20 @@ describe Api::V0::PostsController do
it "attempts to destroy the post" do it "attempts to destroy the post" do
expect(post_service_double).to receive(:retract_post) expect(post_service_double).to receive(:retract_post)
@status = auth_with_read_and_write.user.post(:status_message, text: "hello", public: true, to: "all") @status = auth_with_read_and_write.user.post(:status_message, text: "hello", public: true, to: "all")
delete api_v0_post_path(@status.id), access_token: access_token_with_read_and_write delete(
api_v0_post_path(@status.id),
params: {access_token: access_token_with_read_and_write}
)
end end
end end
context "when given read only access token" do context "when given read only access token" do
before do before do
@status = auth_with_read.user.post(:status_message, text: "hello", public: true, to: "all") @status = auth_with_read.user.post(:status_message, text: "hello", public: true, to: "all")
delete api_v0_post_path(@status.id), access_token: access_token_with_read delete(
api_v0_post_path(@status.id),
params: {access_token: access_token_with_read}
)
end end
it "doesn't delete the post" do it "doesn't delete the post" do

View file

@ -13,12 +13,18 @@ describe Api::V0::PostsController do
describe "#aspect" do describe "#aspect" do
it "contains expected aspect message" do it "contains expected aspect message" do
get api_v0_aspects_stream_path(a_ids: [@aspect.id]), access_token: access_token get(
api_v0_aspects_stream_path(a_ids: [@aspect.id]),
params: {access_token: access_token}
)
expect(response.body).to include("This is a status message") expect(response.body).to include("This is a status message")
end end
it "does not save to requested aspects to session" do it "does not save to requested aspects to session" do
get api_v0_aspects_stream_path(a_ids: [@aspect.id]), access_token: access_token get(
api_v0_aspects_stream_path(a_ids: [@aspect.id]),
params: {access_token: access_token}
)
expect(session[:a_ids]).to be_nil expect(session[:a_ids]).to be_nil
end end
end end

View file

@ -1,13 +1,15 @@
# frozen_string_literal: true # frozen_string_literal: true
describe ConversationService do describe ConversationService do
before do
opts = { opts = {
subject: "conversation subject", subject: "conversation subject",
message: {text: "conversation text"}, message: {text: "conversation text"},
participant_ids: [bob.person.id] participant_ids: [bob.person.id]
} }
conversation = alice.build_conversation(opts) @conversation = alice.build_conversation(opts)
conversation.save @conversation.save
end
describe "#all_for_user" do describe "#all_for_user" do
before do before do
@ -23,9 +25,9 @@ describe ConversationService do
opts = { opts = {
subject: "conversation subject 3", subject: "conversation subject 3",
message: {text: "conversation text 3"}, message: {text: "conversation text 3"},
participant_ids: [bob.person.id] participant_ids: []
} }
@conversation = alice.build_conversation(opts) @conversation = bob.build_conversation(opts)
@conversation.save! @conversation.save!
end end
@ -49,14 +51,14 @@ describe ConversationService do
describe "#find!" do describe "#find!" do
it "returns the conversation, if it is the user's conversation" do it "returns the conversation, if it is the user's conversation" do
expect(bob_conversation_service.find!(conversation.guid)).to eq( expect(bob_conversation_service.find!(@conversation.guid)).to eq(
conversation @conversation
) )
end end
it "returns the conversation, if the user is recipient" do it "returns the conversation, if the user is recipient" do
expect(bob_conversation_service.find!(conversation.guid)).to eq( expect(bob_conversation_service.find!(@conversation.guid)).to eq(
conversation @conversation
) )
end end
@ -68,7 +70,7 @@ describe ConversationService do
it "raises RecordNotFound if the user is not recipient" do it "raises RecordNotFound if the user is not recipient" do
expect { expect {
eve_conversation_service.find!(conversation.guid) eve_conversation_service.find!(@conversation.guid)
}.to raise_error ActiveRecord::RecordNotFound }.to raise_error ActiveRecord::RecordNotFound
end end
end end
@ -101,22 +103,24 @@ describe ConversationService do
describe "#get_visibility" do describe "#get_visibility" do
it "returns visibility for current user" do it "returns visibility for current user" do
visibility = alice_conversation_service.get_visibility(conversation.guid) visibility = alice_conversation_service.get_visibility(
@conversation.guid
)
expect(visibility).to_not be_nil expect(visibility).to_not be_nil
end end
it "raises RecordNotFound if the user has no visibility" do it "raises RecordNotFound if the user has no visibility" do
expect { expect {
eve_conversation_service.get_visibility(conversation.id) eve_conversation_service.get_visibility(@conversation.id)
}.to raise_error ActiveRecord::RecordNotFound }.to raise_error ActiveRecord::RecordNotFound
end end
end end
describe "#destroy!" do describe "#destroy!" do
it "deletes the conversation, when it is the user conversation" do it "deletes the conversation, when it is the user conversation" do
alice_conversation_service.destroy!(conversation.guid) alice_conversation_service.destroy!(@conversation.guid)
expect { expect {
alice_conversation_service.find!(conversation.guid) alice_conversation_service.find!(@conversation.guid)
}.to raise_error ActiveRecord::RecordNotFound }.to raise_error ActiveRecord::RecordNotFound
end end
@ -128,7 +132,7 @@ describe ConversationService do
it "raises RecordNotFound if the user is not part of the conversation" do it "raises RecordNotFound if the user is not part of the conversation" do
expect { expect {
eve_conversation_service.destroy!(conversation.guid) eve_conversation_service.destroy!(@conversation.guid)
}.to raise_error ActiveRecord::RecordNotFound }.to raise_error ActiveRecord::RecordNotFound
end end
end end