Skip to content
This repository has been archived by the owner on Nov 27, 2022. It is now read-only.

Commit

Permalink
add testcase for user signin context
Browse files Browse the repository at this point in the history
  • Loading branch information
mzp committed Apr 15, 2014
1 parent b2ddb8f commit cb5a209
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 107 deletions.
5 changes: 3 additions & 2 deletions app/controllers/dashboard_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class DashboardController < ApplicationController
def index
@revisions = Revision.order(date: :desc).limit(4)
def index
p current_user
@revisions = Revision.order(date: :desc).limit(4)
end
end
1 change: 1 addition & 0 deletions app/controllers/github_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class GithubController < ApplicationController
skip_before_filter :verify_authenticity_token, only: :hook
skip_before_filter :authenticate_user!, only: :hook
include ErrorHandle
def index
end
Expand Down
1 change: 1 addition & 0 deletions app/controllers/revisions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class RevisionsController < ApplicationController
include ErrorHandle
skip_before_action :authenticate_user!, only: [:show, :via_hash]

# GET /revisions
# GET /revisions.json
Expand Down
2 changes: 0 additions & 2 deletions app/helpers/authentication_helper.rb

This file was deleted.

45 changes: 27 additions & 18 deletions spec/controllers/dashboard_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
require 'spec_helper'

describe DashboardController do

before do
user = User.create!
user.save
sign_in user
context "not sign-in" do
describe "GET 'index'" do
before { get 'index' }
subject { response }
it { should redirect_to(new_user_session_path) }
end
end

describe "GET 'index'" do
context "user sign-in" do
before do
@foo = create(:revision, hash_code: 'foo')
@bar = create(:revision, hash_code: 'bar')
@baz = create(:revision, hash_code: 'baz')
get 'index'
user = User.create!
user.save
sign_in user
end

describe 'response' do
subject { response }
it { should be_success }
end
describe "GET 'index'" do
before do
@foo = create(:revision, hash_code: 'foo')
@bar = create(:revision, hash_code: 'bar')
@baz = create(:revision, hash_code: 'baz')
get 'index'
end

describe 'response' do
subject { response }
it { should be_success }
end

describe 'revisions' do
subject { assigns(:revisions) }
describe 'revisions' do
subject { assigns(:revisions) }

it { should_not be_empty }
it { should include(@foo,@bar,@baz) }
it { should_not be_empty }
it { should include(@foo,@bar,@baz) }
end
end
end
end
106 changes: 64 additions & 42 deletions spec/controllers/github_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,94 @@
require 'ostruct'

describe GithubController do
before do
user = User.create!
user.save
sign_in user
end

def commit(hash_code, url, log)
OpenStruct.new(hash_code: hash_code,
url: url,
log: log)
end

describe "GET 'index'" do
before do
get 'index'
end

describe 'response' do
context "not signin" do
before { get 'index' }
subject { response }
it { should be_success }
it { should redirect_to(new_user_session_path) }
end
end

describe "POST 'import'" do
context 'success' do
context "signin" do
before do
create(:revision, hash_code: 'bar')

RevisionIt::Service::Github.
stub(:commits).
with('https://github.com/codefirst/revision-it').
and_yield(commit("foo", "http://example.com", "this is text")).
and_yield(commit("bar", "http://example.com", "this is text"))
user = User.create!
user.save
sign_in user
end

post 'import_all', url: 'https://github.com/codefirst/revision-it'
before do
get 'index'
end

describe 'response' do
subject { response }
it { should redirect_to(github_path) }
it { should be_success }
end
end
end

describe 'new revision' do
subject { Revision.where(hash_code: 'foo').first }
its(:log) { should == 'this is text' }
its(:url) { should == 'http://example.com' }
describe "POST 'import'" do
context "not signin" do
before { get 'index' }
subject { response }
it { should redirect_to(new_user_session_path) }
end

context "signin" do

before do
user = User.create!
user.save
sign_in user
end

describe 'update revision' do
subject { Revision.where(hash_code: 'bar').first }
its(:log) { should == 'this is text' }
its(:url) { should == 'http://example.com' }
context 'success' do
before do
create(:revision, hash_code: 'bar')

RevisionIt::Service::Github.
stub(:commits).
with('https://github.com/codefirst/revision-it').
and_yield(commit("foo", "http://example.com", "this is text")).
and_yield(commit("bar", "http://example.com", "this is text"))

post 'import_all', url: 'https://github.com/codefirst/revision-it'
end

describe 'response' do
subject { response }
it { should redirect_to(github_path) }
end

describe 'new revision' do
subject { Revision.where(hash_code: 'foo').first }
its(:log) { should == 'this is text' }
its(:url) { should == 'http://example.com' }
end

describe 'update revision' do
subject { Revision.where(hash_code: 'bar').first }
its(:log) { should == 'this is text' }
its(:url) { should == 'http://example.com' }
end
end
end

context 'error' do
describe 'invalid url' do
before {
post 'import_all', url: 'https://example.com'
}
context 'error' do
describe 'invalid url' do
before {
post 'import_all', url: 'https://example.com'
}

subject { response }
it { should_not be_success }
its(:status) { should == 400 }
subject { response }
it { should_not be_success }
its(:status) { should == 400 }
end
end

end
end

Expand Down
44 changes: 26 additions & 18 deletions spec/controllers/revision_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
require 'spec_helper'

describe RevisionsController do
before do
user = User.create!
user.save
sign_in user
end

describe "GET 'index'" do
before do
100.times {|i|
create(:revision, hash_code: "commit-#{i}")
}
get 'index'
end

describe 'response' do
context "not signin" do
before { get 'index' }
subject { response }
it { should be_success }
it { should redirect_to(new_user_session_path) }
end

describe 'assigns' do
subject { assigns :revisions }
it { should have(100).items }
context "signin" do
before do
user = User.create!
user.save
sign_in user
end

before do
100.times {|i|
create(:revision, hash_code: "commit-#{i}")
}
get 'index'
end

describe 'response' do
subject { response }
it { should be_success }
end

describe 'assigns' do
subject { assigns :revisions }
it { should have(100).items }
end
end
end

Expand Down
25 changes: 20 additions & 5 deletions spec/controllers/welcome_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
require 'spec_helper'

describe WelcomeController do

describe "GET 'index'" do
it "returns http success" do
get 'index'
response.should be_success
context "not sign in" do
describe "GET 'index'" do
it "returns http success" do
get 'index'
response.should be_success
end
end
end

context "user sign in" do
describe "GET 'index'" do
before do
user = User.create!
user.save
sign_in user
end

it "returns http success" do
get 'index'
response.should redirect_to(dashboard_path)
end
end
end
end
15 changes: 0 additions & 15 deletions spec/helpers/authentication_helper_spec.rb

This file was deleted.

5 changes: 0 additions & 5 deletions spec/models/user_spec.rb

This file was deleted.

0 comments on commit cb5a209

Please sign in to comment.