| 
							
								 | 
							
							require 'rails_helper'
 | 
						
						
						
						
							 | 
							
								 | 
							
							require 'shared_account_spec'
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							RSpec.describe "Account request", :type => :request do
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							  describe "注册用户" do
 | 
						
						
						
						
							 | 
							
								 | 
							
							    include_context "create user"
 | 
						
						
						
						
							 | 
							
								 | 
							
							    it "正常注册可以成功" do
 | 
						
						
						
						
							 | 
							
								 | 
							
							      shared_register
 | 
						
						
						
						
							 | 
							
								 | 
							
							      expect(response).to redirect_to(my_account_url)
 | 
						
						
						
						
							 | 
							
								 | 
							
							    end
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							    it "修改用户资料" do
 | 
						
						
						
						
							 | 
							
								 | 
							
							      shared_register
 | 
						
						
						
						
							 | 
							
								 | 
							
							      shared_update_user
 | 
						
						
						
						
							 | 
							
								 | 
							
							      expect(response).to redirect_to(user_url(session[:user_id]))
 | 
						
						
						
						
							 | 
							
								 | 
							
							    end
 | 
						
						
						
						
							 | 
							
								 | 
							
							  end
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 | 
						
						
						
						
							 | 
							
								 | 
							
							  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
 | 
						
						
						
						
							 | 
							
								 | 
							
							
 |