we_bridge_rails_engine_users 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +37 -0
- data/app/assets/javascripts/users.js +2 -0
- data/app/assets/stylesheets/scaffold.css +56 -0
- data/app/assets/stylesheets/users.css +4 -0
- data/app/controllers/sessions_controller.rb +82 -0
- data/app/controllers/users_controller.rb +9 -0
- data/app/helpers/users_helper.rb +2 -0
- data/app/models/person.rb +13 -0
- data/app/models/person_name.rb +6 -0
- data/app/models/person_name_text.rb +24 -0
- data/app/models/person_status.rb +2 -0
- data/app/models/person_text.rb +4 -0
- data/app/models/user.rb +12 -0
- data/app/models/user_account.rb +13 -0
- data/app/models/user_text.rb +13 -0
- data/app/views/sessions/login.html.builder +23 -0
- data/app/views/users/_form.html.builder +14 -0
- data/app/views/users/edit.html.builder +1 -0
- data/app/views/users/index.html.builder +1 -0
- data/app/views/users/new.html.builder +1 -0
- data/app/views/users/show.html.builder +1 -0
- data/config/routes.rb +14 -0
- data/db/migrate/20150611132719_add_sessions_table.rb +12 -0
- data/db/migrate/20150612005752_create_people.rb +41 -0
- data/db/migrate/20150712132226_add_users.rb +41 -0
- data/lib/tasks/we_bridge_rails_engine_langs_tasks.rake +4 -0
- data/lib/we_bridge_rails_engine_users.rb +16 -0
- data/lib/we_bridge_rails_engine_users/application_controller.rb +41 -0
- data/lib/we_bridge_rails_engine_users/engine.rb +13 -0
- data/lib/we_bridge_rails_engine_users/provider_settings.rb +76 -0
- data/lib/we_bridge_rails_engine_users/version.rb +3 -0
- data/spec/controllers/sessions_controller_spec.rb +95 -0
- data/spec/controllers/users_controller_spec.rb +182 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/javascripts/application.js +16 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/controllers/application_controller.rb +8 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/views/layouts/application.html.builder +14 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +29 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +28 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +41 -0
- data/spec/dummy/config/environments/production.rb +79 -0
- data/spec/dummy/config/environments/test.rb +42 -0
- data/spec/dummy/config/initializers/assets.rb +11 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/provider_settings.rb +2 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/omniauth.yml +63 -0
- data/spec/dummy/config/omniauth.yml.org +63 -0
- data/spec/dummy/config/routes.rb +56 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/migrate/20150803101150_create_langs.rb +20 -0
- data/spec/dummy/db/schema.rb +134 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +6092 -0
- data/spec/dummy/log/test.log +17718 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/95kd6PNg5I5Dn8j-ds2kESZ4yqJWDlUtJlRkBYcHG9I.cache +2 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/9HedZDeYq2v6HBA9bD890T4cGZibttejhDq0xfEW1OA.cache +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/Cgic9Tyvkk6sqvxCRHMOht8-YWqOpKEbwWLaLUhtCu8.cache +1 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/DztYUoD64LBf1ciKDNbwfU3ggD7rYJ4R0V_4HPJxzL4.cache +1 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/EQFBBT0dblIxqvZw8BbpahKH5sRVDrYirvYcxKXzg6s.cache +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/G4HidZB-L5zKNezfe7O20FU58YFyoibIF0zyMpkigNA.cache +1 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/H5b9tPX4ofo6fe6MRg91ChMHmowlsYkfUKJ9b0NUPIU.cache +3 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/HYpkPupwGozKHs5qChj3sJ-IhRP-YDSiqc-n_hQB1RU.cache +1 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/N4s9e38lth9IeIVQYEn7L9iPWJJEt_FeNaWXgogeW80.cache +1 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/OvqhBxVGrgbFQCuriofYnoMd-Hh6MyK7cCQHpSQ6mY8.cache +1 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/Px2rpH6lCQmniOcJktyX-jHQL2GJFj42xgRDMwftICg.cache +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/TFk4N5unhxWMiNeFWRf2BmB7KZ2y9c6v4RR7SWHFSl8.cache +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/To7LnOAuTthN8xMQdWQRC_bDQ1AeR2Jt1SihvBvu7VM.cache +1 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/Tuk6Xmd6rNalBiAHW2q9yxztUsaXh6vGBN4kuwW4coo.cache +2 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/UthTiMoUK3z7QvIjgYJBlSTzwZ37UE-dam-92yUeRlg.cache +1 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/VhMjuakubXLw9TGrYLMTshl1haIqjdUQDFmrtWqFg48.cache +2 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/W6OPXh5hmWxHo1cCFc6MpmejeEJM-OWXJkmXVnoDdjk.cache +1 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/ivn9YXCSjX-45u61mNupKlBqHNTDKvF7tsbBnXjtu3o.cache +1 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/iyOlCOrvKIBBbvSO3lENE8lJeeFJpDFUKEP4NAUTizg.cache +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/j7FsLjc9QBY0x2gZy--i0nskAf-2VCkQ5tCr7M1FKww.cache +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/kR7RRgmwH5OahOaSgGVRid1-zpVfnzh9ktcpTc2r8U8.cache +1 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/mAcPZ6Lxmw7lRLF2rOOauuET5gBKl9QaZJgnOkL56Uw.cache +2 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/mOtV0PLRbjn4_sQL0ZHsuAv79ctsBmFq0tLYtQFngZs.cache +1 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/msJs71sOQgd6VP4kducGvRsZ3uZNW4d_vksrGcVZtH4.cache +2 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/pCDWkYCgTWLKByBqi8TDJneTltLoW9rReMGsBiGm9Uo.cache +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/v3.0/pOOmld85ShNKx5kGi9BubXdV_VZJntx0EoTivl0-bBM.cache +1 -0
- data/spec/dummy/tmp/pids/server.pid +1 -0
- data/spec/factories/users.rb +6 -0
- data/spec/helpers/users_helper_spec.rb +15 -0
- data/spec/models/user_spec.rb +5 -0
- data/spec/omniauth.yml +63 -0
- data/spec/rails_helper.rb +54 -0
- data/spec/routing/users_routing_spec.rb +31 -0
- data/spec/spec_helper.rb +95 -0
- data/spec/views/users/edit.html.builder_spec.rb +16 -0
- data/spec/views/users/index.html.builder_spec.rb +16 -0
- data/spec/views/users/new.html.builder_spec.rb +14 -0
- data/spec/views/users/show.html.builder_spec.rb +13 -0
- metadata +383 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
class AddUsers < ActiveRecord::Migration
|
|
2
|
+
def change
|
|
3
|
+
create_table :users do |t|
|
|
4
|
+
t.integer :person_id, null: true
|
|
5
|
+
t.string :email
|
|
6
|
+
t.string :user_img_url
|
|
7
|
+
t.integer :lang_id, null: false
|
|
8
|
+
t.datetime :deleted_at
|
|
9
|
+
t.timestamps
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
create_table :user_texts do |t|
|
|
13
|
+
t.integer :parent_id, null: false
|
|
14
|
+
t.integer :lang_id, null: false
|
|
15
|
+
t.string :nick_name, null: true
|
|
16
|
+
t.string :nick_name_pron, null: true
|
|
17
|
+
t.datetime :deleted_at
|
|
18
|
+
t.timestamps
|
|
19
|
+
end
|
|
20
|
+
add_index :user_texts, [:lang_id,:parent_id], unique: true
|
|
21
|
+
|
|
22
|
+
create_table :user_accounts do |t|
|
|
23
|
+
t.integer :user_id, null: false
|
|
24
|
+
t.string :provider,null: false
|
|
25
|
+
t.string :uid, null: false
|
|
26
|
+
t.string :token
|
|
27
|
+
t.string :name
|
|
28
|
+
t.string :nickname
|
|
29
|
+
t.string :email
|
|
30
|
+
t.string :url
|
|
31
|
+
t.string :image_url
|
|
32
|
+
t.string :description
|
|
33
|
+
t.text :other
|
|
34
|
+
t.text :credentials
|
|
35
|
+
t.text :raw_info
|
|
36
|
+
t.datetime :deleted_at
|
|
37
|
+
t.timestamps
|
|
38
|
+
end
|
|
39
|
+
add_index :user_accounts, [:uid], unique: true
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require "we_bridge_rails_engine_users/engine"
|
|
2
|
+
require "we_bridge_rails_engine_users/application_controller"
|
|
3
|
+
require "we_bridge_rails_engine_users/provider_settings"
|
|
4
|
+
require 'we_bridge/html_builder'
|
|
5
|
+
require "we_bridge/exceptions"
|
|
6
|
+
require 'omniauth'
|
|
7
|
+
|
|
8
|
+
module WeBridgeRailsEngineUsers
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
WeBridge::HtmlBuilder.register_template_handler
|
|
12
|
+
|
|
13
|
+
OmniAuth.config.before_request_phase = proc{|env|
|
|
14
|
+
req = Rack::Request.new(env)
|
|
15
|
+
req.session[:redirect_to] = req.params["redirect_to"] || req.referrer
|
|
16
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module WeBridgeRailsEngineUsers
|
|
2
|
+
module ApplicationController
|
|
3
|
+
def self.included(application_controller)
|
|
4
|
+
application_controller.helper_method :current_user, :logged_in?
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def login_required
|
|
8
|
+
if !current_user
|
|
9
|
+
raise WeBridge::Exceptions::UnauthorizedError
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def logged_in?
|
|
14
|
+
!!current_user
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def set_user_id(user_id)
|
|
18
|
+
session[:user_id] = user_id
|
|
19
|
+
current_user
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def check_current_user(user,message=nil)
|
|
23
|
+
if !(current_user && user == current_user)
|
|
24
|
+
raise WeBridge::Exceptions::ForbiddenError
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def set_user!(id=nil)
|
|
29
|
+
@user = User.find(id || params[:user_id] || params[:id])
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def current_user
|
|
33
|
+
uid = session[:user_id]
|
|
34
|
+
if @current_user && uid.to_i != @current_user.id
|
|
35
|
+
@current_user = nil
|
|
36
|
+
end
|
|
37
|
+
@current_user ||= User.find_by(id: uid) if uid
|
|
38
|
+
@current_user
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module WeBridgeRailsEngineUsers
|
|
2
|
+
class Engine < ::Rails::Engine
|
|
3
|
+
config.generators do |g|
|
|
4
|
+
g.test_framework :rspec, fixture: false, view_specs: true, helper_specs: true, controller_specs: true, request_specs: true, route_specs: true, model_specs: true
|
|
5
|
+
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
|
|
6
|
+
g.template_engine :builder
|
|
7
|
+
g.assets true
|
|
8
|
+
g.helper true
|
|
9
|
+
g.stylesheets true
|
|
10
|
+
g.javascripts true
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
require 'yaml'
|
|
2
|
+
module WeBridgeRailsEngineUsers
|
|
3
|
+
class ProviderSettings < Hash
|
|
4
|
+
|
|
5
|
+
@@instance = nil
|
|
6
|
+
@@file_loaded = false
|
|
7
|
+
|
|
8
|
+
def self.instance
|
|
9
|
+
@@instance ||= self.new
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.file_loaded?
|
|
13
|
+
!!@@file_loaded
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.reload!
|
|
17
|
+
@@instance = nil
|
|
18
|
+
@@file_loaded = false
|
|
19
|
+
self.instance.merge!(YAML.load_file(self.file_path)[Rails.env].symbolize_keys!.to_hash)
|
|
20
|
+
self.set_middleware
|
|
21
|
+
self
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
@@file_path = nil
|
|
25
|
+
|
|
26
|
+
def self.file_path=(path)
|
|
27
|
+
@@file_path = path
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.file_path
|
|
31
|
+
@@file_path || "#{Rails.root}/config/omniauth.yml"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.availables(&block)
|
|
35
|
+
rtn = self.instance.delete_if{|key,conf|
|
|
36
|
+
!(conf['key'] && conf['secret'])
|
|
37
|
+
}
|
|
38
|
+
rtn.each do |oauth_provider_symbol,conf|
|
|
39
|
+
name = conf['name'] || oauth_provider_symbol
|
|
40
|
+
block.call(name,conf,oauth_provider_symbol)
|
|
41
|
+
end if block
|
|
42
|
+
rtn
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.request_path(name)
|
|
46
|
+
"/auth/#{name}"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def self.callback_path(name)
|
|
50
|
+
"/auth/#{name}/callback"
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def self.auth_callback_as(name)
|
|
54
|
+
"auth_callback_#{name}"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def self.auth_as(name)
|
|
58
|
+
"auth_#{name}"
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def self.set_middleware
|
|
62
|
+
mw = Rails.application.config.middleware
|
|
63
|
+
mw.delete OmniAuth::Builder
|
|
64
|
+
mw.use OmniAuth::Builder do
|
|
65
|
+
WeBridgeRailsEngineUsers::ProviderSettings.availables do |name, conf, oauth_provider_symbol|
|
|
66
|
+
conf = conf.symbolize_keys
|
|
67
|
+
key,secret = conf[:key], conf[:secret]
|
|
68
|
+
conf.merge!(
|
|
69
|
+
request_path: ProviderSettings.request_path(name),
|
|
70
|
+
callback_path: ProviderSettings.callback_path(name))
|
|
71
|
+
provider oauth_provider_symbol, key, secret, conf.merge(key: nil, secret: nil, name: nil)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
require 'rails_helper'
|
|
2
|
+
RSpec.describe SessionsController, type: :controller do
|
|
3
|
+
|
|
4
|
+
# This should return the minimal set of attributes required to create a valid
|
|
5
|
+
# model. As you add validations to model, be sure to
|
|
6
|
+
# adjust the attributes here as well.
|
|
7
|
+
let(:valid_attributes) {
|
|
8
|
+
{email: 'tester@baseball-bridge.org', lang_id: Lang.find_or_create_by(code: :ja).id}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
let(:invalid_attributes) {
|
|
12
|
+
{lang_id: nil}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
# This should return the minimal set of values that should be in the session
|
|
16
|
+
# in order to pass any filters (e.g. authentication) defined in
|
|
17
|
+
# Controller. Be sure to keep this updated too.
|
|
18
|
+
let(:valid_session) { {} }
|
|
19
|
+
|
|
20
|
+
let(:model) { User }
|
|
21
|
+
|
|
22
|
+
let(:new_attributes) {
|
|
23
|
+
valid_attributes.merge(email: 'tester2@baseball-bridge.org')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
describe "GET #login" do
|
|
27
|
+
it "assigns all records as @records" do
|
|
28
|
+
# record = model.create! valid_attributes
|
|
29
|
+
get :login, {}, valid_session
|
|
30
|
+
# expect(assigns(:records)).to eq([record])
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
describe "GET #login" do
|
|
35
|
+
it "login page" do
|
|
36
|
+
get :login
|
|
37
|
+
expect(response.status).to eq(200)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
describe "GET #callback" do
|
|
42
|
+
before :each do
|
|
43
|
+
WeBridgeRailsEngineUsers::ProviderSettings.file_path = File.expand_path('../../omniauth.yml',__FILE__)
|
|
44
|
+
end
|
|
45
|
+
let(:auth){
|
|
46
|
+
provider = nil
|
|
47
|
+
WeBridgeRailsEngineUsers::ProviderSettings.availables do |name|
|
|
48
|
+
provider = name; break;
|
|
49
|
+
end
|
|
50
|
+
{
|
|
51
|
+
'provider' => provider,
|
|
52
|
+
'uid' => '123545',
|
|
53
|
+
'info' => {
|
|
54
|
+
'email' => 'tester@baseball-bridge.org',
|
|
55
|
+
'image' => 'http://www.baseball-bridge.org/image/1.jpg'
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
before do
|
|
61
|
+
request.env["omniauth.auth"] = auth
|
|
62
|
+
# @path = WeBridgeRailsEngineUsers::ProviderSettings.callback_path(auth['provider'])
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it "no user , no account" do
|
|
66
|
+
get :callback, provider: auth['provider']
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
it "no user , some account" do
|
|
70
|
+
UserAccount.find_or_create_by(user_id: -1, provider: auth['provider'], uid: auth['uid'])
|
|
71
|
+
get :callback, provider: auth['provider']
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
it "some user , some account" do
|
|
75
|
+
user = User.create!(lang_id: 1)
|
|
76
|
+
ua = UserAccount.find_or_create_by(provider: auth['provider'], uid: auth['uid'], user_id: user.id)
|
|
77
|
+
get :callback, provider: auth['provider']
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
describe "GET #failure" do
|
|
82
|
+
it "normal" do
|
|
83
|
+
get :failure
|
|
84
|
+
expect(response).to redirect_to(action: :login)
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
describe "GET #logout" do
|
|
89
|
+
it "normal" do
|
|
90
|
+
get :logout, {}, {user_id: 1}
|
|
91
|
+
expect(session[:user_id]).to be_nil
|
|
92
|
+
expect(response).to redirect_to(action: :login)
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
require 'rails_helper'
|
|
2
|
+
|
|
3
|
+
# This spec was generated by rspec-rails when you ran the scaffold generator.
|
|
4
|
+
# It demonstrates how one might use RSpec to specify the controller code that
|
|
5
|
+
# was generated by Rails when you ran the scaffold generator.
|
|
6
|
+
#
|
|
7
|
+
# It assumes that the implementation code is generated by the rails scaffold
|
|
8
|
+
# generator. If you are using any extension libraries to generate different
|
|
9
|
+
# controller code, this generated spec may or may not pass.
|
|
10
|
+
#
|
|
11
|
+
# It only uses APIs available in rails and/or rspec-rails. There are a number
|
|
12
|
+
# of tools you can use to make these specs even more expressive, but we're
|
|
13
|
+
# sticking to rails and rspec-rails APIs to keep things simple and stable.
|
|
14
|
+
#
|
|
15
|
+
# Compared to earlier versions of this generator, there is very limited use of
|
|
16
|
+
# stubs and message expectations in this spec. Stubs are only used when there
|
|
17
|
+
# is no simpler way to get a handle on the object needed for the example.
|
|
18
|
+
# Message expectations are only used when there is no simpler way to specify
|
|
19
|
+
# that an instance is receiving a specific message.
|
|
20
|
+
|
|
21
|
+
RSpec.describe UsersController, type: :controller do
|
|
22
|
+
|
|
23
|
+
# This should return the minimal set of attributes required to create a valid
|
|
24
|
+
# model. As you add validations to model, be sure to
|
|
25
|
+
# adjust the attributes here as well.
|
|
26
|
+
let(:valid_attributes) {
|
|
27
|
+
{email: 'tester@baseball-bridge.org', lang_id: Lang.find_or_create_by(code: :ja).id}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let(:invalid_attributes) {
|
|
31
|
+
{lang_id: nil}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
# This should return the minimal set of values that should be in the session
|
|
35
|
+
# in order to pass any filters (e.g. authentication) defined in
|
|
36
|
+
# Controller. Be sure to keep this updated too.
|
|
37
|
+
let(:valid_user){ User.create!(valid_attributes) }
|
|
38
|
+
let(:valid_user2){ User.create!(valid_attributes) }
|
|
39
|
+
|
|
40
|
+
let(:valid_session) { {user_id: valid_user.id} }
|
|
41
|
+
|
|
42
|
+
let(:logout_session) { valid_session.merge(user_id: nil) }
|
|
43
|
+
|
|
44
|
+
let(:login_session) { valid_session }
|
|
45
|
+
|
|
46
|
+
let(:model) { User }
|
|
47
|
+
|
|
48
|
+
let(:new_attributes) {
|
|
49
|
+
valid_attributes.merge(email: 'tester2@baseball-bridge.org')
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
def index_path
|
|
53
|
+
users_path
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def show_path(params)
|
|
57
|
+
user_path(params)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def edit_path(params)
|
|
61
|
+
edit_user_path(params)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def new_path
|
|
65
|
+
new_user_path
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
describe "GET #index" do
|
|
69
|
+
it "assigns all records as @records" do
|
|
70
|
+
record = model.create! valid_attributes
|
|
71
|
+
get :index, {}, logout_session
|
|
72
|
+
expect(assigns(:records)).to eq([record,valid_user])
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
describe "GET #show" do
|
|
77
|
+
it "assigns the requested record as @record" do
|
|
78
|
+
record = model.create! valid_attributes
|
|
79
|
+
get :show, {:id => record.id}, valid_session
|
|
80
|
+
expect(assigns(:record)).to eq(record)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
describe "GET #new" do
|
|
85
|
+
it "no routing" do
|
|
86
|
+
expect{
|
|
87
|
+
get :new, {}, valid_session
|
|
88
|
+
}.to raise_error(ActionController::UrlGenerationError)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
describe "user is self required" do
|
|
93
|
+
let!(:record){ valid_user }
|
|
94
|
+
|
|
95
|
+
before do
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
describe "GET #edit" do
|
|
99
|
+
it "assigns the requested record as @record" do
|
|
100
|
+
get :edit, {:id => record.id}, login_session
|
|
101
|
+
expect(assigns(:record)).to eq(record)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
it "another user" do
|
|
105
|
+
get :edit, {:id => record.id}, {user_id: valid_user2.id}
|
|
106
|
+
expect(response.status).to eq(403)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
it "not logged in" do
|
|
110
|
+
get :edit, {:id => record.id}, logout_session
|
|
111
|
+
expect(response.status).to eq(401)
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
describe "PUT #update" do
|
|
116
|
+
context "with valid params" do
|
|
117
|
+
it "updates the requested record" do
|
|
118
|
+
put :update, {:id => record.id, model.table_name => new_attributes}, login_session
|
|
119
|
+
record.reload
|
|
120
|
+
new_attributes.each do |k,v|
|
|
121
|
+
expect(record[k].to_s).to eq(v.to_s)
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
it "assigns the requested record as @record" do
|
|
126
|
+
put :update, {:id => record.id, model.table_name => valid_attributes}, login_session
|
|
127
|
+
expect(assigns(:record)).to eq(record)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
it "redirects to the record" do
|
|
131
|
+
put :update, {:id => record.id, model.table_name => valid_attributes}, login_session
|
|
132
|
+
expect(response).to redirect_to(record)
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
context "with invalid params" do
|
|
137
|
+
it "assigns the record as @record" do
|
|
138
|
+
put :update, {:id => record.id, model.table_name => invalid_attributes}, login_session
|
|
139
|
+
expect(response).to redirect_to(edit_path(id: record.id))
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
it "re-renders the 'edit' template" do
|
|
143
|
+
put :update, {:id => record.id, model.table_name => invalid_attributes}, login_session
|
|
144
|
+
expect(response).to redirect_to(edit_path(id: record.id))
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
it "another user" do
|
|
149
|
+
put :update, {:id => record.id}, {user_id: valid_user2.id}
|
|
150
|
+
expect(response.status).to eq(403)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
it "not logged in" do
|
|
154
|
+
put :update, {:id => record.id}, logout_session
|
|
155
|
+
expect(response.status).to eq(401)
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
describe "DELETE #destroy" do
|
|
160
|
+
it "destroys the requested record" do
|
|
161
|
+
expect {
|
|
162
|
+
delete :destroy, {:id => record.id}, login_session
|
|
163
|
+
}.to change(model, :count).by(-1)
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
it "destroys the requested record redirect" do
|
|
167
|
+
delete :destroy, {:id => record.id}, login_session
|
|
168
|
+
expect(response).to redirect_to(index_path)
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
it "another user" do
|
|
172
|
+
delete :destroy, {:id => record.id}, {user_id: valid_user2.id}
|
|
173
|
+
expect(response.status).to eq(403)
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
it "not logged in" do
|
|
177
|
+
delete :destroy, {:id => record.id}, logout_session
|
|
178
|
+
expect(response.status).to eq(401)
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
end
|