require 'spec_helper' describe Lifen::User 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) } describe ':create' do context 'missing field' do let(:user) { Lifen::User.new(first_name: "Marc") } it 'raises an error' do expect{ VCR.use_cassette "users/create/missing_fields" do user.create end }.to raise_error(Lifen::Error) end end context 'invalid secret key' do let(:user) { Lifen::User.new(email: "invalid-key-email@test.tld") } before do Lifen.configure do |config| config.application_access_token = "invalid-secret-key" end end it 'raises an error' do expect{ VCR.use_cassette "users/create/invalid_token" do user.create end }.to raise_error(Lifen::InvalidSecretTokenError) end end context 'exisiting user' do let(:user) { Lifen::User.new(email: "existing-user@domain.tld", first_name: "Existing", last_name: "User") } it 'creates the user' do expect{ VCR.use_cassette "users/create/existing_user" do user.create end }.to raise_error(Lifen::UserAlreadyExistingError) end end context 'valid attributes' do let(:user) { Lifen::User.new(email: "valid-email-spec@domain.tld", first_name: "Valid", last_name: "User") } it 'creates the user' do VCR.use_cassette "users/create/valid_attributes" do user.create end expect(user.uuid).to_not be_nil end end end describe ':token.refresh' do context 'invalid user uuid' do let(:user) { Lifen::User.new(uuid: "invalid-user-uuid") } it 'raises an error' do expect{ VCR.use_cassette "users/token/refresh/invalid_user_uuid" do user.token.refresh end }.to raise_error(Lifen::Error) end end context 'valid user uuid' do let(:user) { Lifen::User.new(uuid: "11e6be14-8267-25ee-a3bd-0242ac110002") } it 'raises an error' do previous_token_value = user.token.value VCR.use_cassette "users/token/refresh/valid_user_uuid" do user.token.refresh end expect(user.token.value).to_not be_nil expect(user.token.value).to_not eq previous_token_value end end end describe ':status.refresh' do context 'invalid token' do let(:user) { Lifen::User.new(token: invalid_token) } it 'raises an error' do expect{ VCR.use_cassette "users/status/refresh/invalid_token" do user.status.refresh end }.to raise_error(Lifen::Error) end end context 'valid token' do # let(:valid_token_value) { "REPLACE_ME" } let(:user) { Lifen::User.new(token: valid_token) } it 'raises an error' do VCR.use_cassette "users/status/refresh/valid_token" do user.status.refresh end expect(user.unread_messages).to eq 2 end end end end