| @@ -1,3 +1,2 @@ | |||
| --color | |||
| --require spec_helper | |||
| --drb | |||
| @@ -39,7 +39,6 @@ group :development, :test do | |||
| gem 'pry-byebug' | |||
| end | |||
| gem 'pry-stack_explorer' | |||
| gem "spork-rails" | |||
| end | |||
| gem 'rspec-rails', '~> 3.0' | |||
| @@ -1,4 +1,3 @@ | |||
| require 'faker' | |||
| FactoryGirl.define do | |||
| factory :user do | |||
| @@ -7,4 +6,4 @@ FactoryGirl.define do | |||
| password "foobar" | |||
| password_confirmation "foobar" | |||
| end | |||
| end | |||
| end | |||
| @@ -0,0 +1,46 @@ | |||
| # This file is copied to spec/ when you run 'rails generate rspec:install' | |||
| ENV['RAILS_ENV'] ||= 'test' | |||
| require 'spec_helper' | |||
| require File.expand_path('../../config/environment', __FILE__) | |||
| require 'rspec/rails' | |||
| # Add additional requires below this line. Rails is not loaded until this point! | |||
| # Requires supporting ruby files with custom matchers and macros, etc, in | |||
| # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are | |||
| # run as spec files by default. This means that files in spec/support that end | |||
| # in _spec.rb will both be required and run as specs, causing the specs to be | |||
| # run twice. It is recommended that you do not name files matching this glob to | |||
| # end with _spec.rb. You can configure this pattern with the --pattern | |||
| # option on the command line or in ~/.rspec, .rspec or `.rspec-local`. | |||
| # | |||
| # The following line is provided for convenience purposes. It has the downside | |||
| # of increasing the boot-up time by auto-requiring all files in the support | |||
| # directory. Alternatively, in the individual `*_spec.rb` files, manually | |||
| # require only the support files necessary. | |||
| # | |||
| # Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } | |||
| RSpec.configure do |config| | |||
| # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures | |||
| config.fixture_path = "#{::Rails.root}/spec/fixtures" | |||
| # If you're not using ActiveRecord, or you'd prefer not to run each of your | |||
| # examples within a transaction, remove the following line or assign false | |||
| # instead of true. | |||
| config.use_transactional_fixtures = true | |||
| # RSpec Rails can automatically mix in different behaviours to your tests | |||
| # based on their file location, for example enabling you to call `get` and | |||
| # `post` in specs under `spec/controllers`. | |||
| # | |||
| # You can disable this behaviour by removing the line below, and instead | |||
| # explicitly tag your specs with their type, e.g.: | |||
| # | |||
| # RSpec.describe UsersController, :type => :controller do | |||
| # # ... | |||
| # end | |||
| # | |||
| # The different available types are documented in the features, such as in | |||
| # https://relishapp.com/rspec/rspec-rails/docs | |||
| config.infer_spec_type_from_file_location! | |||
| end | |||
| @@ -0,0 +1,71 @@ | |||
| require 'rails_helper' | |||
| RSpec.describe "Account request", :type => :request do | |||
| describe "用户登录" do | |||
| let(:user){FactoryGirl.create(:user)} | |||
| it "未登录访问需要登录页面会自动跳入登录页" do | |||
| get 'my/page' | |||
| expect(response).to redirect_to(signin_path) | |||
| end | |||
| context "正常登录" do | |||
| before{post signin_path, username: user.login, password: user.password} | |||
| it "登录成功,正常跳转" do | |||
| expect(response).to redirect_to(my_account_url) | |||
| end | |||
| it "登录成功,session正确" do | |||
| expect(user.id).to eq(session[:user_id]) | |||
| end | |||
| it "正常登录后可以访问需要认证的页面" do | |||
| get 'my/account' | |||
| expect(response).to have_http_status(:success) | |||
| expect(response.body).to include(user.login) | |||
| end | |||
| end | |||
| context "登录失败" do | |||
| before{post signin_path, username: user.login, password: 'wrong password'} | |||
| it {expect(response).to render_template('account/login')} | |||
| it "跳加登录页面" do | |||
| get 'my/page' | |||
| expect(response).to redirect_to(signin_path) | |||
| end | |||
| end | |||
| context "自动登录" do | |||
| before{ | |||
| post signin_path, username: user.login, password: user.password, autologin: 1 | |||
| } | |||
| it "登录成功跳转到个人首页" do | |||
| expect(response).to redirect_to(my_account_url) | |||
| end | |||
| it "验证token" do | |||
| token = Token.first | |||
| expect(token).not_to be_nil | |||
| expect(user.id).to eq(token.user.id) | |||
| expect(token.action).to eq('autologin') | |||
| expect(user.id).to eq(session[:user_id]) | |||
| expect(token.value).to eq(cookies['autologin']) | |||
| end | |||
| it 'session 失效后,可以用token自动重新登录' do | |||
| token = Token.first | |||
| reset! | |||
| User.current = nil | |||
| get my_account_url | |||
| expect(response).to redirect_to(signin_url) | |||
| cookies[:autologin] = token.value | |||
| get my_account_url | |||
| expect(response).to have_http_status(:success) | |||
| expect(response.body).to include(user.login) | |||
| end | |||
| end | |||
| end | |||
| end | |||
| @@ -1,53 +1,6 @@ | |||
| require 'rubygems' | |||
| require 'rspec/core' | |||
| require_relative 'support/spork_patch' | |||
| require 'spork' | |||
| #uncomment the following line to use spork with the debugger | |||
| #require 'spork/ext/ruby-debug' | |||
| Spork.prefork do | |||
| # Loading more in this block will cause your tests to run faster. However, | |||
| # if you change any configuration or code from libraries loaded here, you'll | |||
| # need to restart spork for it take effect. | |||
| end | |||
| Spork.each_run do | |||
| # This code will be run each time you run your specs. | |||
| end | |||
| # --- Instructions --- | |||
| # Sort the contents of this file into a Spork.prefork and a Spork.each_run | |||
| # block. | |||
| # | |||
| # The Spork.prefork block is run only once when the spork server is started. | |||
| # You typically want to place most of your (slow) initializer code in here, in | |||
| # particular, require'ing any 3rd-party gems that you don't normally modify | |||
| # during development. | |||
| # | |||
| # The Spork.each_run block is run each time you run your specs. In case you | |||
| # need to load files that tend to change during development, require them here. | |||
| # With Rails, your application modules are loaded automatically, so sometimes | |||
| # this block can remain empty. | |||
| # | |||
| # Note: You can modify files loaded *from* the Spork.each_run block without | |||
| # restarting the spork server. However, this file itself will not be reloaded, | |||
| # so if you change any of the code inside the each_run block, you still need to | |||
| # restart the server. In general, if you have non-trivial code in this file, | |||
| # it's advisable to move it into a separate file so you can easily edit it | |||
| # without restarting spork. (For example, with RSpec, you could move | |||
| # non-trivial code into a file spec/support/my_helper.rb, making sure that the | |||
| # spec/support/* files are require'd from inside the each_run block.) | |||
| # | |||
| # Any code that is left outside the two blocks will be run during preforking | |||
| # *and* during each_run -- that's probably not what you want. | |||
| # | |||
| # These instructions should self-destruct in 10 seconds. If they don't, feel | |||
| # free to delete them. | |||
| # require_relative 'support/spork_patch' | |||
| # This file was generated by the `rails generate rspec:install` command. Conventionally, all | |||
| # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. | |||
| @@ -0,0 +1,22 @@ | |||
| # https://stackoverflow.com/questions/24030907/spork-0-9-2-and-rspec-3-0-0-uninitialized-constant-rspeccorecommandline-n/24085168#24085168 | |||
| # https://github.com/manafire/spork/commit/38c79dcedb246daacbadb9f18d09f50cc837de51#diff-937afaa19ccfee172d722a05112a7c6fL6 | |||
| class Spork::TestFramework::RSpec | |||
| def run_tests(argv, stderr, stdout) | |||
| if rspec1? | |||
| ::Spec::Runner::CommandLine.run( | |||
| ::Spec::Runner::OptionParser.parse(argv, stderr, stdout) | |||
| ) | |||
| elsif rspec3? | |||
| options = ::RSpec::Core::ConfigurationOptions.new(argv) | |||
| ::RSpec::Core::Runner.new(options).run(stderr, stdout) | |||
| else | |||
| ::RSpec::Core::CommandLine.new(argv).run(stderr, stdout) | |||
| end | |||
| end | |||
| def rspec3? | |||
| return false if !defined?(::RSpec::Core::Version::STRING) | |||
| ::RSpec::Core::Version::STRING =~ /^3\./ | |||
| end | |||
| end | |||