spec/models/account_spec.rb in bookingsync-engine-0.1.2 vs spec/models/account_spec.rb in bookingsync-engine-0.1.3

- old
+ new

@@ -1,28 +1,57 @@ require 'spec_helper' RSpec.describe Account, type: :model do + shared_examples "it takes attributes from auth" do + it "sets name" do + expect(account.name).to eq "business name" + end + + it "sets token" do + expect(account.oauth_access_token).to eql("token") + expect(account.oauth_refresh_token).to eql("refresh token") + expect(account.oauth_expires_at).to eql("expires at") + end + end + describe ".from_omniauth" do + before { Account.create!(provider: "bookingsync", uid: 456) } + let(:auth) { OmniAuth.config.mock_auth[:bookingsync] } context "when account exists" do let!(:account) { Account.create!(provider: "bookingsync", uid: 123) } it "loads the existing account" do expect(Account.from_omniauth(auth)).to eql(account) end - it "updates account's name" do - Account.from_omniauth(auth) - expect(account.reload.name).to eql("business name") + describe "the updated account" do + before do + Account.from_omniauth(auth) + account.reload + end + + it_behaves_like "it takes attributes from auth" end + end - it "updates account's token" do - Account.from_omniauth(auth) - account.reload - expect(account.oauth_access_token).to eql("token") - expect(account.oauth_refresh_token).to eql("refresh token") - expect(account.oauth_expires_at).to eql("expires at") + context "when account doesn't exist" do + it "creates new account" do + expect { + Account.from_omniauth(auth) + }.to change { Account.count }.by(1) + end + + describe "the newly created account" do + let!(:account) { Account.from_omniauth(auth) } + + it "sets uid and provider from auth" do + expect(account.uid).to eq 123 + expect(account.provider).to eq "bookingsync" + end + + it_behaves_like "it takes attributes from auth" end end end describe "#clear_token!" do