require 'spec_helper' describe Lifen::Flow do let(:valid_token_value) { "valid_token" } let(:invalid_token) { Lifen::Token.new(value: "invalid_token", expires_at: Time.now.to_i + 60) } let(:valid_token) { Lifen::Token.new(value: valid_token_value, expires_at: Time.now.to_i + 60) } let(:user) { Lifen::User.new(token: valid_token, uuid: "25588996-4ff1-2dbb-9643-eabb809fa654") } describe "index" do context "invalid token" do it 'raises an error' do user = Lifen::User.new(token: invalid_token) expect{ VCR.use_cassette "flows/invalid_token" do user.flows end }.to raise_error(Lifen::UnauthorizedError, "Error 401, Token is not valid, User Client, GET 'https://develop.lifen.fr/central/api/chats' with params '{}' and bearer 'invalid_token'") end end context "valid token but internal error" do it 'raises an error' do expect{ VCR.use_cassette "flows/internal_error" do user.flows end }.to raise_error(Lifen::Error, "Error 500, Internal server error (trace ID: 2e2eac3a7e3fa2), User Client, GET 'https://develop.lifen.fr/central/api/chats' with params '{}' and bearer 'valid_token'") end it 'raises an error (without trace ID)' do expect{ VCR.use_cassette "flows/internal_error_without_trace_id" do user.flows end }.to raise_error(Lifen::Error, "Error 500, Internal server error (trace ID: unknown), User Client, GET 'https://develop.lifen.fr/central/api/chats' with params '{}' and bearer 'valid_token'") end end context "valid token" do # let(:valid_token_value) { "REPLACE_ME" } before(:each) do VCR.use_cassette "flows/valid_token" do @flows = user.flows end @flow = @flows.first end it { expect(@flows.size).to eq(1) } it 'has valid attributes' do expect(@flow.user).to eq(user) expect(@flow.uuid).to eq("11e6be18-7a35-d759-9177-0242ac110002") expect(@flow.title).to eq("Rspec Flow") end end end describe 'create' do # let(:valid_token_value) { "REPLACE_ME" } let(:flow) { Lifen::Flow.new(user: user, title: "Rspec Flow") } it 'creates a flow' do VCR.use_cassette "flows/create" do @new_flow = flow.create end expect(@new_flow.uuid).to_not be_nil expect(@new_flow.user).to eq(user) expect(@new_flow.title).to eq("Rspec Flow") end context 'attaching users at the same time' do let(:user) { Lifen::User.new(token: valid_token, uuid: "11e6be14-8267-25ee-a3bd-0242ac110002") } let(:other_user) { Lifen::User.new(uuid: "11e6be15-0fb1-0a21-a3bd-0242ac110002") } let(:flow) { Lifen::Flow.new(user: user, title: "Rspec Flow", users: [other_user]) } it 'creates a flow with attached users' do VCR.use_cassette "flows/create_with_users" do flow.create end expect(flow.users.size).to eq(2) first_user = flow.users.first expect(first_user.uuid).to eq("11e6be14-8267-25ee-a3bd-0242ac110002") expect(first_user.first_name).to eq("Existing") expect(flow.users.last.uuid).to eq("11e6be15-0fb1-0a21-a3bd-0242ac110002") end end end describe 'save' do let(:flow) { Lifen::Flow.new(user: user, uuid: "11e6c916-efae-4e35-8c16-0242ac110002") } it 'updates the title' do flow.title = "New title" VCR.use_cassette "flows/update_valid_title" do flow.save end expect(flow.title).to eq "New title" end end describe 'attach_users' do # let(:valid_token_value) { "REPLACE_ME" } context 'invalid flow uuid' do let(:flow) { Lifen::Flow.new(uuid: "invalid-uuid", user: user) } it 'raises an error' do expect{ VCR.use_cassette "flows/attach_users/invalid_flow_uuid" do flow.attach_users(user) end }.to raise_error(Lifen::InvalidParamsError, "Error 400, Invalid params, User Client, POST 'https://develop.lifen.fr/central/api/chats/invalid-uuid/attach_users?rel=activeUsers' with params '[\"25588996-4ff1-2dbb-9643-eabb809fa654\"]' and bearer 'valid_token'") end end context 'valid flow uuid' do let(:flow) { Lifen::Flow.new(uuid: "11e6be18-7a35-d759-9177-0242ac110002", user: user) } let(:other_user) { Lifen::User.new(uuid: "11e6be15-0fb1-0a21-a3bd-0242ac110002") } context 'invalid user params' do it 'raises an error' do invalid_user = Lifen::User.new(uuid: "invalid-uuid") expect{ VCR.use_cassette "flows/attach_users/invalid_user_uuid" do flow.attach_users(invalid_user) end }.to raise_error(Lifen::InvalidParamsError, "Error 400, Invalid params, User Client, POST 'https://develop.lifen.fr/central/api/chats/11e6be18-7a35-d759-9177-0242ac110002/attach_users?rel=activeUsers' with params '[\"invalid-uuid\"]' and bearer 'valid_token'") end end it 'attaches a user' do VCR.use_cassette "flows/attach_users/valid" do flow.attach_users(other_user) end expect(flow.users.size).to eq(2) expect(flow.users.first.uuid).to eq("11e6be14-8267-25ee-a3bd-0242ac110002") expect(flow.users.first.first_name).to eq("Existing") end end end describe 'detach_users' do # let(:valid_token_value) { "REPLACE_ME" } context 'valid flow uuid' do let(:flow) { Lifen::Flow.new(uuid: "11e6be18-7a35-d759-9177-0242ac110002", user: user) } let(:other_user) { Lifen::User.new(uuid: "11e6be15-0fb1-0a21-a3bd-0242ac110002") } it 'detaches a users' do VCR.use_cassette "flows/detach_users/valid" do flow.detach_users(other_user) end expect(flow.users.size).to eq(1) end # it 'raises an error if the error is not detached' do # VCR.use_cassette "flows/detach_users/valid_again" do # flow.detach_users!(user) # end # end end end end