From 0e3bb4ee2692cefc2df21e186b91682c06b9d262 Mon Sep 17 00:00:00 2001 From: Artur-Gaifutdinov Date: Fri, 28 Feb 2014 19:29:01 -0500 Subject: [PATCH 1/6] removed dead code, added more tests --- app/controllers/application_controller.rb | 7 ------- app/helpers/application_helper.rb | 4 ---- spec/models/deleted_user_spec.rb | 20 ++++++++++++++++++++ spec/unit/sluggifier_spec.rb | 2 -- 4 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 spec/models/deleted_user_spec.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 95596f7c..68f1059c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,14 +1,7 @@ class ApplicationController < ActionController::Base protect_from_forgery - helper_method :title - def title t=nil - @title = t unless t.blank? - @title - end - rescue_from CanCan::AccessDenied do |exception| redirect_to login_url, :alert => exception.message end - end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 3419e0a3..eb35501b 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,8 +1,4 @@ module ApplicationHelper - def program_path program - "/users/#{program.author_username}/programs/#{program.slug}" - end - def markdown(text) Redcarpet::Markdown.new(Redcarpet::Render::HTML.new(hard_wrap: true), autolink: true).render(text).html_safe end diff --git a/spec/models/deleted_user_spec.rb b/spec/models/deleted_user_spec.rb new file mode 100644 index 00000000..9afc850b --- /dev/null +++ b/spec/models/deleted_user_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe DeletedUser do + + before do + @user = DeletedUser.new + end + + it 'has deleted username' do + @user.username.should == 'Deleted User' + end + + it 'has no email' do + @user.email.should == 'none' + end + + it 'returns deleted user param' do + @user.to_param.should == 'deleted_user' + end +end \ No newline at end of file diff --git a/spec/unit/sluggifier_spec.rb b/spec/unit/sluggifier_spec.rb index 19afa177..9cc71ce9 100644 --- a/spec/unit/sluggifier_spec.rb +++ b/spec/unit/sluggifier_spec.rb @@ -22,6 +22,4 @@ end end end - - end From 0cabceeb82b40aceca9e3adb1b6d4c47b40061ea Mon Sep 17 00:00:00 2001 From: Artur-Gaifutdinov Date: Sun, 2 Mar 2014 23:16:31 -0500 Subject: [PATCH 2/6] Question Presenter spec --- app/presenters/question_presenter.rb | 4 +- spec/controllers/static_controller_spec.rb | 9 +++++ spec/fabricators/answer_fabricator.rb | 1 - spec/fabricators/question_fabricator.rb | 1 + spec/models/rel_spec.rb | 14 +++++++ spec/presenters/question_presenter_spec.rb | 46 ++++++++++++++++++++++ 6 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 spec/controllers/static_controller_spec.rb create mode 100644 spec/models/rel_spec.rb create mode 100644 spec/presenters/question_presenter_spec.rb diff --git a/app/presenters/question_presenter.rb b/app/presenters/question_presenter.rb index aaaba577..265a653e 100644 --- a/app/presenters/question_presenter.rb +++ b/app/presenters/question_presenter.rb @@ -22,7 +22,7 @@ def collection_path end def edit_resource_path - edit_question_path + edit_question_path(@question) end def apply_scope(chain) @@ -30,6 +30,6 @@ def apply_scope(chain) end def answers_path - question_answer_path(@question) + question_answers_path(@question) end end diff --git a/spec/controllers/static_controller_spec.rb b/spec/controllers/static_controller_spec.rb new file mode 100644 index 00000000..a304e045 --- /dev/null +++ b/spec/controllers/static_controller_spec.rb @@ -0,0 +1,9 @@ +require 'spec_helper' + +describe StaticController do + describe "GET root" do + it "responds with success" do + expect(:get => "/").to route_to(:controller => "static", :action => "root") + end + end +end \ No newline at end of file diff --git a/spec/fabricators/answer_fabricator.rb b/spec/fabricators/answer_fabricator.rb index a02e7ab2..25f90dd5 100644 --- a/spec/fabricators/answer_fabricator.rb +++ b/spec/fabricators/answer_fabricator.rb @@ -1,4 +1,3 @@ Fabricator(:answer) do description "MyString" - user "" end \ No newline at end of file diff --git a/spec/fabricators/question_fabricator.rb b/spec/fabricators/question_fabricator.rb index 2da52be4..74c4c5ea 100644 --- a/spec/fabricators/question_fabricator.rb +++ b/spec/fabricators/question_fabricator.rb @@ -1,4 +1,5 @@ Fabricator(:question) do + id "RandomID" title "Title" description "Description" user diff --git a/spec/models/rel_spec.rb b/spec/models/rel_spec.rb new file mode 100644 index 00000000..a614be8e --- /dev/null +++ b/spec/models/rel_spec.rb @@ -0,0 +1,14 @@ +require 'spec_helper' + +describe Rel do + + before do + @rel = Rel.create(:slug => 'text', :description => 'some text') + end + + context 'to_param' do + it 'returns slug' do + @rel.to_param.should == 'text' + end + end +end \ No newline at end of file diff --git a/spec/presenters/question_presenter_spec.rb b/spec/presenters/question_presenter_spec.rb new file mode 100644 index 00000000..13463fc7 --- /dev/null +++ b/spec/presenters/question_presenter_spec.rb @@ -0,0 +1,46 @@ +require 'spec_helper' + +describe QuestionPresenter do + + let(:question){Fabricate(:question)} + let(:another_question){Fabricate(:question, :id => 'AnotherID')} + let(:answer){Fabricate(:answer, :question_id => question.id)} + + before do + @qp = QuestionPresenter.new(question) + end + + context 'collection_path' do + it 'returns questions_path' do + @qp.collection_path.should == '/questions' + end + end + + context 'edit_resource_path' do + it 'returns edit_question_path' do + @qp.edit_resource_path.should == '/questions/RandomID/edit' + end + end + + context 'new_resource_path' do + it 'returns new_question_path' do + @qp.new_resource_path.should == '/questions/new' + end + end + + context 'resource_path' do + it 'returns self question path if nil' do + @qp.resource_path(nil).should == '/questions/RandomID' + end + + it 'returns question path if not nil' do + @qp.resource_path(another_question).should == '/questions/AnotherID' + end + end + + context 'answers_path' do + it 'returns questions answer path' do + @qp.answers_path.should == '/questions/RandomID/answers' + end + end +end \ No newline at end of file From a0d2ed1dfdb6e45543a6d9a5e810eac47fe3dc8f Mon Sep 17 00:00:00 2001 From: Artur-Gaifutdinov Date: Mon, 3 Mar 2014 21:06:26 -0500 Subject: [PATCH 3/6] Support presenter spec --- app/presenters/support_presenter.rb | 4 +- spec/presenters/support_presenter_spec.rb | 45 +++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 spec/presenters/support_presenter_spec.rb diff --git a/app/presenters/support_presenter.rb b/app/presenters/support_presenter.rb index 2075cf09..acb871c5 100644 --- a/app/presenters/support_presenter.rb +++ b/app/presenters/support_presenter.rb @@ -18,14 +18,14 @@ def collection_path end def edit_resource_path - edit_support_question_path + edit_support_question_path(@support_question) end def resource_path(question) if question support_question_path(question) else - support_question_path(@question) + support_question_path(@support_question) end end diff --git a/spec/presenters/support_presenter_spec.rb b/spec/presenters/support_presenter_spec.rb new file mode 100644 index 00000000..6dd9ac83 --- /dev/null +++ b/spec/presenters/support_presenter_spec.rb @@ -0,0 +1,45 @@ +require 'spec_helper' + +describe SupportPresenter do + + let(:question){Fabricate(:question)} + let(:another_question){Fabricate(:question, :id => 'AnotherID')} + + before do + @sp = SupportPresenter.new(question) + end + + context 'page_title' do + it 'returns support questions title' do + @sp.page_title.should == 'Support Questions' + end + end + + context 'collection_path' do + it 'returns support questions_path' do + @sp.collection_path.should == '/support/questions' + end + end + + context 'edit_resource_path' do + it 'returns edit_support_question_path' do + @sp.edit_resource_path.should == '/support/questions/RandomID/edit' + end + end + + context 'new_resource_path' do + it 'returns new_support_question_path' do + @sp.new_resource_path.should == '/support/questions/new' + end + end + + context 'resource_path' do + it 'returns self question path if nil' do + @sp.resource_path(nil).should == '/support/questions/RandomID' + end + + it 'returns question path if not nil' do + @sp.resource_path(another_question).should == '/support/questions/AnotherID' + end + end +end \ No newline at end of file From 32ffa202f773de5a292e40986c1e29cffe2e0561 Mon Sep 17 00:00:00 2001 From: Artur-Gaifutdinov Date: Tue, 4 Mar 2014 20:24:45 -0500 Subject: [PATCH 4/6] Rels and static controllers spec --- app/controllers/api/rels_controller.rb | 2 +- spec/controllers/rel_controller_spec.rb | 21 +++++++++++++++++++++ spec/controllers/static_controller_spec.rb | 15 +++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 spec/controllers/rel_controller_spec.rb diff --git a/app/controllers/api/rels_controller.rb b/app/controllers/api/rels_controller.rb index 3cc30f1f..00894e2e 100644 --- a/app/controllers/api/rels_controller.rb +++ b/app/controllers/api/rels_controller.rb @@ -6,4 +6,4 @@ def index def show @rel = Rel.first(:slug => params[:id]) end -end +end \ No newline at end of file diff --git a/spec/controllers/rel_controller_spec.rb b/spec/controllers/rel_controller_spec.rb new file mode 100644 index 00000000..3f8f30a1 --- /dev/null +++ b/spec/controllers/rel_controller_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +describe Api::RelsController do + + let(:slug){Fabricate(:rel)} + + + describe "GET index" do + it "responds with success" do + get :index + response.should be_success + end + end + + describe "GET show" do + it "responds with success" do + get :show + response.should be_success + end + end +end \ No newline at end of file diff --git a/spec/controllers/static_controller_spec.rb b/spec/controllers/static_controller_spec.rb index a304e045..ece397af 100644 --- a/spec/controllers/static_controller_spec.rb +++ b/spec/controllers/static_controller_spec.rb @@ -6,4 +6,19 @@ expect(:get => "/").to route_to(:controller => "static", :action => "root") end end + + describe "GET api_root" do + it "should render api layout" do + get :api_root + response.should render_template(:layout => "api") + end + end + + describe "GET newest_version" do + it "should render version of api" do + get :newest_version + response.header['Content-Type'].should include 'application/json' + response.body.should include "1.0.0".to_json + end + end end \ No newline at end of file From d39e127a15a8115cff146ce9c1b996449df56d6d Mon Sep 17 00:00:00 2001 From: Artur-Gaifutdinov Date: Thu, 6 Mar 2014 22:19:28 -0500 Subject: [PATCH 5/6] Helpers spec --- spec/controllers/mailer_controller_spec.rb | 7 +++++++ spec/controllers/questions_controller_spec.rb | 1 - spec/controllers/rel_controller_spec.rb | 2 -- spec/controllers/user_controller_spec.rb | 7 +++++++ spec/helpers/program_helper_spec.rb | 20 +++++++++++++++++++ 5 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 spec/helpers/program_helper_spec.rb diff --git a/spec/controllers/mailer_controller_spec.rb b/spec/controllers/mailer_controller_spec.rb index c65b252a..2c5f7554 100644 --- a/spec/controllers/mailer_controller_spec.rb +++ b/spec/controllers/mailer_controller_spec.rb @@ -88,6 +88,13 @@ ActionMailer::Base.deliveries.last.body.to_s.should match @message.body end end + + describe 'empty message' do + it "returns exception" do + post :create + expect(subject).to render_template(:new) + end + end end context 'when user is not a moderator' do diff --git a/spec/controllers/questions_controller_spec.rb b/spec/controllers/questions_controller_spec.rb index 54222ec1..c6e15a89 100644 --- a/spec/controllers/questions_controller_spec.rb +++ b/spec/controllers/questions_controller_spec.rb @@ -1,7 +1,6 @@ require 'spec_helper' describe QuestionsController do - describe "GET index" do context "with format atom" do it "responds with success" do diff --git a/spec/controllers/rel_controller_spec.rb b/spec/controllers/rel_controller_spec.rb index 3f8f30a1..ac13dba5 100644 --- a/spec/controllers/rel_controller_spec.rb +++ b/spec/controllers/rel_controller_spec.rb @@ -1,10 +1,8 @@ require 'spec_helper' describe Api::RelsController do - let(:slug){Fabricate(:rel)} - describe "GET index" do it "responds with success" do get :index diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb index be688723..f756c742 100644 --- a/spec/controllers/user_controller_spec.rb +++ b/spec/controllers/user_controller_spec.rb @@ -50,4 +50,11 @@ expect(flash[:notice]).to eq("You're no longer following #{mozart.username}") end end + + describe 'Deleted user' do + it 'should create new deleted user' do + get :deleted_user + response.should be_success + end + end end diff --git a/spec/helpers/program_helper_spec.rb b/spec/helpers/program_helper_spec.rb new file mode 100644 index 00000000..8a4e4c6e --- /dev/null +++ b/spec/helpers/program_helper_spec.rb @@ -0,0 +1,20 @@ +require "spec_helper" + +describe ProgramsHelper do + let(:simple) { Fabricate(:program) } + let(:complicated) { Fabricate(:program, :author_username => nil) } + + describe "#program_link" do + it "returns author's program path if author username exists" do + helper.program_link(simple).should == "#{link_to(simple.title.titleize, + user_program_path(simple.author_username, simple))}".html_safe + end + + it "returns url by program's slug if author username doesn't exist" do + helper.program_link(complicated).should == "#{link_to(complicated.title.titleize, + url_for(:controller => '/programs', + :action => 'show', + :id => complicated.slug))}".html_safe + end + end +end \ No newline at end of file From cf442d3c955578fa53258785cf31a73a1d0720c4 Mon Sep 17 00:00:00 2001 From: Artur-Gaifutdinov Date: Sat, 8 Mar 2014 23:36:46 -0500 Subject: [PATCH 6/6] User model spec --- features/step_definitions/user_steps.rb | 13 ++++++++++++- features/users.feature | 8 ++++++-- spec/models/user_spec.rb | 16 +++++++++++++--- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/features/step_definitions/user_steps.rb b/features/step_definitions/user_steps.rb index c3395f59..dafb78b1 100644 --- a/features/step_definitions/user_steps.rb +++ b/features/step_definitions/user_steps.rb @@ -1,5 +1,5 @@ def login_user - @user = User.create!(username: "test_user", + @user = User.create!(username: "test_user", email: "test_user@example.com", password: "foobar", password_confirmation: "foobar") @@ -20,6 +20,13 @@ def create_other_user login_user unless @user end +Given /^a steve exists$/ do + @steve = User.create!(username: "steve", + email: "steve_user@example.com", + password: "foobar", + password_confirmation: "foobar") +end + When /^I go to look at my profile page$/ do visit user_path(@user) end @@ -73,3 +80,7 @@ def create_other_user page.should have_content("Test user likes to edit his profile") end +Then(/^I should see 'Steve'$/) do + @user.following?(@steve).should == true + page.should have_link @steve.username +end diff --git a/features/users.feature b/features/users.feature index cbe60fdc..2d67ae5d 100644 --- a/features/users.feature +++ b/features/users.feature @@ -3,7 +3,8 @@ Feature: Manage account As a user of this site, I can view and update my profile, see my followers Background: - Given a logged in user + Given a steve exists + And a logged in user Scenario: View my profile When I go to look at my profile page @@ -23,4 +24,7 @@ Feature: Manage account When I am following someone And I click on the number of people I am following on my profile Then I should see someone I'm following - + + Scenario: I should always follow steve and vice versa + When I click on the number of people I am following on my profile + Then I should see 'Steve' diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 39a70431..c8d70a56 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,10 +1,10 @@ require 'spec_helper' describe User do - - let(:bob){Fabricate(:user)} + let(:bob){Fabricate(:user, :username => 'hacker')} let(:mozart){Fabricate(:user)} - + let(:hello_world){Fabricate(:program, :author_username => bob.username)} + it 'can be created validly' do bob.should be_valid mozart.should be_valid @@ -17,4 +17,14 @@ bob.reload bob.following?(mozart).should be_false end + + describe 'programs' do + it 'should return programs list' do + bob.programs.class.should == Plucky::Query + hello_world.author_username.should == 'hacker' + bob.username.should == 'hacker' + bob.programs.count.should == 1 + bob.programs.first.should == hello_world + end + end end \ No newline at end of file