Follow official specs
This commit is contained in:
parent
3f00195eed
commit
71d324a8e4
6 changed files with 107 additions and 9 deletions
|
|
@ -8,19 +8,26 @@ module Api
|
||||||
protected
|
protected
|
||||||
|
|
||||||
rescue_from Exception do |e|
|
rescue_from Exception do |e|
|
||||||
|
pp e
|
||||||
logger.error e.message
|
logger.error e.message
|
||||||
render json: {error: e.message}, status: 500
|
render json: {error: e.message}, status: 500
|
||||||
end
|
end
|
||||||
|
|
||||||
rescue_from ActiveRecord::RecordNotFound do
|
rescue_from ActiveRecord::RecordNotFound do
|
||||||
|
pp e
|
||||||
|
logger.error e.message
|
||||||
render json: {error: I18n.t("api.error.not_found")}, status: 404
|
render json: {error: I18n.t("api.error.not_found")}, status: 404
|
||||||
end
|
end
|
||||||
|
|
||||||
rescue_from ActiveRecord::RecordInvalid do |e|
|
rescue_from ActiveRecord::RecordInvalid do |e|
|
||||||
|
pp e
|
||||||
|
logger.error e.message
|
||||||
render json: {error: e.to_s}, status: 400
|
render json: {error: e.to_s}, status: 400
|
||||||
end
|
end
|
||||||
|
|
||||||
rescue_from ActionController::ParameterMissing do |e|
|
rescue_from ActionController::ParameterMissing do |e|
|
||||||
|
pp e
|
||||||
|
logger.error e.message
|
||||||
render json: {
|
render json: {
|
||||||
error: I18n.t("api.error.wrong_parameters"),
|
error: I18n.t("api.error.wrong_parameters"),
|
||||||
message: e.message
|
message: e.message
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,14 @@ module Api
|
||||||
end
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
conversations = conversation_service.all_for_user
|
filter = {}
|
||||||
|
if params[:only_after] then
|
||||||
|
filter["only_after"] = params[:only_after]
|
||||||
|
end
|
||||||
|
if params[:unread] then
|
||||||
|
filter["unread"] = params[:unread]
|
||||||
|
end
|
||||||
|
conversations = conversation_service.all_for_user(filter)
|
||||||
render json: conversations.map {|x| conversation_as_json(x) }
|
render json: conversations.map {|x| conversation_as_json(x) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,10 @@ module Api
|
||||||
|
|
||||||
def index
|
def index
|
||||||
conversation = conversation_service.find!(params[:conversation_id])
|
conversation = conversation_service.find!(params[:conversation_id])
|
||||||
render json: conversation.messages.map {|x| message_json(x)}, status: 201
|
conversation.set_read(user)
|
||||||
|
render json: conversation.messages.map {
|
||||||
|
|x| message_json(x)
|
||||||
|
}, status: 201
|
||||||
end
|
end
|
||||||
|
|
||||||
def conversation_service
|
def conversation_service
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,21 @@ class ConversationService
|
||||||
@user = user
|
@user = user
|
||||||
end
|
end
|
||||||
|
|
||||||
def all_for_user
|
def all_for_user(filter={})
|
||||||
Conversation.where(author_id: @user.person.id)
|
conversation_filter = {}
|
||||||
|
if !filter[:only_after].nil? then
|
||||||
|
conversation_filter = \
|
||||||
|
'conversations.created_at >= ?', filter[:only_after]
|
||||||
|
end
|
||||||
|
|
||||||
|
visibility_filter = {person_id: @user.person_id}
|
||||||
|
if filter[:unread] == true then
|
||||||
|
visibility_filter["unread"] = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
Conversation.where(conversation_filter)
|
||||||
.joins(:conversation_visibilities)
|
.joins(:conversation_visibilities)
|
||||||
.where(conversation_visibilities: {
|
.where(conversation_visibilities: visibility_filter)
|
||||||
person_id: @user.person_id
|
|
||||||
})
|
|
||||||
.all
|
.all
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@ describe Api::V0::ConversationsController 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, @conversation
|
||||||
|
@conversation_guid = JSON.parse(response.body)["conversation"]["guid"]
|
||||||
conversation = JSON.parse(response.body)["conversation"]
|
conversation = JSON.parse(response.body)["conversation"]
|
||||||
|
|
||||||
expect(response.status).to eq 201
|
expect(response.status).to eq 201
|
||||||
|
|
@ -31,6 +32,7 @@ describe Api::V0::ConversationsController do
|
||||||
expect(conversation["subject"]).to eq @conversation[:subject]
|
expect(conversation["subject"]).to eq @conversation[:subject]
|
||||||
expect(conversation["created_at"]).to_not be_nil
|
expect(conversation["created_at"]).to_not be_nil
|
||||||
expect(conversation["participants"].length).to eq 2
|
expect(conversation["participants"].length).to eq 2
|
||||||
|
conversation_service.find!(@conversation_guid)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -46,13 +48,37 @@ describe Api::V0::ConversationsController do
|
||||||
before do
|
before do
|
||||||
post api_v0_conversations_path, @conversation
|
post api_v0_conversations_path, @conversation
|
||||||
post api_v0_conversations_path, @conversation
|
post api_v0_conversations_path, @conversation
|
||||||
|
sleep(1)
|
||||||
|
post api_v0_conversations_path, @conversation
|
||||||
|
conversation_guid = JSON.parse(response.body)["conversation"]["guid"]
|
||||||
|
conversation = conversation_service.find!(conversation_guid)
|
||||||
|
conversation.conversation_visibilities[0].unread = 1
|
||||||
|
conversation.conversation_visibilities[0].save!
|
||||||
|
conversation.conversation_visibilities[1].unread = 1
|
||||||
|
conversation.conversation_visibilities[1].save!
|
||||||
|
@date = conversation.created_at
|
||||||
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, 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
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns all the user unread conversations" do
|
||||||
|
get api_v0_conversations_path, unread: true, access_token: access_token
|
||||||
|
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
|
||||||
|
|
||||||
|
it "returns all the user conversations after a given date" do
|
||||||
|
get(
|
||||||
|
api_v0_conversations_path,
|
||||||
|
only_after: @date, access_token: access_token
|
||||||
|
)
|
||||||
|
expect(response.status).to eq 200
|
||||||
|
expect(JSON.parse(response.body).length).to eq 1
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#show" do
|
describe "#show" do
|
||||||
|
|
@ -71,7 +97,6 @@ describe Api::V0::ConversationsController do
|
||||||
conversation = JSON.parse(response.body)["conversation"]
|
conversation = JSON.parse(response.body)["conversation"]
|
||||||
expect(conversation["guid"]).to eq conversation_guid
|
expect(conversation["guid"]).to eq conversation_guid
|
||||||
expect(conversation["subject"]).to eq @conversation[:subject]
|
expect(conversation["subject"]).to eq @conversation[:subject]
|
||||||
expect(conversation["created_at"]).to_not be_nil
|
|
||||||
expect(conversation["participants"].length).to eq 2
|
expect(conversation["participants"].length).to eq 2
|
||||||
expect(conversation["read"]).to eq true
|
expect(conversation["read"]).to eq true
|
||||||
end
|
end
|
||||||
|
|
@ -161,4 +186,9 @@ describe Api::V0::ConversationsController do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def conversation_service
|
||||||
|
ConversationService.new(alice)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,51 @@ describe ConversationService do
|
||||||
conversation = alice.build_conversation(opts)
|
conversation = alice.build_conversation(opts)
|
||||||
conversation.save
|
conversation.save
|
||||||
|
|
||||||
|
describe "#all_for_user" do
|
||||||
|
before do
|
||||||
|
opts = {
|
||||||
|
subject: "conversation subject 2",
|
||||||
|
message: {text: "conversation text 2"},
|
||||||
|
participant_ids: [bob.person.id]
|
||||||
|
}
|
||||||
|
@conversation = alice.build_conversation(opts)
|
||||||
|
@conversation.save!
|
||||||
|
sleep(1)
|
||||||
|
@date = @conversation.created_at
|
||||||
|
opts = {
|
||||||
|
subject: "conversation subject 3",
|
||||||
|
message: {text: "conversation text 3"},
|
||||||
|
participant_ids: [bob.person.id]
|
||||||
|
}
|
||||||
|
@conversation = alice.build_conversation(opts)
|
||||||
|
@conversation.save!
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns all conversations" do
|
||||||
|
expect(alice_conversation_service.all_for_user().length).to eq(3)
|
||||||
|
expect(bob_conversation_service.all_for_user().length).to eq(3)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns all unread conversations" do
|
||||||
|
@conversation.conversation_visibilities[0].unread = true
|
||||||
|
@conversation.conversation_visibilities[0].save!
|
||||||
|
conversations = bob_conversation_service.all_for_user(
|
||||||
|
filter={unread: true}
|
||||||
|
)
|
||||||
|
expect(conversations.length).to eq(2)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns conversation after a given date" do
|
||||||
|
conversations = bob_conversation_service.all_for_user(
|
||||||
|
filter={only_after: @date}
|
||||||
|
)
|
||||||
|
expect(conversations.length).to eq(2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
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(alice_conversation_service.find!(conversation.guid)).to eq(
|
expect(bob_conversation_service.find!(conversation.guid)).to eq(
|
||||||
conversation
|
conversation
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue