facemock 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.coveralls.yml +1 -0
  2. data/.gitignore +22 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +6 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +154 -0
  8. data/Rakefile +6 -0
  9. data/db/.gitkeep +0 -0
  10. data/facemock.gemspec +31 -0
  11. data/lib/facemock/config.rb +88 -0
  12. data/lib/facemock/database/application.rb +20 -0
  13. data/lib/facemock/database/permission.rb +21 -0
  14. data/lib/facemock/database/table.rb +340 -0
  15. data/lib/facemock/database/user.rb +26 -0
  16. data/lib/facemock/database.rb +121 -0
  17. data/lib/facemock/errors.rb +7 -0
  18. data/lib/facemock/fb_graph/application/test_users.rb +36 -0
  19. data/lib/facemock/fb_graph/application/user/permission.rb +10 -0
  20. data/lib/facemock/fb_graph/application/user.rb +69 -0
  21. data/lib/facemock/fb_graph/application.rb +48 -0
  22. data/lib/facemock/fb_graph/user.rb +13 -0
  23. data/lib/facemock/fb_graph.rb +30 -0
  24. data/lib/facemock/version.rb +3 -0
  25. data/lib/facemock.rb +19 -0
  26. data/spec/facemock/config_spec.rb +185 -0
  27. data/spec/facemock/database/application_spec.rb +73 -0
  28. data/spec/facemock/database/permission_spec.rb +52 -0
  29. data/spec/facemock/database/tables_spec.rb +728 -0
  30. data/spec/facemock/database/user_spec.rb +169 -0
  31. data/spec/facemock/database_spec.rb +270 -0
  32. data/spec/facemock/errors_spec.rb +9 -0
  33. data/spec/facemock/fb_graph/application/test_users_spec.rb +155 -0
  34. data/spec/facemock/fb_graph/application/user_spec.rb +208 -0
  35. data/spec/facemock/fb_graph/application_spec.rb +132 -0
  36. data/spec/facemock/fb_graph/user_spec.rb +36 -0
  37. data/spec/facemock/fb_graph_spec.rb +47 -0
  38. data/spec/facemock_spec.rb +74 -0
  39. data/spec/spec_helper.rb +18 -0
  40. data/spec/support/tables_helper.rb +46 -0
  41. metadata +64 -3
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::InvalidToken.new "test exception"
144
+ rescue => e
145
+ puts "#{e.class} : #{e.message}" #=> Facemock::FbGraph::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,31 @@
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 "sqlite3"
22
+ spec.add_dependency "fb_graph"
23
+ spec.add_dependency "hashie"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "simplecov"
29
+ spec.add_development_dependency "pry"
30
+ spec.add_development_dependency "coveralls"
31
+ end
@@ -0,0 +1,88 @@
1
+ require 'yaml'
2
+ require 'hashie'
3
+ require 'facemock/errors'
4
+ require 'facemock/database'
5
+ require 'facemock/fb_graph/application'
6
+
7
+ module Facemock
8
+ module Config
9
+ extend self
10
+
11
+ def default_database
12
+ Facemock::Database.new
13
+ end
14
+
15
+ def database
16
+ default_database
17
+ end
18
+
19
+ def reset_database
20
+ db = Facemock::Database.new
21
+ db.disconnect!
22
+ db.drop
23
+ end
24
+
25
+ def load_users(ymlfile)
26
+ load_data = YAML.load_file(ymlfile)
27
+ raise Facemock::Errors::IncorrectDataFormat.new "data is not Array" unless load_data.kind_of?(Array)
28
+
29
+ load_data.each do |app_data|
30
+ data = Hashie::Mash.new(app_data)
31
+ app_id = data.app_id
32
+ app_secret = data.app_secret
33
+ users = data.users
34
+
35
+ # Validate data format
36
+ raise Facemock::Errors::IncorrectDataFormat.new "app id is empty" unless validate_id(app_id)
37
+ raise Facemock::Errors::IncorrectDataFormat.new "app secret is empty" unless validate_secret(app_secret)
38
+ raise Facemock::Errors::IncorrectDataFormat.new "users format is incorrect" unless validate_users(users)
39
+
40
+ # Create application and user record
41
+ self.database if @db
42
+ app = Facemock::FbGraph::Application.new(app_id, secret: app_secret)
43
+ users.each do |options|
44
+ app.test_user!(options)
45
+ end
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def validate_id(id)
52
+ case id
53
+ when String then !id.empty?
54
+ when Integer then id >= 0
55
+ else false
56
+ end
57
+ end
58
+
59
+ def validate_secret(app_secret)
60
+ case app_secret
61
+ when String then !app_secret.empty?
62
+ else false
63
+ end
64
+ end
65
+
66
+ def validate_users(users)
67
+ case users
68
+ when Array
69
+ return false if users.empty?
70
+ users.each {|user| return false unless validate_user(Hashie::Mash.new(user)) }
71
+ true
72
+ else false
73
+ end
74
+ end
75
+
76
+ def validate_user(user)
77
+ return false unless validate_id(user.identifier)
78
+ [:name, :password, :name].each do |key|
79
+ value = user.send(key)
80
+ case value
81
+ when String then return false if value.empty?
82
+ else return false
83
+ end
84
+ end
85
+ true
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,20 @@
1
+ require 'facemock/database'
2
+ require 'facemock/database/table'
3
+ require 'sqlite3'
4
+ require 'hashie'
5
+
6
+ module Facemock
7
+ class Database
8
+ class Application < Table
9
+ TABLE_NAME = :applications
10
+ COLUMN_NAMES = [:id, :secret, :created_at]
11
+
12
+ def initialize(options={})
13
+ opts = Hashie::Mash.new(options)
14
+ @id = ( opts.id.to_i > 0 ) ? opts.id.to_i : (0..9).to_a.shuffle[0..15].join.to_i
15
+ @secret = opts.secret || Digest::SHA512.hexdigest(identifier.to_s)
16
+ @created_at = opts.created_at
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ require 'facemock/database'
2
+ require 'facemock/database/table'
3
+ require 'sqlite3'
4
+ require 'hashie'
5
+
6
+ module Facemock
7
+ class Database
8
+ class Permission < Table
9
+ TABLE_NAME = :permissions
10
+ COLUMN_NAMES = [:id, :name, :user_id, :created_at]
11
+
12
+ def initialize(options={})
13
+ opts = Hashie::Mash.new(options)
14
+ @id = opts.id
15
+ @name = opts.name
16
+ @user_id = opts.user_id
17
+ @created_at = opts.created_at
18
+ end
19
+ end
20
+ end
21
+ end