ki 0.3.3 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (170) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rspec +1 -1
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/Gemfile +2 -21
  7. data/Gemfile.lock +60 -100
  8. data/Guardfile +1 -1
  9. data/LICENSE.md +21 -0
  10. data/README.md +63 -0
  11. data/Rakefile +3 -45
  12. data/bin/ki +1 -1
  13. data/ki.gemspec +31 -137
  14. data/lib/ki.rb +21 -21
  15. data/lib/ki/api_error.rb +28 -0
  16. data/lib/ki/base_request.rb +34 -0
  17. data/lib/ki/helpers.rb +55 -0
  18. data/lib/ki/ki.rb +40 -0
  19. data/lib/ki/ki_cli.rb +46 -0
  20. data/lib/ki/ki_config.rb +20 -0
  21. data/lib/ki/middleware.rb +155 -0
  22. data/lib/ki/model.rb +70 -0
  23. data/lib/{modules → ki/modules}/callbacks.rb +0 -0
  24. data/lib/ki/modules/format_of.rb +10 -0
  25. data/lib/{modules → ki/modules}/model_helpers.rb +0 -12
  26. data/lib/ki/modules/public_file_helper.rb +13 -0
  27. data/lib/ki/modules/query_interface.rb +36 -0
  28. data/lib/{modules → ki/modules}/restrictions.rb +0 -30
  29. data/lib/ki/modules/view_helper.rb +13 -0
  30. data/lib/ki/orm.rb +65 -0
  31. data/lib/ki/version.rb +3 -0
  32. data/spec/examples/base/.ruby-gemset +1 -0
  33. data/spec/examples/base/.ruby-version +1 -0
  34. data/spec/examples/base/Gemfile +4 -0
  35. data/spec/examples/base/app.rb +1 -0
  36. data/{examples → spec/examples}/base/config.ru +0 -3
  37. data/spec/examples/base/config.yml +17 -0
  38. data/spec/examples/base/public/favicon.ico +0 -0
  39. data/spec/examples/base/public/javascripts/.gitkeep +0 -0
  40. data/spec/examples/base/public/stylesheets/.gitkeep +0 -0
  41. data/spec/examples/base/views/index.haml +6 -0
  42. data/spec/examples/couch-lock/.ruby-gemset +1 -0
  43. data/spec/examples/couch-lock/.ruby-version +1 -0
  44. data/spec/examples/couch-lock/Gemfile +4 -0
  45. data/spec/examples/couch-lock/Gemfile.lock +48 -0
  46. data/spec/examples/couch-lock/app.rb +42 -0
  47. data/spec/examples/couch-lock/config.ru +3 -0
  48. data/spec/examples/couch-lock/config.yml +17 -0
  49. data/spec/examples/couch-lock/install.sh +3 -0
  50. data/spec/examples/couch-lock/public/favicon.ico +0 -0
  51. data/spec/examples/couch-lock/public/javascripts/.gitkeep +0 -0
  52. data/spec/examples/couch-lock/public/stylesheets/.gitkeep +0 -0
  53. data/spec/examples/couch-lock/views/index.haml +18 -0
  54. data/spec/examples/json.northpole.ro/.ruby-gemset +1 -0
  55. data/spec/examples/json.northpole.ro/.ruby-version +1 -0
  56. data/spec/examples/json.northpole.ro/Capfile +25 -0
  57. data/spec/examples/json.northpole.ro/Gemfile +6 -0
  58. data/spec/examples/json.northpole.ro/Gemfile.lock +68 -0
  59. data/spec/examples/json.northpole.ro/app.rb +79 -0
  60. data/spec/examples/json.northpole.ro/client.rb +25 -0
  61. data/spec/examples/json.northpole.ro/config.ru +3 -0
  62. data/spec/examples/json.northpole.ro/config.yml +17 -0
  63. data/spec/examples/json.northpole.ro/config/deploy.rb +59 -0
  64. data/spec/examples/json.northpole.ro/config/deploy/production.rb +39 -0
  65. data/spec/examples/json.northpole.ro/foo.rb +10 -0
  66. data/spec/examples/json.northpole.ro/public/JNorthPole.jar +0 -0
  67. data/spec/examples/json.northpole.ro/public/favicon.ico +0 -0
  68. data/spec/examples/json.northpole.ro/public/font/FontAwesome.otf +0 -0
  69. data/spec/examples/json.northpole.ro/public/font/fontawesome-webfont.eot +0 -0
  70. data/spec/examples/json.northpole.ro/public/font/fontawesome-webfont.svg +284 -0
  71. data/spec/examples/json.northpole.ro/public/font/fontawesome-webfont.ttf +0 -0
  72. data/spec/examples/json.northpole.ro/public/font/fontawesome-webfont.woff +0 -0
  73. data/spec/examples/json.northpole.ro/public/images/bear.png +0 -0
  74. data/spec/examples/json.northpole.ro/public/images/bg.png +0 -0
  75. data/spec/examples/json.northpole.ro/public/images/cloud.png +0 -0
  76. data/spec/examples/json.northpole.ro/public/images/dotnet.png +0 -0
  77. data/spec/examples/json.northpole.ro/public/images/dotnet_small.png +0 -0
  78. data/spec/examples/json.northpole.ro/public/images/footer.png +0 -0
  79. data/spec/examples/json.northpole.ro/public/images/glyphicons-halflings-white.png +0 -0
  80. data/spec/examples/json.northpole.ro/public/images/glyphicons-halflings.png +0 -0
  81. data/spec/examples/json.northpole.ro/public/images/ice.png +0 -0
  82. data/spec/examples/json.northpole.ro/public/images/java.png +0 -0
  83. data/spec/examples/json.northpole.ro/public/images/java_small.png +0 -0
  84. data/spec/examples/json.northpole.ro/public/images/json.png +0 -0
  85. data/spec/examples/json.northpole.ro/public/images/json_small.png +0 -0
  86. data/spec/examples/json.northpole.ro/public/images/logo.png +0 -0
  87. data/spec/examples/json.northpole.ro/public/images/mobile.png +0 -0
  88. data/spec/examples/json.northpole.ro/public/images/php.png +0 -0
  89. data/spec/examples/json.northpole.ro/public/images/php_small.png +0 -0
  90. data/spec/examples/json.northpole.ro/public/images/python.png +0 -0
  91. data/spec/examples/json.northpole.ro/public/images/python_small.png +0 -0
  92. data/spec/examples/json.northpole.ro/public/images/ruby.png +0 -0
  93. data/spec/examples/json.northpole.ro/public/images/ruby_small.png +0 -0
  94. data/spec/examples/json.northpole.ro/public/images/shell.png +0 -0
  95. data/spec/examples/json.northpole.ro/public/images/shell_small.png +0 -0
  96. data/spec/examples/json.northpole.ro/public/javascripts/analytics.js +9 -0
  97. data/spec/examples/json.northpole.ro/public/javascripts/app.coffee +133 -0
  98. data/spec/examples/json.northpole.ro/public/javascripts/docs.min.js +16 -0
  99. data/spec/examples/json.northpole.ro/public/javascripts/jnorthpole.coffee +34 -0
  100. data/spec/examples/json.northpole.ro/public/javascripts/util.coffee +31 -0
  101. data/spec/examples/json.northpole.ro/public/stylesheets/app.sass +23 -0
  102. data/spec/examples/json.northpole.ro/public/stylesheets/application.sass +222 -0
  103. data/spec/examples/json.northpole.ro/public/stylesheets/cover.css +154 -0
  104. data/spec/examples/json.northpole.ro/views/curl.haml +19 -0
  105. data/spec/examples/json.northpole.ro/views/doc.haml +16 -0
  106. data/spec/examples/json.northpole.ro/views/doc_table.haml +45 -0
  107. data/spec/examples/json.northpole.ro/views/dotnet.haml +24 -0
  108. data/spec/examples/json.northpole.ro/views/examples.haml +28 -0
  109. data/spec/examples/json.northpole.ro/views/faq.haml +32 -0
  110. data/spec/examples/json.northpole.ro/views/foo.haml +7 -0
  111. data/spec/examples/json.northpole.ro/views/index.haml +4 -0
  112. data/spec/examples/json.northpole.ro/views/java.haml +0 -0
  113. data/spec/examples/json.northpole.ro/views/layout.haml +42 -0
  114. data/spec/examples/json.northpole.ro/views/php.haml +37 -0
  115. data/spec/examples/json.northpole.ro/views/playground.haml +38 -0
  116. data/spec/examples/json.northpole.ro/views/python.haml +17 -0
  117. data/spec/examples/json.northpole.ro/views/ruby.haml +12 -0
  118. data/spec/examples/json.northpole.ro/views/signup.haml +2 -0
  119. data/spec/examples/json.northpole.ro/views/thanks.haml +12 -0
  120. data/spec/functional_spec.rb +4 -0
  121. data/spec/lib/ki/base_request_spec.rb +88 -0
  122. data/spec/lib/ki/helpers_spec.rb +12 -0
  123. data/spec/lib/ki/ki_config_spec.rb +7 -0
  124. data/spec/lib/ki/middleware_spec.rb +11 -0
  125. data/spec/lib/ki/model_spec.rb +100 -0
  126. data/spec/lib/ki/modules/format_of_spec.rb +15 -0
  127. data/spec/lib/ki/orm_spec.rb +83 -0
  128. data/spec/spec_helper.rb +18 -5
  129. data/spec/util_spec.rb +7 -36
  130. metadata +269 -193
  131. data/.document +0 -5
  132. data/.rvmrc +0 -1
  133. data/LICENSE.txt +0 -20
  134. data/README.rdoc +0 -3
  135. data/VERSION +0 -1
  136. data/examples/base/Gemfile +0 -3
  137. data/examples/base/Gemfile.lock +0 -24
  138. data/examples/base/app.rb +0 -4
  139. data/examples/base/config.yml +0 -16
  140. data/gfx/colors.xcf +0 -0
  141. data/gfx/ki3.eps +0 -0
  142. data/gfx/logo.png +0 -0
  143. data/ki_wrap.rb +0 -35
  144. data/lib/conf.rb +0 -28
  145. data/lib/db.rb +0 -73
  146. data/lib/helpers.rb +0 -34
  147. data/lib/ki_cli.rb +0 -76
  148. data/lib/mockreq.rb +0 -45
  149. data/lib/model.rb +0 -110
  150. data/lib/modules/email.rb +0 -51
  151. data/lib/modules/query_interface.rb +0 -42
  152. data/lib/req.rb +0 -57
  153. data/lib/resp.rb +0 -70
  154. data/lib/static_file.rb +0 -68
  155. data/lib/util.rb +0 -114
  156. data/lib/views/404.haml +0 -1
  157. data/lib/views/406.haml +0 -1
  158. data/lib/views/index.haml +0 -7
  159. data/lib/views/layout.haml +0 -1
  160. data/spec/conf_spec.rb +0 -6
  161. data/spec/db_spec.rb +0 -93
  162. data/spec/integration/integration_spec.rb +0 -114
  163. data/spec/ki_spec.rb +0 -7
  164. data/spec/model_spec.rb +0 -91
  165. data/spec/modules/callbacks_spec.rb +0 -25
  166. data/spec/modules/email_spec.rb +0 -19
  167. data/spec/modules/query_interface_spec.rb +0 -93
  168. data/spec/modules/restrictions_spec.rb +0 -110
  169. data/spec/performance/performance.sh +0 -1
  170. data/spec/req_spec.rb +0 -29
@@ -1,114 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- def encrypt s
4
- s + '!'
5
- end
6
-
7
- describe 'Integration Tests' do
8
- before :each do
9
- @db = Ki::Db.instance
10
- @db.remove_collections
11
- end
12
-
13
- it "should handle user and storage" do
14
- class User < Ki::Model
15
- requires :mail, :password
16
- respond_to :json
17
- forbid :delete, :update
18
- unique :mail
19
-
20
- def before_all
21
- params['password'] = encrypt(params['password']) if params['password']
22
- end
23
- end
24
-
25
- class Storage < Ki::Model
26
- respond_to :json
27
- requires :mail, :all_my_data
28
-
29
- def before_all
30
- ensure_authorization
31
- end
32
-
33
- private
34
-
35
- def ensure_authorization
36
- if params['mail'].nil? || params['password'].nil?
37
- raise 'not authorized'
38
- end
39
- h = { 'mail' => params['mail'], 'password' => encrypt(params['password'])}
40
- u = User.find(h)
41
- if u.empty?
42
- raise 'not authorized'
43
- end
44
- params.delete('password')
45
- end
46
- end
47
-
48
- expect {
49
- User.new(Ki::ReqFactory.new(:post, { 'mail' => 'cool@beans.com'})).hash['id']
50
- }.to raise_error 'password missing'
51
-
52
- expect {
53
- id = User.new(Ki::ReqFactory.new(:post, { 'mail' => 'cool@beans.com', 'password' => '123'})).hash['id']
54
- User.new(Ki::ReqFactory.new(:get, { 'id' => id })).hash[0]['password'].should == '123!'
55
- }.to change(User, :count).by(1)
56
-
57
- expect {
58
- id = User.new(Ki::ReqFactory.new(:post, { 'mail' => 'cool@beans.com', 'password' => '123'})).hash['id']
59
- }.to raise_error 'mail not unique'
60
-
61
- expect {
62
- Storage.new(Ki::ReqFactory.new(:post, {}))
63
- }.to raise_error 'not authorized'
64
-
65
- expect {
66
- Storage.new(Ki::ReqFactory.new(:post, {'mail' => 'cool@beans.com'}))
67
- }.to raise_error 'not authorized'
68
-
69
- # create storage
70
- storage = Storage.new(Ki::ReqFactory.new(:post, {'mail' => 'cool@beans.com', 'password' => '123', 'all_my_data' => [1,2,3]}))
71
- id = storage.hash['id']
72
- r = Storage.new(Ki::ReqFactory.new(:get, { 'mail' => 'cool@beans.com', 'password' => '123'}))
73
- r.hash[0]['all_my_data'].should == [1,2,3]
74
- r.hash[0]['password'].should == nil
75
-
76
- expect {
77
- Storage.new(Ki::ReqFactory.new(:delete, {}))
78
- }.to raise_error 'not authorized'
79
-
80
- expect {
81
- Storage.new(Ki::ReqFactory.new(:delete, {'id' => id}))
82
- }.to raise_error 'not authorized'
83
-
84
- expect {
85
- Storage.new(Ki::ReqFactory.new(:delete, {'mail' => 'cool@beans.com', 'password' => '123'}))
86
- }.to raise_error 'param id missing'
87
-
88
- # delete from storage
89
- expect {
90
- Storage.new(Ki::ReqFactory.new(:delete, {'id' => id, 'mail' => 'cool@beans.com', 'password' => '123'}))
91
- }.to change(Storage, :count).by(-1)
92
-
93
- # not allowed to delete user
94
- expect {
95
- id = User.new(Ki::ReqFactory.new(:post, { 'mail' => 'other@beans.com', 'password' => '123'})).hash['id']
96
- p User.new(Ki::ReqFactory.new(:delete, { 'id' => id }))
97
- }.to raise_error 'action forbidden'
98
-
99
- storage = Storage.new(Ki::ReqFactory.new(:post, {'mail' => 'cool@beans.com', 'password' => '123', 'all_my_data' => [4,4,5]}))
100
- id = storage.hash['id']
101
- expect {
102
- Storage.new(Ki::ReqFactory.new(:put, {'mail' => 'cool@beans.com', 'password' => '123', 'all_my_data' => [1]}))
103
- }.to raise_error 'param id missing'
104
-
105
- expect {
106
- Storage.new(Ki::ReqFactory.new(:put, {'mail' => 'cool@beans.com', 'password' => '1234', 'all_my_data' => [1]}))
107
- }.to raise_error 'not authorized'
108
-
109
- storage = Storage.new(Ki::ReqFactory.new(:put, {'id' => id, 'mail' => 'cool@beans.com', 'password' => '123', 'all_my_data' => [1]}))
110
- storage.hash['id'].should == id
111
- storage = Storage.find(id)
112
- storage['all_my_data'].should == [1]
113
- end
114
- end
@@ -1,7 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe Ki do
4
- it "should work" do
5
- 1.should == 1
6
- end
7
- end
@@ -1,91 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe Ki::Model do
4
- before :each do
5
- class Zoidberg < Ki::Model
6
- end
7
- @db = Ki::Db.instance
8
- @db.remove_collections
9
- end
10
-
11
- it "should correctly extract class_name" do
12
- # Zoidberg.new(@get).class_name.should == "zoidberg"
13
- end
14
-
15
- it "should create a new item in the databse" do
16
- post = Ki::ReqFactory.new(:post_homer)
17
- @db.db['zoidberg'].count.should == 0
18
- hash = Zoidberg.new(post).hash
19
- @db.db['zoidberg'].count.should == 1
20
- @db.find('zoidberg', hash['id'])['user'].should == 'homer'
21
- end
22
-
23
- it "should get a model depending on id" do
24
- id = @db.create 'zoidberg', { :name => 'homer' }
25
-
26
- get = Ki::ReqFactory.new(:get, {'id' => id})
27
- Zoidberg.new(get).hash[0]['name'].should == 'homer'
28
- end
29
-
30
- it "should find objects depending on params" do
31
- @db.create 'zoidberg', { :name => 'homer', :valid => true }
32
- @db.create 'zoidberg', { :name => 'bart', :valid => true }
33
- @db.create 'zoidberg', { :name => 'batman', :valid => false, :cool => true }
34
- @db.create 'zoidberg', { :name => 'joker', :valid => false, :cool => true }
35
- @db.create 'zoidberg', { :name => 'joker', :valid => false, :cool => false }
36
-
37
- r = Zoidberg.new(Ki::ReqFactory.new(:get, { :name => 'homer' })).hash
38
- r[0]['name'].should == 'homer'
39
-
40
- r = Zoidberg.new(Ki::ReqFactory.new(:get, { :valid => true })).hash
41
- r.length.should == 2
42
-
43
- r.collect { |z|
44
- z['valid'].should be_true
45
- }
46
-
47
- r = Zoidberg.new(Ki::ReqFactory.new(:get, { :valid => false, :cool => true })).hash
48
- r.length.should == 2
49
- end
50
-
51
- it "should only accept valid object ids" do
52
- expect {
53
- Zoidberg.new(Ki::ReqFactory.new(:get_with_params, { :id => 'homer' }))
54
- }.to raise_error
55
- end
56
-
57
- it "should update a document" do
58
- id = @db.create 'zoidberg', { :name => 'homer' }
59
- @db.find('zoidberg', id)['name'].should == 'homer'
60
-
61
- Zoidberg.new(Ki::ReqFactory.new(:put, {'id' => id, :name => 'bart'}))
62
- @db.find('zoidberg', id)['name'].should == 'bart'
63
- end
64
-
65
- it "should raise error if id is missing when updating" do
66
- expect {
67
- Zoidberg.new(Ki::ReqFactory.new(:put, {:name => 'bart'}))
68
- }.to raise_error
69
- end
70
-
71
- it "should raise error if id is invalid" do
72
- expect {
73
- Zoidberg.new(Ki::ReqFactory.new(:put, {'id' => 'f*ck', :name => 'bart'}))
74
- }.to raise_error
75
- end
76
-
77
- it "should delete a document" do
78
- expect {
79
- Zoidberg.new(Ki::ReqFactory.new(:delete))
80
- }.to raise_error
81
-
82
- expect {
83
- Zoidberg.new(Ki::ReqFactory.new(:delete, {'id' => 'f*ck', :name => 'bart'}))
84
- }.to raise_error
85
-
86
- id = @db.create 'zoidberg', { :name => 'homer' }
87
- expect {
88
- Zoidberg.new(Ki::ReqFactory.new(:delete, {'id' => id}))
89
- }.to change(@db.db['zoidberg'], :count).by(-1)
90
- end
91
- end
@@ -1,25 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe Ki::Callbacks do
4
- it "should have before and after find" do
5
- class Foo < Ki::Model
6
- def before_create
7
- @req.params['meaning_of_life'] = 42
8
- end
9
- end
10
-
11
- id = Foo.new(Ki::ReqFactory.new(:post, {:foo => 'bar'})).hash['id']
12
- Foo.new(Ki::ReqFactory.new(:get, { 'id' => id })).hash[0]['meaning_of_life'].should == 42
13
- end
14
-
15
- it "should handle before and after all" do
16
- class Foo < Ki::Model
17
- def before_all
18
- params[:cool] = true unless get?
19
- end
20
- end
21
-
22
- id = Foo.new(Ki::ReqFactory.new(:post, {:foo => 'bar'})).hash['id']
23
- Foo.new(Ki::ReqFactory.new(:get, { 'id' => id })).hash[0]['cool'].should be_true
24
- end
25
- end
@@ -1,19 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe Ki::Email do
4
- it "should validate email properly" do
5
- extend Ki::Email
6
-
7
- valid_mail?('asd').should be_false
8
- valid_mail?('asd.asd').should be_false
9
- valid_mail?('').should be_false
10
- valid_mail?(nil).should be_false
11
- valid_mail?('asda@asdasd').should be_false
12
- valid_mail?('cool$#Q!@asd.com').should be_false
13
-
14
- valid_mail?('cool1@gmail.com').should be_true
15
- valid_mail?('cool@gmail.co').should be_true
16
- valid_mail?('cool@gmail.co.uk').should be_true
17
- valid_mail?('cool+beans@gmail.com').should be_true
18
- end
19
- end
@@ -1,93 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe Ki::QueryInterface do
4
- before :each do
5
- @db = Ki::Db.instance
6
- @db.remove_collections
7
- end
8
-
9
- it "should find waldo" do
10
- id = @db.create 'waldo', { :location => 'amsterdam' }
11
-
12
- class Waldo < Ki::Model
13
- end
14
-
15
- waldo = Waldo.find(id)
16
- waldo['location'].should == 'amsterdam'
17
- end
18
-
19
- it "should find by a hash" do
20
- @db.create 'waldo', { :location => 'amsterdam', :cool => true, :value => 0 }
21
- @db.create 'waldo', { :location => 'copenhagen', :cool => true, :value => 1 }
22
- @db.create 'waldo', { :location => 'kronstadt', :cool => true, :value => 1 }
23
-
24
- class Waldo < Ki::Model
25
- end
26
-
27
- r = Waldo.find({ :cool => true })
28
- r.length.should == 3
29
-
30
- r = Waldo.find({ :cool => true, :value => 1 })
31
- r.length.should == 2
32
- end
33
-
34
- it "should delete porn" do
35
- id = @db.create 'porn', { :website => 'youporn.com' }
36
-
37
- class Porn < Ki::Model
38
- end
39
-
40
- expect {
41
- Porn.delete(id)
42
- }.to change(@db.db['porn'], :count).by(-1)
43
- end
44
-
45
- it "should count sheep" do
46
- class Sheep < Ki::Model
47
- end
48
-
49
- Sheep.count.should == 0
50
- expect {
51
- @db.create 'sheep', { :wool => 'green' }
52
- }.to change(Sheep, :count).by(1)
53
- Sheep.count.should == 1
54
- end
55
-
56
- it "should create the universe in 7 days" do
57
- class Universe < Ki::Model
58
- end
59
-
60
- universe = { :water => true }
61
- Universe.count.should == 0
62
- expect {
63
- id = Universe.create universe
64
- Universe.find(id)['water'].should be_true
65
- }.to change(Universe, :count).by(1)
66
- end
67
-
68
- it "should update all the software" do
69
- class Software < Ki::Model
70
- end
71
-
72
- id = Software.create({ :version => 1.1 })
73
- expect {
74
- Software.update({'id' => id, :version => 1.2})
75
- Software.find(id)['version'].should == 1.2
76
- }.to change(Software, :count).by(0)
77
- end
78
-
79
- it "should find or create" do
80
- class Crab < Ki::Model
81
- end
82
- hash = {'user' => 'name', 'birth' => '2015'}
83
-
84
- Crab.find(hash).empty?.should be_true
85
- expect {
86
- Crab.find_or_create hash
87
- }.to change(Crab, :count).by(1)
88
-
89
- expect {
90
- Crab.find_or_create hash
91
- }.to change(Crab, :count).by(0)
92
- end
93
- end
@@ -1,110 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe Ki::Restrictions do
4
- before :each do
5
- @db = Ki::Db.instance
6
- @db.remove_collections
7
- end
8
-
9
- it "should only allow specified methods" do
10
- class Foo < Ki::Model
11
- end
12
- expect {
13
- Foo.new(Ki::ReqFactory.new(:head))
14
- }.to raise_error
15
- end
16
-
17
- it "should not have any restrictions by default" do
18
- class Foo < Ki::Model
19
- end
20
- Foo.forbidden_actions.empty?.should be_true
21
- Foo.new(Ki::ReqFactory.new(:post_homer))
22
- end
23
-
24
- it "should restrict search when find is forbidden" do
25
- class Foo < Ki::Model
26
- forbid :find
27
- end
28
- expect {
29
- Foo.new(Ki::ReqFactory.new(:search, {'foo' => 'bar'} ))
30
- }.to raise_exception
31
- end
32
-
33
- it "should only restrict :delete" do
34
- class Foo < Ki::Model
35
- forbid :delete
36
- end
37
- Foo.forbidden_actions.include?(:delete).should be_true
38
- id = Foo.new(Ki::ReqFactory.new(:post_homer)).hash['id']
39
- expect {
40
- Foo.new(Ki::ReqFactory.new(:delete, { 'id' => id } ))
41
- }.to raise_exception
42
- end
43
-
44
- it "should respond to html and json by default" do
45
- class Foo < Ki::Model
46
- end
47
- Foo.responds_to_formats.should == [:json, :html]
48
- end
49
-
50
- it "should allow only json" do
51
- class Foo < Ki::Model
52
- respond_to :json
53
- end
54
- Foo.responds_to_formats.include?(:json).should be_true
55
- Foo.responds_to_formats.include?(:html).should be_false
56
- end
57
-
58
- it "should not have any required attributes" do
59
- class Foo < Ki::Model
60
- end
61
- Foo.required_attributes.empty?.should be_true
62
- end
63
-
64
- it "should require name" do
65
- class Foo < Ki::Model
66
- requires :name
67
- end
68
- Foo.required_attributes.include?(:name).should be_true
69
-
70
- expect {
71
- Foo.new(Ki::ReqFactory.new(:post_homer))
72
- }.to raise_exception
73
-
74
- expect {
75
- Foo.new(Ki::ReqFactory.new(:post, {'name' => 'bart' }))
76
- }.to change(Ki::Db.instance.db['foo'], :count).by(1)
77
- end
78
-
79
- it "should handle unique attributes" do
80
- class Foo < Ki::Model
81
- end
82
- Foo.unique_attributes.should == []
83
-
84
- class Bar < Ki::Model
85
- unique :foo
86
- end
87
- Bar.unique_attributes.should == [:foo]
88
-
89
- expect {
90
- Bar.new(Ki::ReqFactory.new(:post, { 'foo' => 'bar'}))
91
- }.to change(Bar, :count).by(1)
92
-
93
- expect {
94
- Bar.new(Ki::ReqFactory.new(:post, { 'foo' => 'bar'}))
95
- }.to raise_error 'foo not unique'
96
- end
97
-
98
- it "should default to :layout" do
99
- class Foo < Ki::Model
100
- end
101
- Foo.selected_layout.should == :layout
102
- end
103
-
104
- it "should chage default layout" do
105
- class Foo < Ki::Model
106
- layout :none
107
- end
108
- Foo.selected_layout.should == :none
109
- end
110
- end