facemock 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -112,7 +112,7 @@ for specified gem
112
112
  Facemock::FbGraph.on
113
113
  Facemock::Config.load_users("./test_users.yml")
114
114
 
115
- yaml file see belo.
115
+ yaml file see below.
116
116
 
117
117
  ---
118
118
  - :app_id: '000000000000001'
@@ -134,6 +134,22 @@ yaml file see belo.
134
134
  :email: test_user_three@example.com
135
135
  :password: testpass
136
136
 
137
+ ### AuthHash
138
+
139
+ require 'facemock'
140
+
141
+ app = Facemock::Database::Application.create!
142
+ user = Facemock::Database::User.craete!(application_id: app.id)
143
+ auth_hash = Facemock.auth_hash(user.access_token)
144
+
145
+ # auth_hash == { "provider" => "facebook",
146
+ # "uid" => 100007315962084,
147
+ # "info" => { "name" => "c6fyxii0u2" },
148
+ # "credentials" => { "token" => "d4a88140f1...",
149
+ # "expires_at" => 2014-11-02 09:09:51 +0900 },
150
+ # "extra" => { "raw_info" => { "id" => 100007315962084,
151
+ # "name" => "c6fyxii0u2" } } }
152
+
137
153
  ### Exception
138
154
 
139
155
  require 'facemock'
data/facemock.gemspec CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_dependency "sqlite3"
22
22
  spec.add_dependency "fb_graph"
23
+ spec.add_dependency "omniauth"
23
24
  spec.add_dependency "hashie"
24
25
 
25
26
  spec.add_development_dependency "bundler", "~> 1.3"
data/lib/facemock.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  require "facemock/version"
2
- require "facemock/fb_graph"
3
2
  require "facemock/config"
3
+ require "facemock/fb_graph"
4
+ require "facemock/database"
5
+ require "facemock/errors"
6
+ require "facemock/auth_hash"
4
7
 
5
8
  module Facemock
6
9
  extend self
@@ -16,4 +19,23 @@ module Facemock
16
19
  def on?
17
20
  FbGraph == Facemock::FbGraph
18
21
  end
22
+
23
+ def auth_hash(access_token=nil)
24
+ if access_token.kind_of?(String) && access_token.size > 0
25
+ user = Facemock::Database::User.find_by_access_token(access_token)
26
+ if user
27
+ Facemock::AuthHash.new({
28
+ provider: "facebook",
29
+ uid: user.id,
30
+ info: { name: user.name },
31
+ credentials: { token: access_token, expires_at: Time.now + 60.days },
32
+ extra: { raw_info: { id: user.id, name: user.name } }
33
+ })
34
+ else
35
+ Facemock::AuthHash.new
36
+ end
37
+ else
38
+ Facemock::AuthHash.new
39
+ end
40
+ end
19
41
  end
@@ -0,0 +1,8 @@
1
+ require 'omniauth/auth_hash'
2
+ require 'facemock/database'
3
+ require 'hashie'
4
+
5
+ module Facemock
6
+ class AuthHash < OmniAuth::AuthHash
7
+ end
8
+ end
@@ -38,10 +38,11 @@ module Facemock
38
38
  raise Facemock::Errors::IncorrectDataFormat.new "users format is incorrect" unless validate_users(users)
39
39
 
40
40
  # Create application and user record
41
- Facemock::Database::Application.new({ id: app_id, secret: app_secret }).save!
42
- app = Facemock::FbGraph::Application.new(app_id, secret: app_secret)
41
+ app = Facemock::Database::Application.create!({ id: app_id, secret: app_secret })
43
42
  users.each do |options|
44
- app.test_user!(options)
43
+ user = Facemock::Database::User.new(options)
44
+ user.application_id = app.id
45
+ user.save!
45
46
  end
46
47
  end
47
48
  end
@@ -3,13 +3,14 @@ require 'facemock/database/table'
3
3
  require 'facemock/database/application'
4
4
  require 'facemock/database/user'
5
5
  require 'facemock/database/permission'
6
+ require 'facemock/database/authorization_code'
6
7
 
7
8
  module Facemock
8
9
  class Database
9
10
  ADAPTER = "sqlite3"
10
11
  DB_DIRECTORY = File.expand_path("../../../db", __FILE__)
11
12
  DEFAULT_DB_NAME = "facemock"
12
- TABLE_NAMES = [:applications, :users, :permissions]
13
+ TABLE_NAMES = [:applications, :users, :permissions, :authorization_codes]
13
14
 
14
15
  attr_reader :name
15
16
  attr_reader :connection
@@ -117,5 +118,16 @@ module Facemock
117
118
  );
118
119
  SQL
119
120
  end
121
+
122
+ def create_authorization_codes_table
123
+ @connection.execute <<-SQL
124
+ CREATE TABLE authorization_codes (
125
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
126
+ string TEXT NOT NULL,
127
+ user_id INTEGER NOT NULL,
128
+ created_at DATETIME NOT NULL
129
+ );
130
+ SQL
131
+ end
120
132
  end
121
133
  end
@@ -1,13 +1,12 @@
1
- require 'facemock/database'
2
1
  require 'facemock/database/table'
3
- require 'sqlite3'
4
- require 'hashie'
2
+ require 'facemock/database/user'
5
3
 
6
4
  module Facemock
7
5
  class Database
8
6
  class Application < Table
9
7
  TABLE_NAME = :applications
10
8
  COLUMN_NAMES = [:id, :secret, :created_at]
9
+ CHILDREN = [ User ]
11
10
 
12
11
  # WANT : DBに登録済みの値と重複しないようにする(id, secret)
13
12
  def initialize(options={})
@@ -0,0 +1,18 @@
1
+ require 'facemock/database/table'
2
+
3
+ module Facemock
4
+ class Database
5
+ class AuthorizationCode < Table
6
+ TABLE_NAME = :authorization_codes
7
+ COLUMN_NAMES = [:id, :string, :user_id, :created_at]
8
+
9
+ def initialize(options={})
10
+ opts = Hashie::Mash.new(options)
11
+ @id = opts.id
12
+ @string = opts.string || rand(36**255).to_s(36)
13
+ @user_id = opts.user_id
14
+ @created_at = opts.created_at
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,7 +1,4 @@
1
- require 'facemock/database'
2
1
  require 'facemock/database/table'
3
- require 'sqlite3'
4
- require 'hashie'
5
2
 
6
3
  module Facemock
7
4
  class Database
@@ -10,6 +10,7 @@ module Facemock
10
10
  # * initialize()
11
11
  TABLE_NAME = :tables
12
12
  COLUMN_NAMES = [:id, :text, :active, :number, :created_at]
13
+ CHILDREN = []
13
14
 
14
15
  def initialize(options={})
15
16
  opts = Hashie::Mash.new(options)
@@ -27,15 +28,18 @@ module Facemock
27
28
  def update_attributes!(options)
28
29
  # カラムに含まれるかどうかの確認。なければNoMethodError
29
30
  options.each_key {|key| self.send(key) }
30
- if persisted?
31
- update!(options)
32
- else
33
- insert!(options)
34
- end
31
+ persisted? ? update!(options) : insert!(options)
35
32
  end
36
33
 
37
34
  def destroy
38
35
  raise unless persisted?
36
+ self.class.children.each do |klass|
37
+ klass_last_name = self.class.name.split("::").last.downcase
38
+ find_method_name = "find_all_by_#{klass_last_name}_id"
39
+ objects = klass.send(find_method_name, self.id)
40
+ objects.each{|object| object.destroy }
41
+ end
42
+
39
43
  execute "DELETE FROM #{table_name} WHERE ID = #{self.id};"
40
44
  self
41
45
  end
@@ -70,6 +74,12 @@ module Facemock
70
74
  end
71
75
  end
72
76
 
77
+ def self.create!(options={})
78
+ instance = self.new(options)
79
+ instance.save!
80
+ instance
81
+ end
82
+
73
83
  def self.all
74
84
  records = execute "SELECT * FROM #{table_name};"
75
85
  records_to_objects(records)
@@ -95,15 +105,13 @@ module Facemock
95
105
  end
96
106
 
97
107
  def self.method_missing(name, *args)
98
- if name =~ /^find_by_(.+)/ || name =~ /^find_all_by_(.+)/
99
- column_name = $1
108
+ if ((name =~ /^find_by_(.+)/ || name =~ /^find_all_by_(.+)/) &&
109
+ (column_name = $1) && column_names.include?(column_name.to_sym))
110
+ raise ArgumentError, "wrong number of arguments (#{args.size} for 1)" unless args.size == 1
111
+ define_find_method(name, column_name) ? send(name, args.first) : super
100
112
  else
101
113
  super
102
114
  end
103
- super unless column_names.include?(column_name.to_sym)
104
- raise ArgumentError, "wrong number of arguments (#{args.size} for 1)" unless args.size == 1
105
- super unless define_find_method(name, column_name)
106
- send(name, args.first)
107
115
  end
108
116
 
109
117
  def table_name
@@ -126,6 +134,10 @@ module Facemock
126
134
  self::COLUMN_NAMES
127
135
  end
128
136
 
137
+ def self.children
138
+ self::CHILDREN
139
+ end
140
+
129
141
  def self.column_type(column_name)
130
142
  return nil unless column_names.include?(column_name.to_s.to_sym)
131
143
  table_info.send(column_name).type
@@ -1,13 +1,13 @@
1
- require 'facemock/database'
2
1
  require 'facemock/database/table'
3
- require 'sqlite3'
4
- require 'hashie'
2
+ require 'facemock/database/permission'
3
+ require 'facemock/database/authorization_code'
5
4
 
6
5
  module Facemock
7
6
  class Database
8
7
  class User < Table
9
8
  TABLE_NAME = :users
10
9
  COLUMN_NAMES = [:id, :name, :email, :password, :installed, :access_token, :application_id, :created_at]
10
+ CHILDREN = [ Permission, AuthorizationCode ]
11
11
 
12
12
  def initialize(options={})
13
13
  opts = Hashie::Mash.new(options)
@@ -3,5 +3,6 @@ module Facemock
3
3
  class Error < StandardError; end
4
4
  class IncorrectDataFormat < Error; end
5
5
  class ColumnTypeNotNull < Error; end
6
+ class InvalidToken < Error; end
6
7
  end
7
8
  end
@@ -44,9 +44,7 @@ module Facemock
44
44
  def test_user!(options={})
45
45
  validate_and_raise_error
46
46
  options.merge!({application_id: self.identifier})
47
- user = User.new(options)
48
- user.save!
49
- user
47
+ user = User.create!(options)
50
48
  end
51
49
 
52
50
  def test_users(options={})
@@ -38,10 +38,8 @@ module Facemock
38
38
  end
39
39
 
40
40
  def destroy
41
- @permission_objects.each do |permission|
42
- permission.destroy
43
- end
44
41
  super
42
+ @permission_objects = []
45
43
  end
46
44
 
47
45
  def revoke!
@@ -1,3 +1,3 @@
1
1
  module Facemock
2
- VERSION = '0.0.6'
2
+ VERSION = '0.0.7'
3
3
  end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Facemock::AuthHash do
4
+ it 'should inherit a OmniAuth::AuthHash class' do
5
+ expect(Facemock::AuthHash.ancestors).to include OmniAuth::AuthHash
6
+ end
7
+ end
@@ -5,10 +5,6 @@ describe Facemock::Config do
5
5
  let(:db_name) { ".test" }
6
6
  let(:ymlfile) { "testdata.yml" }
7
7
 
8
- it 'should have a database module' do
9
- expect(Facemock::Database).to be_truthy
10
- end
11
-
12
8
  before { stub_const("Facemock::Database::DEFAULT_DB_NAME", db_name) }
13
9
 
14
10
  describe '#default_database' do
@@ -1,13 +1,20 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Facemock::Database::Application do
4
- let(:table_name) { :applications }
5
- let(:column_names) { [:id, :secret, :created_at] }
4
+ include TableHelper
6
5
 
7
- let(:id) { 1 }
8
- let(:secret) { "test_secret" }
9
- let(:created_at) { Time.now }
10
- let(:options) { { id: id, secret: secret, created_at: created_at } }
6
+ let(:db_name) { ".test" }
7
+
8
+ let(:table_name) { :applications }
9
+ let(:column_names) { [ :id, :secret, :created_at ] }
10
+ let(:children) { [ Facemock::Database::User ] }
11
+
12
+ let(:id) { 1 }
13
+ let(:secret) { "test_secret" }
14
+ let(:created_at) { Time.now }
15
+ let(:options) { { id: id, secret: secret, created_at: created_at } }
16
+
17
+ after { remove_dynamically_defined_all_method }
11
18
 
12
19
  describe '::TABLE_NAME' do
13
20
  subject { Facemock::Database::Application::TABLE_NAME }
@@ -19,6 +26,11 @@ describe Facemock::Database::Application do
19
26
  it { is_expected.to eq column_names }
20
27
  end
21
28
 
29
+ describe '::CHILDREN' do
30
+ subject { Facemock::Database::Application::CHILDREN }
31
+ it { is_expected.to eq children }
32
+ end
33
+
22
34
  describe '#initialize' do
23
35
  context 'without option' do
24
36
  subject { Facemock::Database::Application.new }
@@ -70,4 +82,25 @@ describe Facemock::Database::Application do
70
82
  end
71
83
  end
72
84
  end
85
+
86
+ describe 'destroy' do
87
+ before do
88
+ stub_const("Facemock::Database::DEFAULT_DB_NAME", db_name)
89
+ @database = Facemock::Database.new
90
+ end
91
+ after { @database.drop }
92
+
93
+ context 'when has user' do
94
+ before do
95
+ @application = Facemock::Database::Application.create!
96
+ Facemock::Database::User.create!(application_id: @application.id)
97
+ end
98
+
99
+ it 'should delete permissions' do
100
+ @application.destroy
101
+ users = Facemock::Database::User.find_all_by_application_id(@application.id)
102
+ expect(users).to be_empty
103
+ end
104
+ end
105
+ end
73
106
  end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ describe Facemock::Database::AuthorizationCode do
4
+ include TableHelper
5
+
6
+ let(:table_name) { :authorization_codes }
7
+ let(:column_names) { [ :id, :string, :user_id, :created_at ] }
8
+ let(:children) { [] }
9
+
10
+ let(:id) { 1 }
11
+ let(:string) { "test_code" }
12
+ let(:user_id) { 1 }
13
+ let(:created_at) { Time.now }
14
+ let(:options) { { id: id, string: string, user_id: user_id, created_at: created_at } }
15
+
16
+ after { remove_dynamically_defined_all_method }
17
+
18
+ describe '::TABLE_NAME' do
19
+ subject { Facemock::Database::AuthorizationCode::TABLE_NAME }
20
+ it { is_expected.to eq table_name }
21
+ end
22
+
23
+ describe '::COLUMN_NAMES' do
24
+ subject { Facemock::Database::AuthorizationCode::COLUMN_NAMES }
25
+ it { is_expected.to eq column_names }
26
+ end
27
+
28
+ describe '::CHILDREN' do
29
+ subject { Facemock::Database::AuthorizationCode::CHILDREN }
30
+ it { is_expected.to eq children }
31
+ end
32
+
33
+ describe '#initialize' do
34
+ context 'without option' do
35
+ subject { Facemock::Database::AuthorizationCode.new }
36
+ it { is_expected.to be_kind_of Facemock::Database::AuthorizationCode }
37
+
38
+ context 'then attributes' do
39
+ it 'should be nil except string' do
40
+ column_names.each do |column_name|
41
+ value = Facemock::Database::AuthorizationCode.new.send(column_name)
42
+ if column_name == :string
43
+ expect(value).not_to be_nil
44
+ else
45
+ expect(value).to be_nil
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ context 'then string' do
52
+ it 'should be random string' do
53
+ string1 = Facemock::Database::AuthorizationCode.new.string
54
+ string2 = Facemock::Database::AuthorizationCode.new.string
55
+ expect(string1).to be_kind_of String
56
+ expect(string1.size).to be < 256
57
+ expect(string1).not_to eq string2
58
+ end
59
+ end
60
+ end
61
+
62
+ context 'with all options' do
63
+ subject { Facemock::Database::AuthorizationCode.new(options) }
64
+ it { is_expected.to be_kind_of Facemock::Database::AuthorizationCode }
65
+
66
+ context 'then attributes' do
67
+ it 'should set specified value by option' do
68
+ column_names.each do |column_name|
69
+ value = Facemock::Database::AuthorizationCode.new(options).send(column_name)
70
+ expect(value).to eq options[column_name]
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -1,14 +1,19 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Facemock::Database::Permission do
4
- let(:table_name) { :permissions }
5
- let(:column_names) { [ :id, :name, :user_id, :created_at ] }
4
+ include TableHelper
6
5
 
7
- let(:id) { 1 }
8
- let(:name) { "read_stream" }
9
- let(:user_id) { 1 }
10
- let(:created_at) { Time.now }
11
- let(:options) { { id: id, name: name, user_id: user_id, created_at: created_at } }
6
+ let(:table_name) { :permissions }
7
+ let(:column_names) { [ :id, :name, :user_id, :created_at ] }
8
+ let(:children) { [] }
9
+
10
+ let(:id) { 1 }
11
+ let(:name) { "read_stream" }
12
+ let(:user_id) { 1 }
13
+ let(:created_at) { Time.now }
14
+ let(:options) { { id: id, name: name, user_id: user_id, created_at: created_at } }
15
+
16
+ after { remove_dynamically_defined_all_method }
12
17
 
13
18
  describe '::TABLE_NAME' do
14
19
  subject { Facemock::Database::Permission::TABLE_NAME }
@@ -20,6 +25,11 @@ describe Facemock::Database::Permission do
20
25
  it { is_expected.to eq column_names }
21
26
  end
22
27
 
28
+ describe '::CHILDREN' do
29
+ subject { Facemock::Database::Permission::CHILDREN }
30
+ it { is_expected.to eq children }
31
+ end
32
+
23
33
  describe '#initialize' do
24
34
  context 'without option' do
25
35
  subject { Facemock::Database::Permission.new }
@@ -3,9 +3,10 @@ require 'spec_helper'
3
3
  describe Facemock::Database::Table do
4
4
  include TableHelper
5
5
 
6
- let(:db_name) { ".test" }
7
- let(:table_name) { :tables }
8
- let(:column_names) { [:id, :text, :active, :number, :created_at] }
6
+ let(:db_name) { ".test" }
7
+ let(:table_name) { :tables }
8
+ let(:column_names) { [ :id, :text, :active, :number, :created_at ] }
9
+ let(:children) { [] }
9
10
 
10
11
  before do
11
12
  stub_const("Facemock::Database::DEFAULT_DB_NAME", db_name)
@@ -14,8 +15,7 @@ describe Facemock::Database::Table do
14
15
 
15
16
  after do
16
17
  Facemock::Database.new.drop
17
- remove_dynamically_defined_class_method(Facemock::Database::Table)
18
- remove_dynamically_defined_instance_method(Facemock::Database::Table)
18
+ remove_dynamically_defined_all_method
19
19
  end
20
20
 
21
21
  describe '::TABLE_NAME' do
@@ -28,6 +28,11 @@ describe Facemock::Database::Table do
28
28
  it { is_expected.to eq column_names }
29
29
  end
30
30
 
31
+ describe '::CHILDREN' do
32
+ subject { Facemock::Database::Table::CHILDREN }
33
+ it { is_expected.to eq children }
34
+ end
35
+
31
36
  describe '#initialize' do
32
37
  context 'without option' do
33
38
  it 'should have accessor of column' do
@@ -185,6 +190,32 @@ describe Facemock::Database::Table do
185
190
  end
186
191
  end
187
192
 
193
+ describe '#create!' do
194
+ context 'without option' do
195
+ subject { lambda { Facemock::Database::Table.create! } }
196
+ it { is_expected.to raise_error Facemock::Errors::ColumnTypeNotNull }
197
+ end
198
+
199
+ context 'with options that cloumns are not null' do
200
+ before do
201
+ @options = { id: 1, text: "test", active: true, number: 0, created_at: Time.now }
202
+ end
203
+
204
+ it 'should new and save and return saved object' do
205
+ table = Facemock::Database::Table.create!(@options)
206
+ expect(table).to be_kind_of(Facemock::Database::Table)
207
+ column_names.each do |column_name|
208
+ value = table.send(column_name)
209
+ if column_name == :created_at
210
+ expect(value).to be_kind_of Time
211
+ else
212
+ expect(value).to eq @options[column_name]
213
+ end
214
+ end
215
+ end
216
+ end
217
+ end
218
+
188
219
  describe '#update_attributes!' do
189
220
  before { @table = Facemock::Database::Table.new({text: "test", number: 1}) }
190
221
 
@@ -3,16 +3,18 @@ require 'spec_helper'
3
3
  describe Facemock::Database::User do
4
4
  include TableHelper
5
5
 
6
- let(:db_name) { ".test" }
7
- let(:table_name) { :users }
8
- let(:column_names) { [ :id,
9
- :name,
10
- :email,
11
- :password,
12
- :installed,
13
- :access_token,
14
- :application_id,
15
- :created_at] }
6
+ let(:db_name) { ".test" }
7
+ let(:table_name) { :users }
8
+ let(:column_names) { [ :id,
9
+ :name,
10
+ :email,
11
+ :password,
12
+ :installed,
13
+ :access_token,
14
+ :application_id,
15
+ :created_at ] }
16
+ let(:children) { [ Facemock::Database::Permission,
17
+ Facemock::Database::AuthorizationCode ] }
16
18
 
17
19
  let(:id) { 1 }
18
20
  let(:name) { "test user" }
@@ -21,7 +23,7 @@ describe Facemock::Database::User do
21
23
  let(:installed) { true }
22
24
  let(:access_token) { "test_token" }
23
25
  let(:application_id) { 1 }
24
- let(:created_at) { Time.now }
26
+ let(:created_at) { Time.now }
25
27
  let(:options) { { id: id,
26
28
  name: name,
27
29
  email: email,
@@ -31,10 +33,7 @@ describe Facemock::Database::User do
31
33
  application_id: application_id,
32
34
  created_at: created_at } }
33
35
 
34
- after do
35
- remove_dynamically_defined_class_method(Facemock::Database::User)
36
- remove_dynamically_defined_instance_method(Facemock::Database::User)
37
- end
36
+ after { remove_dynamically_defined_all_method }
38
37
 
39
38
  describe '::TABLE_NAME' do
40
39
  subject { Facemock::Database::User::TABLE_NAME }
@@ -46,6 +45,11 @@ describe Facemock::Database::User do
46
45
  it { is_expected.to eq column_names }
47
46
  end
48
47
 
48
+ describe '::CHILDREN' do
49
+ subject { Facemock::Database::User::CHILDREN }
50
+ it { is_expected.to eq children }
51
+ end
52
+
49
53
  describe '#initialize' do
50
54
  context 'without option' do
51
55
  subject { Facemock::Database::User.new }
@@ -63,7 +67,7 @@ describe Facemock::Database::User do
63
67
 
64
68
  describe '.size' do
65
69
  subject { Facemock::Database::User.new.name.size }
66
- it { is_expected.to eq 10 }
70
+ it { is_expected.to be <= 10 }
67
71
  end
68
72
  end
69
73
 
@@ -166,4 +170,31 @@ describe Facemock::Database::User do
166
170
  end
167
171
  end
168
172
  end
173
+
174
+ describe 'destroy' do
175
+ before do
176
+ stub_const("Facemock::Database::DEFAULT_DB_NAME", db_name)
177
+ @database = Facemock::Database.new
178
+ end
179
+ after { @database.drop }
180
+
181
+ context 'when has permission and authorizaion_code' do
182
+ before do
183
+ application = Facemock::Database::Application.create!
184
+ @user = Facemock::Database::User.create!(application_id: application.id)
185
+ permission = Facemock::Database::Permission.new(name: "email", user_id: @user.id)
186
+ permission.save!
187
+ authorization_code = Facemock::Database::AuthorizationCode.new(user_id: @user.id)
188
+ authorization_code.save!
189
+ end
190
+
191
+ it 'should delete permissions' do
192
+ @user.destroy
193
+ permissions = Facemock::Database::Permission.find_all_by_user_id(@user.id)
194
+ expect(permissions).to be_empty
195
+ authorization_codes = Facemock::Database::AuthorizationCode.find_all_by_user_id(@user.id)
196
+ expect(authorization_codes).to be_empty
197
+ end
198
+ end
199
+ end
169
200
  end
@@ -4,7 +4,7 @@ describe Facemock::Database do
4
4
  let(:db_name) { ".test" }
5
5
  let(:default_db_name) { "facemock" }
6
6
  let(:adapter) { "sqlite3" }
7
- let(:table_names) { [:applications, :users, :permissions] }
7
+ let(:table_names) { [:applications, :users, :permissions, :authorization_codes] }
8
8
  let(:db_directory) { File.expand_path("../../../db", __FILE__) }
9
9
  let(:db_filepath) { File.join(db_directory, "#{db_name}.#{adapter}") }
10
10
 
@@ -28,22 +28,6 @@ describe Facemock::Database do
28
28
  it { is_expected.to eq default_db_name }
29
29
  end
30
30
 
31
- it 'should have a table class' do
32
- expect(Facemock::Database::Table).to be_truthy
33
- end
34
-
35
- it 'should have a application class' do
36
- expect(Facemock::Database::Application).to be_truthy
37
- end
38
-
39
- it 'should have a user class' do
40
- expect(Facemock::Database::User).to be_truthy
41
- end
42
-
43
- it 'should have a permission class' do
44
- expect(Facemock::Database::Permission).to be_truthy
45
- end
46
-
47
31
  describe '#initialize' do
48
32
  before do
49
33
  allow_any_instance_of(Facemock::Database).to receive(:connect) { true }
@@ -265,6 +249,13 @@ describe Facemock::Database do
265
249
  end
266
250
 
267
251
  context 'when drop tables' do
252
+ before { @database.drop_tables }
253
+
254
+ it 'should not exist any tables' do
255
+ table_names.each do |table_name|
256
+ expect(@database.table_exists?(table_name)).to eq false
257
+ end
258
+ end
268
259
  end
269
260
  end
270
261
  end
@@ -43,8 +43,7 @@ describe Facemock::FbGraph::Application::TestUsers do
43
43
 
44
44
  context 'when user exist only one' do
45
45
  before do
46
- @user = Facemock::FbGraph::Application::User.new(application_id: facebook_app_id)
47
- @user.save!
46
+ @user = Facemock::FbGraph::Application::User.create!(application_id: facebook_app_id)
48
47
  end
49
48
 
50
49
  it 'should have user' do
@@ -64,7 +63,7 @@ describe Facemock::FbGraph::Application::TestUsers do
64
63
  context 'when user is exist only two' do
65
64
  before do
66
65
  2.times do
67
- Facemock::FbGraph::Application::User.new(application_id: facebook_app_id).save!
66
+ Facemock::FbGraph::Application::User.create!(application_id: facebook_app_id)
68
67
  end
69
68
  @limit = 1
70
69
  end
@@ -82,7 +81,7 @@ describe Facemock::FbGraph::Application::TestUsers do
82
81
  context 'when user is exist only two' do
83
82
  before do
84
83
  2.times do
85
- Facemock::FbGraph::Application::User.new(application_id: facebook_app_id).save!
84
+ Facemock::FbGraph::Application::User.create!(application_id: facebook_app_id)
86
85
  end
87
86
  @after = 1
88
87
  end
@@ -100,7 +99,7 @@ describe Facemock::FbGraph::Application::TestUsers do
100
99
  context 'when user is exist only three' do
101
100
  before do
102
101
  3.times do
103
- Facemock::FbGraph::Application::User.new(application_id: facebook_app_id).save!
102
+ Facemock::FbGraph::Application::User.create!(application_id: facebook_app_id)
104
103
  end
105
104
  @options = { limit: 1, after: 1 }
106
105
  end
@@ -118,8 +117,7 @@ describe Facemock::FbGraph::Application::TestUsers do
118
117
 
119
118
  describe '#collection' do
120
119
  before do
121
- user = Facemock::FbGraph::Application::User.new(application_id: facebook_app_id)
122
- user.save!
120
+ user = Facemock::FbGraph::Application::User.create!(application_id: facebook_app_id)
123
121
  @test_users = Facemock::FbGraph::Application::TestUsers.new(facebook_app_id)
124
122
  end
125
123
 
@@ -17,11 +17,6 @@ describe Facemock::FbGraph::Application::User do
17
17
  before { stub_const("Facemock::Database::DEFAULT_DB_NAME", db_name) }
18
18
  after { Facemock::Database.new.drop }
19
19
 
20
- it 'should have a permission class' do
21
- expect(Facemock::FbGraph::Application::User::Permission).to be_truthy
22
- expect(Facemock::FbGraph::Application::User::Permission.ancestors).to include Facemock::Database::Permission
23
- end
24
-
25
20
  describe '#new' do
26
21
  context 'without options' do
27
22
  subject { lambda { Facemock::FbGraph::Application::User.new } }
@@ -146,8 +141,7 @@ describe Facemock::FbGraph::Application::User do
146
141
  describe '#destroy' do
147
142
  context 'when does not have permission' do
148
143
  before do
149
- @user = Facemock::FbGraph::Application::User.new(options)
150
- @user.save!
144
+ @user = Facemock::FbGraph::Application::User.create!(options)
151
145
  end
152
146
 
153
147
  subject { lambda { @user.destroy } }
@@ -163,8 +157,7 @@ describe Facemock::FbGraph::Application::User do
163
157
  context 'when have some permissions' do
164
158
  before do
165
159
  opts = { application_id: 1, permissions: "email, read_stream" }
166
- @user = Facemock::FbGraph::Application::User.new(opts)
167
- @user.save!
160
+ @user = Facemock::FbGraph::Application::User.create!(opts)
168
161
  end
169
162
 
170
163
  subject { lambda { @user.destroy } }
@@ -174,8 +167,8 @@ describe Facemock::FbGraph::Application::User do
174
167
  @user.destroy
175
168
  expect(Facemock::FbGraph::Application::User.find_by_id(@user.id)).to be_nil
176
169
  expect(Facemock::FbGraph::Application::User::Permission.find_all_by_user_id(@user.id)).to be_empty
177
- expect(@user.permissions).not_to be_empty
178
- expect(@user.permission_objects).not_to be_empty
170
+ expect(@user.permissions).to be_empty
171
+ expect(@user.permission_objects).to be_empty
179
172
  end
180
173
  end
181
174
  end
@@ -183,8 +176,7 @@ describe Facemock::FbGraph::Application::User do
183
176
  describe '#revoke!' do
184
177
  context 'when does not have permission' do
185
178
  before do
186
- @user = Facemock::FbGraph::Application::User.new(options)
187
- @user.save!
179
+ @user = Facemock::FbGraph::Application::User.create!(options)
188
180
  end
189
181
 
190
182
  subject { lambda { @user.revoke! } }
@@ -194,8 +186,7 @@ describe Facemock::FbGraph::Application::User do
194
186
  context 'when have some permissions' do
195
187
  before do
196
188
  opts = { application_id: 1, permissions: "email, read_stream" }
197
- @user = Facemock::FbGraph::Application::User.new(opts)
198
- @user.save!
189
+ @user = Facemock::FbGraph::Application::User.create!(opts)
199
190
  end
200
191
 
201
192
  it 'should destroy permissions' do
@@ -15,16 +15,6 @@ describe Facemock::FbGraph::Application do
15
15
  end
16
16
  after { @database.drop }
17
17
 
18
- it 'should have a user class' do
19
- expect(Facemock::FbGraph::Application::User).to be_truthy
20
- expect(Facemock::FbGraph::Application::User.ancestors).to include Facemock::Database::User
21
- end
22
-
23
- it 'should have a users class' do
24
- expect(Facemock::FbGraph::Application::TestUsers).to be_truthy
25
- expect(Facemock::FbGraph::Application::TestUsers.ancestors).to include Array
26
- end
27
-
28
18
  describe '#new' do
29
19
  before { @default_record_size = Facemock::Database::Application.all.size }
30
20
 
@@ -1,19 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Facemock::FbGraph do
4
- it 'should have a application class' do
5
- expect(Facemock::FbGraph::Application).to be_truthy
6
- end
7
-
8
- it 'should have a user module' do
9
- expect(Facemock::FbGraph::User).to be_truthy
10
- end
11
-
12
- it 'should have a error class' do
13
- expect(Facemock::FbGraph::InvalidToken).to be_truthy
14
- expect(Facemock::FbGraph::InvalidRequest).to be_truthy
15
- end
16
-
17
4
  describe '#on' do
18
5
  subject { Facemock::FbGraph.on }
19
6
  it { is_expected.to be_truthy }
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Facemock do
4
- let(:version) { '0.0.6' }
4
+ let(:version) { '0.0.7' }
5
5
  let(:db_name) { '.test' }
6
6
 
7
7
  describe 'VERSION' do
@@ -9,22 +9,6 @@ describe Facemock do
9
9
  it { is_expected.to eq version }
10
10
  end
11
11
 
12
- it 'should have a config module' do
13
- expect(Facemock::Config).to be_truthy
14
- end
15
-
16
- it 'should have a fb_graph module' do
17
- expect(Facemock::FbGraph).to be_truthy
18
- end
19
-
20
- it 'should have a database class' do
21
- expect(Facemock::Database).to be_truthy
22
- end
23
-
24
- it 'should have a errors module' do
25
- expect(Facemock::Errors).to be_truthy
26
- end
27
-
28
12
  describe '.on' do
29
13
  subject { Facemock.on }
30
14
  it { is_expected.to be_truthy }
@@ -71,4 +55,55 @@ describe Facemock do
71
55
  it { is_expected.to be true }
72
56
  end
73
57
  end
58
+
59
+ describe '.auth_hash' do
60
+ context 'withou argument' do
61
+ subject { Facemock.auth_hash }
62
+ it { is_expected.to be_kind_of Facemock::AuthHash }
63
+ it { is_expected.to be_empty }
64
+ end
65
+
66
+ context 'with incorrect argument' do
67
+ it 'should return empty hash' do
68
+ [nil, false, true, 1, ""].each do |argument|
69
+ value = Facemock.auth_hash(argument)
70
+ expect(value).to be_kind_of Facemock::AuthHash
71
+ expect(value).to be_empty
72
+ end
73
+ end
74
+ end
75
+
76
+ context 'with access_token' do
77
+ before do
78
+ stub_const("Facemock::Database::DEFAULT_DB_NAME", db_name)
79
+ @database = Facemock::Database.new
80
+ application = Facemock::Database::Application.create!
81
+ @user = Facemock::Database::User.create!(application_id: application.id)
82
+ @access_token = @user.access_token
83
+ end
84
+ after { @database.drop }
85
+
86
+ context 'that is incorrect' do
87
+ end
88
+
89
+ context 'that is correct' do
90
+ it 'should return AuthHash with some keys and value' do
91
+ auth_hash = Facemock.auth_hash(@access_token)
92
+ expect(auth_hash).to be_kind_of Facemock::AuthHash
93
+ expect(auth_hash).not_to be_empty
94
+ expect(auth_hash.provider).to eq "facebook"
95
+ expect(auth_hash.uid).to eq @user.id
96
+ [ auth_hash.info, auth_hash.credentials,
97
+ auth_hash.extra, auth_hash.extra.raw_info ].each do |value|
98
+ expect(value).to be_kind_of Hash
99
+ end
100
+ expect(auth_hash.info.name).to eq @user.name
101
+ expect(auth_hash.credentials.token).to eq @user.access_token
102
+ expect(auth_hash.credentials.expires_at).to be > Time.now
103
+ expect(auth_hash.extra.raw_info.id).to eq @user.id
104
+ expect(auth_hash.extra.raw_info.name).to eq @user.name
105
+ end
106
+ end
107
+ end
108
+ end
74
109
  end
@@ -1,5 +1,5 @@
1
1
  module ApplicationCreateHelper
2
2
  def create_application(options={})
3
- Facemock::Database::Application.new(options).save!
3
+ Facemock::Database::Application.create!(options)
4
4
  end
5
5
  end
@@ -1,4 +1,16 @@
1
1
  module TableHelper
2
+ def remove_dynamically_defined_all_method
3
+ klasses = [Facemock::Database::Table,
4
+ Facemock::Database::Application,
5
+ Facemock::Database::User,
6
+ Facemock::Database::Permission,
7
+ Facemock::Database::AuthorizationCode ]
8
+ klasses.each do |klass|
9
+ remove_dynamically_defined_class_method(klass)
10
+ remove_dynamically_defined_instance_method(klass)
11
+ end
12
+ end
13
+
2
14
  # テストで動的に定義したクラスメソッドを削除
3
15
  def remove_dynamically_defined_class_method(klass)
4
16
  klass.methods.each do |method_name|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: facemock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-08-28 00:00:00.000000000 Z
12
+ date: 2014-09-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sqlite3
@@ -43,6 +43,22 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: omniauth
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
46
62
  - !ruby/object:Gem::Dependency
47
63
  name: hashie
48
64
  requirement: !ruby/object:Gem::Requirement
@@ -174,9 +190,11 @@ files:
174
190
  - db/.gitkeep
175
191
  - facemock.gemspec
176
192
  - lib/facemock.rb
193
+ - lib/facemock/auth_hash.rb
177
194
  - lib/facemock/config.rb
178
195
  - lib/facemock/database.rb
179
196
  - lib/facemock/database/application.rb
197
+ - lib/facemock/database/authorization_code.rb
180
198
  - lib/facemock/database/permission.rb
181
199
  - lib/facemock/database/table.rb
182
200
  - lib/facemock/database/user.rb
@@ -188,10 +206,12 @@ files:
188
206
  - lib/facemock/fb_graph/application/user/permission.rb
189
207
  - lib/facemock/fb_graph/user.rb
190
208
  - lib/facemock/version.rb
209
+ - spec/facemock/auth_hash_spec.rb
191
210
  - spec/facemock/config_spec.rb
192
211
  - spec/facemock/database/application_spec.rb
212
+ - spec/facemock/database/authorization_code.rb
193
213
  - spec/facemock/database/permission_spec.rb
194
- - spec/facemock/database/tables_spec.rb
214
+ - spec/facemock/database/table_spec.rb
195
215
  - spec/facemock/database/user_spec.rb
196
216
  - spec/facemock/database_spec.rb
197
217
  - spec/facemock/errors_spec.rb
@@ -219,7 +239,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
219
239
  version: '0'
220
240
  segments:
221
241
  - 0
222
- hash: -2374315487424674791
242
+ hash: -2537604812428127045
223
243
  required_rubygems_version: !ruby/object:Gem::Requirement
224
244
  none: false
225
245
  requirements:
@@ -228,7 +248,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
228
248
  version: '0'
229
249
  segments:
230
250
  - 0
231
- hash: -2374315487424674791
251
+ hash: -2537604812428127045
232
252
  requirements: []
233
253
  rubyforge_project:
234
254
  rubygems_version: 1.8.25
@@ -236,10 +256,12 @@ signing_key:
236
256
  specification_version: 3
237
257
  summary: This is facebook mock application for fb_graph.
238
258
  test_files:
259
+ - spec/facemock/auth_hash_spec.rb
239
260
  - spec/facemock/config_spec.rb
240
261
  - spec/facemock/database/application_spec.rb
262
+ - spec/facemock/database/authorization_code.rb
241
263
  - spec/facemock/database/permission_spec.rb
242
- - spec/facemock/database/tables_spec.rb
264
+ - spec/facemock/database/table_spec.rb
243
265
  - spec/facemock/database/user_spec.rb
244
266
  - spec/facemock/database_spec.rb
245
267
  - spec/facemock/errors_spec.rb