facemock 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ tmp/
19
+ db/*.sqlite3
20
+ vendor/bundle/
21
+ vendor/bundler/
22
+ log/*.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - 2.1.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in facemock.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 ogawa
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,154 @@
1
+ [![Gem Version](https://badge.fury.io/rb/facemock.svg)](http://badge.fury.io/rb/facemock)
2
+ [![Build Status](https://travis-ci.org/ogawatti/facemock.svg?branch=master)](https://travis-ci.org/ogawatti/facemock)
3
+ [![Coverage Status](https://coveralls.io/repos/ogawatti/facemock/badge.png?branch=master)](https://coveralls.io/r/ogawatti/facemock?branch=master)
4
+ [<img src="https://gemnasium.com/ogawatti/facemock.png" />](https://gemnasium.com/ogawatti/facemock)
5
+ [![Code Climate](https://codeclimate.com/github/ogawatti/facemock.png)](https://codeclimate.com/github/ogawatti/facemock)
6
+
7
+ # Facemock
8
+
9
+ Facemock is facebook mock application for FbGraph.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'facemock'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install facemock
24
+
25
+ ## Usage
26
+
27
+ ### Mock on/off
28
+
29
+ for all gem
30
+
31
+ require 'facemock'
32
+
33
+ FbGraph #=> FbGraph
34
+ Facemock.on
35
+ FbGraph #=> Facemock::FbGraph
36
+ Facemock.off
37
+ FbGraph #=> FbGraph
38
+
39
+ for specified gem
40
+
41
+ require 'facemock'
42
+
43
+ FbGraph #=> FbGraph
44
+ Facemock::FbGraph.on
45
+ FbGraph #=> Facemock::FbGraph
46
+ Facemock::FbGraph.off
47
+ FbGraph #=> FbGraph
48
+
49
+ ### Test User
50
+
51
+ require 'facemock'
52
+
53
+ Facemock.on
54
+ facebook_app_id = "100000000000000"
55
+ facebook_app_secret = "facemock_app_secret"
56
+ app = FbGraph::Application.new(facebook_app_id, secret: facebook_app_secret)
57
+
58
+
59
+ ## Create Test User
60
+ user = app.test_user!
61
+ user = app.test_user!( { name: "test user", permissions: "email, read_stream" } )
62
+ user.name #=> "test user"
63
+ user.permissions #=> [:email, :read_stream]
64
+
65
+
66
+ ## Get Created Test User
67
+ app.test_users #=> [#<Facemock::FbGraph::Application::User id: ...>, ...]
68
+ app.test_users.size #=> 2
69
+ test_users.first #=> User that was created at the last
70
+ test_users.last #=> User that was created at the first
71
+
72
+ test_users = app.test_users({ limit: 1, after: 1 })
73
+ test_users.size = 1
74
+ test_users.first.id #=> [#<Facemock::FbGraph::Application::User id: ...>, ...]
75
+
76
+
77
+ # Delete Test User
78
+ app.test_users.size #=> 2
79
+ app.test_users.first.destroy
80
+ app.test_users.size #=> 1
81
+ app.test_users.first.destroy
82
+ app.test_users #=> []
83
+
84
+ ### User
85
+
86
+ require 'facemock'
87
+
88
+ Facemock.on
89
+ facebook_app_id = "100000000000000"
90
+ facebook_app_secret = "facemock_app_secret"
91
+ app = FbGraph::Application.new(facebook_app_id, secret: facebook_app_secret)
92
+ user = app.test_user!({name: "face mock", permissions: "email, read_stream"})
93
+ access_token = user.access_token
94
+
95
+ # Get User by Access Token
96
+ user = FbGraph::User.me(access_token)
97
+ user.name #=> "face mock"
98
+ user.permissions #=> [:email, :read_stream]
99
+
100
+ # Delete permission
101
+ user.revoke!
102
+ user.permissions #=> []
103
+
104
+ # Delete User
105
+ user.destroy
106
+ FbGraph::User.me(access_token) #=> nil
107
+
108
+ ### Register Test User by yaml file
109
+
110
+ require 'facemock'
111
+
112
+ Facemock::FbGraph.on
113
+ Facemock::Config.load_users("./test_users.yml")
114
+
115
+ yaml file see belo.
116
+
117
+ ---
118
+ - :app_id: '000000000000001'
119
+ :app_secret: test_secret_one
120
+ :users:
121
+ - :identifier: 100000000000001
122
+ :name: test user one
123
+ :email: test_user_one@example.com
124
+ :password: testpass
125
+ - :identifier: '100000000000002'
126
+ :name: test user two
127
+ :email: test_user_two@example.com
128
+ :password: testpass
129
+ - :app_id: '000000000000002'
130
+ :app_secret: test_secret_two
131
+ :users:
132
+ - :identifier: 100000000000003
133
+ :name: test user three
134
+ :email: test_user_three@example.com
135
+ :password: testpass
136
+
137
+ ### Exception
138
+
139
+ require 'facemock'
140
+
141
+ Facemock.on
142
+ begin
143
+ raise FbGraph::Errors::InvalidToken.new "test exception"
144
+ rescue => e
145
+ puts "#{e.class} : #{e.message}" #=> Facemock::FbGraph::Errors::InvalidToken : test exception
146
+ end
147
+
148
+ ## Contributing
149
+
150
+ 1. Fork it
151
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
152
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
153
+ 4. Push to the branch (`git push origin my-new-feature`)
154
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/db/.gitkeep ADDED
File without changes
data/facemock.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'facemock/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "facemock"
8
+ spec.version = Facemock::VERSION
9
+ spec.authors = ["ogawatti"]
10
+ spec.email = ["ogawattim@gmail.com"]
11
+ spec.description = %q{This gem is used to mock the communication part of the facebook graph api.}
12
+ spec.summary = %q{This is facebook mock application for fb_graph.}
13
+ spec.homepage = "https://github.com/ogawatti/facemock"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activerecord", "~> 4.1.4"
22
+ spec.add_dependency "sqlite3"
23
+ spec.add_dependency "fb_graph"
24
+ spec.add_dependency "hashie"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.3"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "rspec"
29
+ spec.add_development_dependency "simplecov"
30
+ spec.add_development_dependency "pry"
31
+ spec.add_development_dependency "coveralls"
32
+ end
@@ -0,0 +1,99 @@
1
+ require 'active_record'
2
+
3
+ module Facemock
4
+ module Config
5
+ class Database
6
+ ADAPTER = "sqlite3"
7
+ DB_DIRECTORY = File.expand_path("../../../../db", __FILE__)
8
+ DEFAULT_DB_NAME = "facemock"
9
+ TABLE_NAMES = [:applications, :users, :user_rights]
10
+
11
+ attr_reader :name
12
+
13
+ def initialize(name=nil)
14
+ @name = (name.nil? || name.empty?) ? DEFAULT_DB_NAME : name
15
+ connect
16
+ create_tables
17
+ end
18
+
19
+ def connect
20
+ ActiveRecord::Base.establish_connection(adapter: ADAPTER, database: filepath)
21
+ connection = ActiveRecord::Base.connection unless ActiveRecord::Base.connected?
22
+ ActiveRecord::Migration.verbose = false
23
+ @state = :connected
24
+ connection
25
+ end
26
+
27
+ def disconnect!
28
+ ActiveRecord::Base.connection.disconnect!
29
+ @state = :disconnected
30
+ end
31
+
32
+ def connected?
33
+ @state == :connected
34
+ end
35
+
36
+ def drop
37
+ disconnect!
38
+ File.delete(filepath) if File.exist?(filepath)
39
+ end
40
+
41
+ def clear
42
+ drop_tables
43
+ create_tables
44
+ end
45
+
46
+ def create_tables
47
+ if !File.exist?(filepath) || ActiveRecord::Base.connection.tables.empty?
48
+ TABLE_NAMES.each do |table_name|
49
+ unless ActiveRecord::Base.connection.table_exists? table_name
50
+ self.send "create_#{table_name}_table"
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ def drop_tables
57
+ if File.exist?(filepath) && !ActiveRecord::Base.connection.tables.empty?
58
+ TABLE_NAMES.each do |table_name|
59
+ if ActiveRecord::Base.connection.table_exists? table_name
60
+ ActiveRecord::Migration.drop_table table_name
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ def filepath
67
+ name ||= @name
68
+ File.join(DB_DIRECTORY, "#{@name}.#{ADAPTER}")
69
+ end
70
+
71
+ private
72
+
73
+ def create_applications_table
74
+ ActiveRecord::Migration.create_table :applications do |t|
75
+ t.string :secret, :null => false
76
+ end
77
+ end
78
+
79
+ def create_users_table
80
+ ActiveRecord::Migration.create_table :users do |t|
81
+ t.string :name, :null => false
82
+ t.string :email, :null => false
83
+ t.string :password, :null => false
84
+ t.boolean :installed, :null => false
85
+ t.string :access_token, :null => false
86
+ t.integer :application_id
87
+ t.timestamp :created_at, :null => false
88
+ end
89
+ end
90
+
91
+ def create_user_rights_table
92
+ ActiveRecord::Migration.create_table :user_rights do |t|
93
+ t.string :name, :null => false
94
+ t.integer :user_id, :null => false
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,92 @@
1
+ require 'yaml'
2
+ require 'hashie'
3
+ require 'facemock/errors'
4
+ require 'facemock/config/database'
5
+ require 'facemock/fb_graph/application'
6
+
7
+ module Facemock
8
+ module Config
9
+ extend self
10
+
11
+ def default_database
12
+ Database.new
13
+ end
14
+
15
+ def database(name=nil)
16
+ if name.nil? || name.empty?
17
+ @db = default_database
18
+ else
19
+ @db.disconnect! if @db
20
+ @db = Database.new(name)
21
+ end
22
+ @db
23
+ end
24
+
25
+ def reset_database
26
+ @db = nil
27
+ end
28
+
29
+ def load_users(ymlfile)
30
+ load_data = YAML.load_file(ymlfile)
31
+ raise Facemock::Errors::IncorrectDataFormat.new "data is not Array" unless load_data.kind_of?(Array)
32
+
33
+ load_data.each do |app_data|
34
+ data = Hashie::Mash.new(app_data)
35
+ app_id = data.app_id
36
+ app_secret = data.app_secret
37
+ users = data.users
38
+
39
+ # Validate data format
40
+ raise Facemock::Errors::IncorrectDataFormat.new "app id is empty" unless validate_id(app_id)
41
+ raise Facemock::Errors::IncorrectDataFormat.new "app secret is empty" unless validate_secret(app_secret)
42
+ raise Facemock::Errors::IncorrectDataFormat.new "users format is incorrect" unless validate_users(users)
43
+
44
+ # Create application and user record
45
+ self.database if @db
46
+ app = Facemock::FbGraph::Application.new(app_id, secret: app_secret)
47
+ users.each do |options|
48
+ app.test_user!(options)
49
+ end
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def validate_id(id)
56
+ case id
57
+ when String then !id.empty?
58
+ when Integer then id >= 0
59
+ else false
60
+ end
61
+ end
62
+
63
+ def validate_secret(app_secret)
64
+ case app_secret
65
+ when String then !app_secret.empty?
66
+ else false
67
+ end
68
+ end
69
+
70
+ def validate_users(users)
71
+ case users
72
+ when Array
73
+ return false if users.empty?
74
+ users.each {|user| return false unless validate_user(Hashie::Mash.new(user)) }
75
+ true
76
+ else false
77
+ end
78
+ end
79
+
80
+ def validate_user(user)
81
+ return false unless validate_id(user.identifier)
82
+ [:name, :password, :name].each do |key|
83
+ value = user.send(key)
84
+ case value
85
+ when String then return false if value.empty?
86
+ else return false
87
+ end
88
+ end
89
+ true
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,6 @@
1
+ module Facemock
2
+ module Errors
3
+ class Error < StandardError; end
4
+ class IncorrectDataFormat < StandardError; end
5
+ end
6
+ end
@@ -0,0 +1,36 @@
1
+ require 'facemock/fb_graph/application/user'
2
+
3
+ module Facemock
4
+ module FbGraph
5
+ class Application < ActiveRecord::Base
6
+ class TestUsers < Array
7
+ DEFAULT_LIMIT = 50
8
+ DEFAULT_AFTER = 0
9
+
10
+ def initialize(application_id, options={})
11
+ @limit = limit = (options[:limit] && options[:limit] > 0) ? options[:limit] : DEFAULT_LIMIT
12
+ @after = after = (options[:after] && options[:after] > 0) ? options[:after] : DEFAULT_AFTER
13
+ @application_id = application_id
14
+ st = after
15
+ ed = after + limit - 1
16
+ users = User.where(application_id: application_id).sort_by{|u| u.created_at}
17
+ users = users.reverse[st..ed] || []
18
+ super(users)
19
+ end
20
+
21
+ def collection
22
+ self
23
+ end
24
+
25
+ def next
26
+ options = { limit: @limit, after: @after + @limit }
27
+ TestUsers.new(@application_id, options)
28
+ end
29
+
30
+ def select
31
+ { limit: DEFAULT_LIMIT, after: DEFAULT_AFTER }
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,14 @@
1
+ require 'active_record'
2
+
3
+ module Facemock
4
+ module FbGraph
5
+ class Application < ActiveRecord::Base
6
+ class User < ActiveRecord::Base
7
+ class Right < ActiveRecord::Base
8
+ self.table_name = "user_rights"
9
+ belongs_to :user
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,68 @@
1
+ require 'active_record'
2
+ require 'hashie'
3
+ require 'facemock/fb_graph/application/user/right'
4
+
5
+ module Facemock
6
+ module FbGraph
7
+ class Application < ActiveRecord::Base
8
+ class User < ActiveRecord::Base
9
+ self.table_name = "users"
10
+ alias_attribute :identifier, :id
11
+ has_many :rights, :dependent => :destroy
12
+ attr_reader :permissions
13
+
14
+ def initialize(options={})
15
+ opts = Hashie::Mash.new(options)
16
+ identifier = opts.identifier || ("10000" + (0..9).to_a.shuffle[0..10].join).to_i
17
+ name = opts.name || rand(36**10).to_s(36)
18
+ email = opts.email || name.gsub(" ", "_") + "@example.com"
19
+ password = opts.password || rand(36**10).to_s(36)
20
+ installed = opts.installed || false
21
+ access_token = opts.access_token || Digest::SHA512.hexdigest(identifier.to_s)
22
+ @permissions = []
23
+
24
+ super(
25
+ :name => name,
26
+ :email => email,
27
+ :password => password,
28
+ :installed => installed,
29
+ :access_token => access_token
30
+ )
31
+ self.id = identifier
32
+ if opts.permissions
33
+ build_rights(opts.permissions)
34
+ set_permissions
35
+ elsif opts.application_id
36
+ self.application_id = opts.application_id
37
+ end
38
+ end
39
+
40
+ def fetch
41
+ User.find_by_id(self.id)
42
+ end
43
+
44
+ def revoke!
45
+ self.rights.each{|right| right.destroy}
46
+ @permissions = []
47
+ end
48
+
49
+ private
50
+
51
+ def set_permissions
52
+ @permissions = self.rights.inject([]) do |simbols, right|
53
+ simbols << right.name.to_sym
54
+ end
55
+ @permissions.uniq!
56
+ end
57
+
58
+ def build_rights(permissions_string)
59
+ permissions_string.gsub(/\s/, "").split(",").uniq.each do |permission_name|
60
+ unless self.rights.find{|perm| perm.name == permission_name}
61
+ self.rights.build(name: permission_name)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,44 @@
1
+ require 'active_record'
2
+ require 'hashie'
3
+ require 'facemock/config'
4
+ require 'facemock/fb_graph/application/user'
5
+ require 'facemock/fb_graph/application/test_users'
6
+
7
+ module Facemock
8
+ module FbGraph
9
+ class Application < ActiveRecord::Base
10
+ alias_attribute :identifier, :id
11
+ has_many :users, :dependent => :destroy
12
+
13
+ def initialize(identifier, options={})
14
+ opts = Hashie::Mash.new(options)
15
+ if (identifier == :app && opts.access_token)
16
+ identifier = (0..9).to_a.shuffle[0..15].join
17
+ secret = opts.access_token
18
+ else
19
+ identifer = identifier.to_s
20
+ secret = opts.secret || rand(36**32).to_s(36)
21
+ end
22
+
23
+ super(secret: secret)
24
+ self.identifier = identifier
25
+ save! unless Application.find_by_id_and_secret(identifier, secret)
26
+ end
27
+
28
+ def fetch
29
+ self
30
+ end
31
+
32
+ def test_user!(options={})
33
+ user = User.new(options)
34
+ user.application_id = self.identifier
35
+ user.save!
36
+ user
37
+ end
38
+
39
+ def test_users(options={})
40
+ TestUsers.new(self.identifier, options)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,11 @@
1
+ require 'facemock/fb_graph/user'
2
+ require 'facemock/fb_graph/errors'
3
+
4
+ module Facemock
5
+ module FbGraph
6
+ module Errors
7
+ class Error < StandardError; end
8
+ class InvalidToken < ::FbGraph::InvalidToken; end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ require 'facemock/fb_graph/application/user'
2
+
3
+ module Facemock
4
+ module FbGraph
5
+ module User
6
+ extend self
7
+
8
+ def me(access_token)
9
+ Facemock::Config.database
10
+ Application::User.find_by_access_token(access_token)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,36 @@
1
+ require 'fb_graph'
2
+ require 'facemock/config'
3
+ require 'facemock/fb_graph/user'
4
+ require 'facemock/fb_graph/errors'
5
+ require 'facemock/fb_graph/application'
6
+
7
+ module Facemock
8
+ module FbGraph
9
+ extend self
10
+
11
+ def on(options={})
12
+ if ::FbGraph != Facemock::FbGraph
13
+ Object.const_set(:SourceFbGraph, ::FbGraph)
14
+ Object.send(:remove_const, :FbGraph) if Object.constants.include?(:FbGraph)
15
+ Object.const_set(:FbGraph, Facemock::FbGraph)
16
+ end
17
+
18
+ if options[:database_name]
19
+ Facemock::Config.database(options[:database_name])
20
+ else
21
+ Facemock::Config.database
22
+ end
23
+ true
24
+ end
25
+
26
+ def off
27
+ if ::FbGraph == Facemock::FbGraph
28
+ Object.send(:remove_const, :FbGraph) if Object.constants.include?(:FbGraph)
29
+ Object.const_set(:FbGraph, ::SourceFbGraph)
30
+ Object.send(:remove_const, :SourceFbGraph) if Object.constants.include?(:FbGraph)
31
+ end
32
+ Facemock::Config.reset_database
33
+ true
34
+ end
35
+ end
36
+ end