diff --git a/app/models/user.rb b/app/models/user.rb index af59046..aeadc2b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -12,7 +12,6 @@ def tag_labels def update_with_omniauth(auth) update_attributes!( - uid: auth[:uid], nickname: auth[:info][:nickname], email: auth[:info][:email], image_url: auth[:info][:image], diff --git a/config/initializers/secret_token.rb b/config/initializers/secret_token.rb index f7563c0..3803a53 100644 --- a/config/initializers/secret_token.rb +++ b/config/initializers/secret_token.rb @@ -9,4 +9,4 @@ # Make sure your secret_key_base is kept private # if you're sharing your code publicly. -TaggedGist::Application.config.secret_key_base = '1403ff6447012fb7ef1cc53d21a36935171f132130489d4d457cea155197005dbdf1f0c42507f8d73c5c3bcd2d179426f87e0c1d7ef4a8b52f79e23368e6305a' +TaggedGist::Application.config.secret_key_base = ENV['RAILS_SECRET_KEY'] diff --git a/spec/controllers/root_controller_spec.rb b/spec/controllers/root_controller_spec.rb index accd064..d88d57c 100644 --- a/spec/controllers/root_controller_spec.rb +++ b/spec/controllers/root_controller_spec.rb @@ -1,11 +1,66 @@ require 'spec_helper' describe RootController do + let(:user) {FactoryGirl.create(:user)} - describe "GET 'index'" do - it "returns http success" do - get 'index' - response.should be_success + def valid_session + {user: user.id} + end + + describe "GET index" do + context "with not sign in" do + it "returns http success" do + get :index + expect(response).to be_success + end + + it "should not called User#tag_labels" do + User.any_instance.should_not_receive(:tag_labels) + get :index + end + end + + context "with sign in" do + let!(:tags) do + tags = ["ruby", "javascript"] + tags.each_with_index do |tag, i| + g = Gist.new(gid: "#{Time.now.usec}#{i}", user_id: user.id) + Tag.create!(name: tag, user_id: user.id, gist_id: g.id) + end + tags + end + + it "return http success" do + get :index, {}, valid_session + expect(response).to be_success + end + + it "assigns tags as all tag labels" do + get :index, {}, valid_session + expect(assigns(:tags)).to eq(tags) + end + + it "assigns gists as all gists" do + get :index, {}, valid_session + expect(assigns(:gists)).to eq(user.gists) + end + + it "assigns gists as specified tag" do + get :index, {tag: "ruby"}, valid_session + assigns(:gists).each do |g| + expect(g.tags.inject(false){|f,t| f = true if t.name == "ruby"}).to be_true + end + end + + it "assigns current_tag as nil" do + get :index, {}, valid_session + expect(assigns(:current_tag)).to be_nil + end + + it "assigns current_tag as specified tag" do + get :index, {tag: "ruby"}, valid_session + expect(assigns(:current_tag)).not_to be_nil + end end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 7929923..9ba935e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -6,14 +6,14 @@ def valid_auth uid: "12345678", info: { nickname: "example", - email: "MyString", - image: "MyString", + email: "user@example.com", + image: "image_url", urls: { - GitHub: "MyString" + GitHub: "github_url" } }, credentials: { - token: "MyString" + token: "oauth_token" } } end @@ -33,4 +33,75 @@ def valid_auth expect{User.create_with_omniauth(nil)}.to raise_error(NoMethodError) end end + + describe "#tag_labels" do + let!(:user) do + user = FactoryGirl.create(:user) + ["ruby", "perl", "php", "python", "java", "c"].each_with_index do |t,i| + 2.times do |x| + g = Gist.create!( + gid: "#{x.to_s}#{i.to_s}", + description: "MyString", + html_url: "MyString", + embed_url: "MyString", + public_gist: true, + user_id: user.id + ) + Tag.create!(name: t, user_id: user.id, gist_id: g.id) + end + end + user + end + + it "should get labels" do + expect(user.tag_labels).to eq(["ruby", "perl", "php", "python", "java", "c"]) + end + + it "should return empty array if user don't have tags" do + user = FactoryGirl.create(:user) + expect(user.tag_labels).to be_empty + end + end + + describe "#update_with_omniauth" do + let!(:user) {FactoryGirl.create(:user)} + + it "nickname should be updated" do + expect { + user.update_with_omniauth(valid_auth) + }.to change(user, :nickname) + end + + it "email should be updated" do + expect { + user.update_with_omniauth(valid_auth) + }.to change(user, :email) + end + + it "image_url should be updated" do + expect { + user.update_with_omniauth(valid_auth) + }.to change(user, :image_url) + end + + it "github_url should be updated" do + expect { + user.update_with_omniauth(valid_auth) + }.to change(user, :github_url) + end + + it "access_token should be updated" do + expect { + user.update_with_omniauth(valid_auth) + }.to change(user, :access_token) + end + + it "exception should be generated if not arguments" do + expect{user.update_with_omniauth}.to raise_error(ArgumentError) + end + + it "exception should be generated if argument is nil" do + expect{user.update_with_omniauth(nil)}.to raise_error(NoMethodError) + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b468569..2d0cd1e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -9,6 +9,8 @@ add_filter "/vendor/" end +ENV['RAILS_SECRET_KEY'] = "5ebe2294ecd0e0f08eab7690d2a6ee69" + # This file is copied to spec/ when you run 'rails generate rspec:install' ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__)