From 91e551e35ed9dddedcd2c5b81b17b1f3da0a42ea Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Fri, 5 Feb 2016 22:47:51 +0100 Subject: [PATCH] cover the user model --- Gemfile | 1 + Gemfile.lock | 12 +++++++- config/environments/test.rb | 2 ++ spec/factories/users.rb | 2 +- spec/models/user_spec.rb | 57 +++++++++++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 27a4234..71dea2c 100644 --- a/Gemfile +++ b/Gemfile @@ -29,6 +29,7 @@ group :test do gem 'codeclimate-test-reporter', require: nil gem 'rspec-rails' gem 'coveralls', require: false + gem 'webmock' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index 3b187db..9a7683f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -36,6 +36,7 @@ GEM minitest (~> 5.1) thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) + addressable (2.4.0) airbrake (4.3.5) builder multi_json @@ -89,6 +90,8 @@ GEM term-ansicolor (~> 1.3) thor (~> 0.19.1) tins (~> 1.6.0) + crack (0.4.3) + safe_yaml (~> 1.0.0) daemons (1.2.3) delayed_job (4.1.1) activesupport (>= 3.0, < 5.0) @@ -129,6 +132,7 @@ GEM haml (>= 4.0.6, < 5.0) html2haml (>= 1.0.1) railties (>= 4.0.1) + hashdiff (0.2.3) hashie (3.4.3) hike (1.2.3) html2haml (2.0.0) @@ -242,6 +246,7 @@ GEM rspec-support (3.4.1) ruby_parser (3.7.3) sexp_processor (~> 4.1) + safe_yaml (1.0.4) sass (3.2.19) sass-rails (4.0.5) railties (>= 4.0.0, < 5.0) @@ -289,6 +294,10 @@ GEM unf_ext (0.0.7.2) warden (1.2.6) rack (>= 1.0) + webmock (1.22.6) + addressable (>= 2.3.6) + crack (>= 0.3.2) + hashdiff will_paginate (3.0.7) PLATFORMS @@ -329,7 +338,8 @@ DEPENDENCIES sqlite3 turbolinks uglifier (>= 1.3.0) + webmock will_paginate (= 3.0.7) BUNDLED WITH - 1.10.6 + 1.11.2 diff --git a/config/environments/test.rb b/config/environments/test.rb index 4455de9..dcbd3ad 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -42,4 +42,6 @@ Rails.application.configure do Paperclip.options[:command_path] = "/usr/local/bin/" config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } + + config.api_url = "http://www.example.com" end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 11397b8..1df99aa 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -24,7 +24,7 @@ require 'identicon' FactoryGirl.define do factory :user do - name { Faker::Name.name } + name { Faker::Internet.user_name } avatar { Identicon.data_url_for name } private { false } diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index b18e6d1..7aab751 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -19,6 +19,8 @@ # private :boolean default("f") # +require 'webmock/rspec' + describe User do before :each do @user = create :user @@ -52,10 +54,65 @@ describe User do end end + describe 'balance' do + before :all do + @api_url = "www.example.com/api.json" + end + + it 'should be nil if offline' do + stub_request(:get, /.*/).to_return(status: 404) + expect(@user.balance).to be nil + end + + it 'should be updated when online' do + balance = 5 + stub_request(:get, /.*/).to_return(status: 200, body: JSON.dump({ balance: balance })) + expect(@user.balance).to eq balance + end + end + end + + describe 'omniauth' do + it 'should be a new user' do + name = "yet-another-test-user" + omniauth = double(uid: name) + expect(User.from_omniauth(omniauth).name).to eq name + end + + it 'should be the logged in user' do + second_user = create :user + omniauth = double(uid: second_user.name) + expect(User.from_omniauth(omniauth)).to eq second_user + end + end + + describe 'static users' do describe 'koelkast' do it 'should be false by default' do expect(@user.reload.koelkast).to be false end + + it 'should be true for koelkast' do + expect(User.koelkast.koelkast).to be true + end + + it 'should not be an admin' do + expect(User.koelkast.admin).to be false + end + end + + describe 'guest' do + it 'should not be an admin' do + expect(User.guest.admin).to be false + end + + it 'should be public' do + expect(User.guest.private).to be false + end + + it 'should be a guest' do + expect(User.guest.guest?).to be true + end end end