osm 0.1.17 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -38,14 +38,22 @@ describe "API Access" do
38
38
  FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/users.php?action=getAPIAccess&sectionid=1", :body => body.to_json)
39
39
  end
40
40
 
41
- it "Get All" do
42
- api_accesses = Osm::ApiAccess.get_all(@api, 1)
41
+ describe "Get All" do
42
+ it "From OSM" do
43
+ api_accesses = Osm::ApiAccess.get_all(@api, 1)
44
+
45
+ api_accesses.size.should == 2
46
+ api_access = api_accesses[0]
47
+ api_access.id.should == 1
48
+ api_access.name.should == 'API Name'
49
+ api_access.permissions.should == {:read => [:read], :readwrite => [:read, :write]}
50
+ end
43
51
 
44
- api_accesses.size.should == 2
45
- api_access = api_accesses[0]
46
- api_access.id.should == 1
47
- api_access.name.should == 'API Name'
48
- api_access.permissions.should == {:read => [:read], :readwrite => [:read, :write]}
52
+ it "From cache" do
53
+ api_accesses = Osm::ApiAccess.get_all(@api, 1)
54
+ HTTParty.should_not_receive(:post)
55
+ Osm::ApiAccess.get_all(@api, 1).should == api_accesses
56
+ end
49
57
  end
50
58
 
51
59
  it "Get One" do
@@ -2,30 +2,8 @@
2
2
  require 'spec_helper'
3
3
 
4
4
 
5
- class DummyHttpResult
6
- def initialize(options={})
7
- @response = DummyHttpResponse.new(options[:response])
8
- end
9
- def response
10
- @response
11
- end
12
- end
13
- class DummyHttpResponse
14
- def initialize(options={})
15
- @options = options
16
- end
17
- def code
18
- @options[:code]
19
- end
20
- def body
21
- @options[:body]
22
- end
23
- end
24
-
25
-
26
5
  describe "API" do
27
6
 
28
-
29
7
  it "Create" do
30
8
  @api.should_not be_nil
31
9
  @api.api_id.should == @CONFIGURATION[:api][:osm][:id]
@@ -68,7 +46,7 @@ describe "API" do
68
46
  'email' => user_email,
69
47
  'password' => user_password,
70
48
  }
71
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"userid":"id","secret":"secret"}'}) }
49
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"userid":"id","secret":"secret"}'}) }
72
50
 
73
51
  Osm::Api.authorize(user_email, user_password).should == {:user_id => 'id', :secret => 'secret'}
74
52
  end
@@ -81,15 +59,64 @@ describe "API" do
81
59
  'token' => @CONFIGURATION[:api][:osm][:token],
82
60
  'userid' => '1',
83
61
  'secret' => '2',
84
- }}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'[]'}) }
62
+ }}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'[]'}) }
85
63
  @api.perform_query('test')
86
64
  end
87
65
 
88
66
 
67
+ describe "User Permissions" do
68
+
69
+ it "Get from API" do
70
+ body = [
71
+ {"sectionid"=>"1", "permissions"=>{"a"=>20}},
72
+ {"sectionid"=>"2", "permissions"=>{"a"=>10}}
73
+ ]
74
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getUserRoles", :body => body.to_json)
75
+
76
+ permissions = {1 => {:a => [:read, :write]}, 2 => {:a => [:read]}}
77
+ OsmTest::Cache.should_not_receive('read')
78
+ @api.get_user_permissions.should == permissions
79
+ end
80
+
81
+ it "Get from cache" do
82
+ permissions = {1 => {:a => [:read, :write]}, 2 => {:a => [:read]}}
83
+ OsmTest::Cache.should_receive('exist?').with('OSMAPI-osm-permissions-user_id') { true }
84
+ OsmTest::Cache.should_receive('read').with('OSMAPI-osm-permissions-user_id') { permissions }
85
+ @api.get_user_permissions.should == permissions
86
+ end
87
+
88
+ it "Get ignoring cache" do
89
+ data = [
90
+ {"sectionid"=>"1", "permissions"=>{"a"=>10}},
91
+ ]
92
+ body = {
93
+ 'apiid' => @CONFIGURATION[:api][:osm][:id],
94
+ 'token' => @CONFIGURATION[:api][:osm][:token],
95
+ 'userid' => 'user_id',
96
+ 'secret' => 'secret',
97
+ }
98
+ url = 'https://www.onlinescoutmanager.co.uk/api.php?action=getUserRoles'
99
+
100
+ OsmTest::Cache.should_not_receive('exist?').with('OSMAPI-osm-permissions-user_id')
101
+ OsmTest::Cache.should_not_receive('read').with('OSMAPI-osm-permissions-user_id')
102
+ HTTParty.should_receive(:post).with(url, {:body=>body}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>data.to_json}) }
103
+ @api.get_user_permissions(:no_cache => true).should == {1 => {:a => [:read]}}
104
+ end
105
+
106
+ it "Set" do
107
+ permissions = {1 => {:a => [:read, :write]}, 2 => {:a => [:read]}}
108
+ OsmTest::Cache.should_receive('exist?').with('OSMAPI-osm-permissions-user_id') { true }
109
+ OsmTest::Cache.should_receive('read').with('OSMAPI-osm-permissions-user_id') { permissions }
110
+ OsmTest::Cache.should_receive('write').with('OSMAPI-osm-permissions-user_id', permissions.merge(3 => {:a => [:read]}), {:expires_in=>600}) { true }
111
+ @api.set_user_permissions(3, {:a => [:read]})
112
+ end
113
+
114
+ end
115
+
89
116
 
90
117
  describe "OSM and Internet error conditions:" do
91
118
  it "Raises a connection error if the HTTP status code was not 'OK'" do
92
- HTTParty.stub(:post) { DummyHttpResult.new(:response=>{:code=>'500'}) }
119
+ HTTParty.stub(:post) { OsmTest::DummyHttpResult.new(:response=>{:code=>'500'}) }
93
120
  expect{ Osm::Api.authorize('email@example.com', 'password') }.to raise_error(Osm::ConnectionError, 'HTTP Status code was 500')
94
121
  end
95
122
 
@@ -101,12 +128,12 @@ describe "API" do
101
128
 
102
129
 
103
130
  it "Raises an error if OSM returns an error (as a hash)" do
104
- HTTParty.stub(:post) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"error":"Error message"}'}) }
131
+ HTTParty.stub(:post) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"error":"Error message"}'}) }
105
132
  expect{ Osm::Api.authorize('email@example.com', 'password') }.to raise_error(Osm::Error, 'Error message')
106
133
  end
107
134
 
108
135
  it "Raises an error if OSM returns an error (as a plain string)" do
109
- HTTParty.stub(:post) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'Error message'}) }
136
+ HTTParty.stub(:post) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'Error message'}) }
110
137
  expect{ Osm::Api.authorize('email@example.com', 'password') }.to raise_error(Osm::Error, 'Error message')
111
138
  end
112
139
  end
@@ -128,75 +128,84 @@ describe "Event" do
128
128
  Osm::Model.stub(:get_user_permissions) { {:events => [:read, :write]} }
129
129
  end
130
130
 
131
- it "Get events for section" do
132
- events = Osm::Event.get_for_section(@api, 1)
133
- events.size.should == 1
134
- event = events[0]
135
- event.id.should == 2
136
- event.section_id.should == 1
137
- event.name.should == 'An Event'
138
- event.start.should == Date.new(2001, 1, 2)
139
- event.finish.should == DateTime.new(2001, 2, 5, 12, 0, 0)
140
- event.cost.should == '0.00'
141
- event.location.should == 'Somewhere'
142
- event.notes.should == 'Notes'
143
- event.archived.should be_false
144
- event.notepad.should == 'notepad'
145
- event.public_notepad.should == 'public notepad'
146
- event.confirm_by_date.should == Date.new(2002, 1, 2)
147
- event.allow_changes.should == true
148
- event.reminders.should == false
149
- event.attendance_limit.should == 3
150
- event.attendance_limit_includes_leaders.should == true
151
- event.columns[0].id.should == 'f_1'
152
- event.columns[0].name.should == 'Name'
153
- event.columns[0].label.should == 'Label'
154
- event.valid?.should be_true
155
- end
156
-
157
- it "Fetch events for a section honoring archived option" do
158
- body = {
159
- 'identifier' => 'eventid',
160
- 'label' => 'name',
161
- 'items' => [{
162
- 'eventid' => '1',
163
- 'name' => 'An Event',
164
- 'startdate' => '2001-02-03',
165
- 'enddate' => nil,
166
- 'starttime' => '00:00:00',
167
- 'endtime' => '00:00:00',
168
- 'cost' => '0.00',
169
- 'location' => '',
170
- 'notes' => '',
171
- 'sectionid' => 1,
172
- 'googlecalendar' => nil,
173
- 'archived' => '0'
174
- },{
175
- 'eventid' => '2',
176
- 'name' => 'An Archived Event',
177
- 'startdate' => '2001-02-03',
178
- 'enddate' => nil,
179
- 'starttime' => '00:00:00',
180
- 'endtime' => '00:00:00',
181
- 'cost' => '0.00',
182
- 'location' => '',
183
- 'notes' => '',
184
- 'sectionid' => 1,
185
- 'googlecalendar' => nil,
186
- 'archived' => '1'
187
- }]
188
- }
189
-
190
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/events.php?action=getEvents&sectionid=1&showArchived=true", :body => body.to_json)
191
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/events.php?action=getEvent&sectionid=1&eventid=1", :body => {'config' => '[]', 'archived' => '0', 'eventid' => '1'}.to_json)
192
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/events.php?action=getEvent&sectionid=1&eventid=2", :body => {'config' => '[]', 'archived' => '1', 'eventid' => '2'}.to_json)
131
+ describe "Get events for section" do
132
+ it "From OSM" do
133
+ events = Osm::Event.get_for_section(@api, 1)
134
+ events.size.should == 1
135
+ event = events[0]
136
+ event.id.should == 2
137
+ event.section_id.should == 1
138
+ event.name.should == 'An Event'
139
+ event.start.should == Date.new(2001, 1, 2)
140
+ event.finish.should == DateTime.new(2001, 2, 5, 12, 0, 0)
141
+ event.cost.should == '0.00'
142
+ event.location.should == 'Somewhere'
143
+ event.notes.should == 'Notes'
144
+ event.archived.should be_false
145
+ event.notepad.should == 'notepad'
146
+ event.public_notepad.should == 'public notepad'
147
+ event.confirm_by_date.should == Date.new(2002, 1, 2)
148
+ event.allow_changes.should == true
149
+ event.reminders.should == false
150
+ event.attendance_limit.should == 3
151
+ event.attendance_limit_includes_leaders.should == true
152
+ event.columns[0].id.should == 'f_1'
153
+ event.columns[0].name.should == 'Name'
154
+ event.columns[0].label.should == 'Label'
155
+ event.valid?.should be_true
156
+ end
193
157
 
194
- events = Osm::Event.get_for_section(@api, 1)
195
- all_events = Osm::Event.get_for_section(@api, 1, {:include_archived => true})
158
+ it "From cache" do
159
+ events = Osm::Event.get_for_section(@api, 1)
160
+ HTTParty.should_not_receive(:post)
161
+ Osm::Event.get_for_section(@api, 1).should == events
162
+ end
196
163
 
197
- events.size.should == 1
198
- events[0].id == 1
199
- all_events.size.should == 2
164
+ it "Honours archived option" do
165
+ body = {
166
+ 'identifier' => 'eventid',
167
+ 'label' => 'name',
168
+ 'items' => [{
169
+ 'eventid' => '1',
170
+ 'name' => 'An Event',
171
+ 'startdate' => '2001-02-03',
172
+ 'enddate' => nil,
173
+ 'starttime' => '00:00:00',
174
+ 'endtime' => '00:00:00',
175
+ 'cost' => '0.00',
176
+ 'location' => '',
177
+ 'notes' => '',
178
+ 'sectionid' => 1,
179
+ 'googlecalendar' => nil,
180
+ 'archived' => '0'
181
+ },{
182
+ 'eventid' => '2',
183
+ 'name' => 'An Archived Event',
184
+ 'startdate' => '2001-02-03',
185
+ 'enddate' => nil,
186
+ 'starttime' => '00:00:00',
187
+ 'endtime' => '00:00:00',
188
+ 'cost' => '0.00',
189
+ 'location' => '',
190
+ 'notes' => '',
191
+ 'sectionid' => 1,
192
+ 'googlecalendar' => nil,
193
+ 'archived' => '1'
194
+ }]
195
+ }
196
+
197
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/events.php?action=getEvents&sectionid=1&showArchived=true", :body => body.to_json)
198
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/events.php?action=getEvent&sectionid=1&eventid=1", :body => {'config' => '[]', 'archived' => '0', 'eventid' => '1'}.to_json)
199
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/events.php?action=getEvent&sectionid=1&eventid=2", :body => {'config' => '[]', 'archived' => '1', 'eventid' => '2'}.to_json)
200
+
201
+ events = Osm::Event.get_for_section(@api, 1)
202
+ OsmTest::Cache.clear
203
+ all_events = Osm::Event.get_for_section(@api, 1, {:include_archived => true})
204
+
205
+ events.size.should == 1
206
+ events[0].id == 1
207
+ all_events.size.should == 2
208
+ end
200
209
  end
201
210
 
202
211
  it "Get event" do
@@ -331,7 +340,7 @@ describe "Event" do
331
340
  }
332
341
 
333
342
  Osm::Event.stub(:get_for_section) { [] }
334
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"id":2}'}) }
343
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"id":2}'}) }
335
344
 
336
345
  event = Osm::Event.create(@api, {
337
346
  :section_id => 1,
@@ -356,7 +365,7 @@ describe "Event" do
356
365
 
357
366
  it "Create (failed)" do
358
367
  Osm::Event.stub(:get_for_section) { [] }
359
- HTTParty.should_receive(:post) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
368
+ HTTParty.should_receive(:post) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
360
369
 
361
370
  event = Osm::Event.create(@api, {
362
371
  :section_id => 1,
@@ -400,9 +409,9 @@ describe "Event" do
400
409
  'limitincludesleaders' => true,
401
410
  }
402
411
 
403
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"id":2}'}) }
404
- HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/events.php?action=saveNotepad&sectionid=1', {:body=>{"eventid"=>2, "notepad"=>"notepad", "userid"=>"user_id", "secret"=>"secret", "apiid"=>"1", "token"=>"API TOKEN"}}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
405
- HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/events.php?action=saveNotepad&sectionid=1', {:body=>{"eventid"=>2, "pnnotepad"=>"public notepad", "userid"=>"user_id", "secret"=>"secret", "apiid"=>"1", "token"=>"API TOKEN"}}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
412
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"id":2}'}) }
413
+ HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/events.php?action=saveNotepad&sectionid=1', {:body=>{"eventid"=>2, "notepad"=>"notepad", "userid"=>"user_id", "secret"=>"secret", "apiid"=>"1", "token"=>"API TOKEN"}}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
414
+ HTTParty.should_receive(:post).with('https://www.onlinescoutmanager.co.uk/events.php?action=saveNotepad&sectionid=1', {:body=>{"eventid"=>2, "pnnotepad"=>"public notepad", "userid"=>"user_id", "secret"=>"secret", "apiid"=>"1", "token"=>"API TOKEN"}}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
406
415
 
407
416
  event = Osm::Event.new(
408
417
  :section_id => 1,
@@ -416,16 +425,18 @@ describe "Event" do
416
425
  :confirm_by_date => nil,
417
426
  :allow_changes => true,
418
427
  :reminders => true,
419
- :notepad => 'notepad',
420
- :public_notepad => 'public notepad',
428
+ :notepad => '',
429
+ :public_notepad => '',
421
430
  :attendance_limit => 3,
422
431
  :attendance_limit_includes_leaders => true,
423
432
  )
433
+ event.notepad = 'notepad'
434
+ event.public_notepad = 'public notepad'
424
435
  event.update(@api).should be_true
425
436
  end
426
437
 
427
438
  it "Update (failed)" do
428
- HTTParty.should_receive(:post).exactly(3).times { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
439
+ HTTParty.should_receive(:post).exactly(1).times { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
429
440
 
430
441
  event = Osm::Event.new(
431
442
  :section_id => 1,
@@ -450,7 +461,7 @@ describe "Event" do
450
461
  'secret' => 'secret',
451
462
  }
452
463
 
453
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"ok":true}'}) }
464
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"ok":true}'}) }
454
465
 
455
466
  event = Osm::Event.new(
456
467
  :section_id => 1,
@@ -466,7 +477,7 @@ describe "Event" do
466
477
  end
467
478
 
468
479
  it "Delete (failed)" do
469
- HTTParty.should_receive(:post) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"ok":false}'}) }
480
+ HTTParty.should_receive(:post) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"ok":false}'}) }
470
481
 
471
482
  event = Osm::Event.new(
472
483
  :section_id => 1,
@@ -535,7 +546,7 @@ describe "Event" do
535
546
  'userid' => 'user_id',
536
547
  'secret' => 'secret',
537
548
  }}
538
- ) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
549
+ ) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
539
550
 
540
551
  ea.update(@api, 'f_1').should be_true
541
552
  end
@@ -555,7 +566,7 @@ describe "Event" do
555
566
  'eventid' => '2',
556
567
  'config' => '[{"id":"f_1","name":"Test name","pL":"Test label"}]'
557
568
  }
558
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>body.to_json}) }
569
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>body.to_json}) }
559
570
 
560
571
  event = Osm::Event.new(:id => 2, :section_id => 1)
561
572
  event.should_not be_nil
@@ -567,7 +578,7 @@ describe "Event" do
567
578
  end
568
579
 
569
580
  it "Add column (failed)" do
570
- HTTParty.should_receive(:post) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"config":"[]"}'}) }
581
+ HTTParty.should_receive(:post) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"config":"[]"}'}) }
571
582
 
572
583
  event = Osm::Event.new(:id => 2, :section_id => 1)
573
584
  event.should_not be_nil
@@ -590,7 +601,7 @@ describe "Event" do
590
601
  'eventid' => '2',
591
602
  'config' => '[{"id":"f_1","name":"New name","pL":"New label"}]'
592
603
  }
593
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>body.to_json}) }
604
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>body.to_json}) }
594
605
 
595
606
  event = Osm::Event.new(:id => 2, :section_id => 1)
596
607
  event.columns = [Osm::Event::Column.new(:id => 'f_1', :event => event)]
@@ -607,7 +618,7 @@ describe "Event" do
607
618
  end
608
619
 
609
620
  it "Update column (failed)" do
610
- HTTParty.should_receive(:post) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"config":"[]"}'}) }
621
+ HTTParty.should_receive(:post) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"config":"[]"}'}) }
611
622
 
612
623
  event = Osm::Event.new(:id => 2, :section_id => 1)
613
624
  column = Osm::Event::Column.new(:id => 'f_1', :event => event)
@@ -626,7 +637,7 @@ describe "Event" do
626
637
  'columnId' => 'f_1'
627
638
  }
628
639
 
629
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"eventid":"2","config":"[]"}'}) }
640
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"eventid":"2","config":"[]"}'}) }
630
641
 
631
642
  event = Osm::Event.new(:id => 2, :section_id => 1)
632
643
  column = Osm::Event::Column.new(:id => 'f_1', :event => event)
@@ -637,7 +648,7 @@ describe "Event" do
637
648
  end
638
649
 
639
650
  it "Delete column (failed)" do
640
- HTTParty.should_receive(:post) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"config":"[{\"id\":\"f_1\"}]"}'}) }
651
+ HTTParty.should_receive(:post) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"config":"[{\"id\":\"f_1\"}]"}'}) }
641
652
 
642
653
  event = Osm::Event.new(:id => 2, :section_id => 1)
643
654
  column = Osm::Event::Column.new(:id => 'f_1', :event => event)
@@ -5,11 +5,24 @@ require 'date'
5
5
 
6
6
  describe "Flexi Record" do
7
7
 
8
- it "Create FlexiRecord::Field" do
9
- field = Osm::FlexiRecord::Field.new(
8
+ it "Create" do
9
+ fr = Osm::FlexiRecord.new(
10
+ :id => 1,
11
+ :section_id => 2,
12
+ :name => 'name'
13
+ )
14
+ fr.id.should == 1
15
+ fr.section_id.should == 2
16
+ fr.name.should == 'name'
17
+ fr.valid?.should be_true
18
+ end
19
+
20
+ it "Create FlexiRecord::Column" do
21
+ field = Osm::FlexiRecord::Column.new(
10
22
  :id => "f_1",
11
23
  :name => "Field Name",
12
- :editable => true
24
+ :editable => true,
25
+ :flexi_record => Osm::FlexiRecord.new(),
13
26
  )
14
27
 
15
28
  field.id.should == 'f_1'
@@ -31,7 +44,8 @@ describe "Flexi Record" do
31
44
  'age' => nil,
32
45
  'f_1' => 'a',
33
46
  'f_2' => 'b',
34
- }
47
+ },
48
+ :flexi_record => Osm::FlexiRecord.new()
35
49
  )
36
50
 
37
51
  rd.member_id.should == 1
@@ -49,9 +63,22 @@ describe "Flexi Record" do
49
63
  rd.valid?.should be_true
50
64
  end
51
65
 
66
+ it "Sorts by name" do
67
+ fr1 = Osm::FlexiRecord.new(:id => 3, :name => 'A')
68
+ fr2 = Osm::FlexiRecord.new(:id => 2, :name => 'B')
69
+ fr3 = Osm::FlexiRecord.new(:id => 1, :name => 'C')
70
+ records = [fr2, fr1, fr3]
71
+
72
+ records.sort.should == [fr1, fr2, fr3]
73
+ end
74
+
52
75
 
53
76
  describe "Using the API" do
54
77
 
78
+ before :each do
79
+ @flexi_record = Osm::FlexiRecord.new(:section_id => 1, :id => 2, :name => 'A Flexi Record')
80
+ end
81
+
55
82
  it "Fetch Fields" do
56
83
  data = {
57
84
  "extraid" => "2",
@@ -76,7 +103,7 @@ describe "Flexi Record" do
76
103
  }
77
104
  FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/extras.php?action=getExtra&sectionid=1&extraid=2", :body => data.to_json)
78
105
 
79
- fields = Osm::FlexiRecord.get_fields(@api, 1, 2)
106
+ fields = @flexi_record.get_columns(@api)
80
107
  fields.is_a?(Array).should be_true
81
108
  fields[0].valid?.should be_true
82
109
  fields[0].id.should == 'firstname'
@@ -116,9 +143,9 @@ describe "Flexi Record" do
116
143
  ]}
117
144
  ]
118
145
  }
119
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>data.to_json}) }
146
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>data.to_json}) }
120
147
 
121
- Osm::FlexiRecord.add_field(@api, 1, 2, 'name').should be_true
148
+ @flexi_record.add_column(@api, 'name').should be_true
122
149
  end
123
150
 
124
151
  it "Add field (failure)" do
@@ -141,9 +168,9 @@ describe "Flexi Record" do
141
168
  ]}
142
169
  ]
143
170
  }
144
- HTTParty.should_receive(:post) { DummyHttpResult.new(:response=>{:code=>'200', :body=>data.to_json}) }
171
+ HTTParty.should_receive(:post) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>data.to_json}) }
145
172
 
146
- Osm::FlexiRecord.add_field(@api, 1, 2, 'name').should be_false
173
+ @flexi_record.add_column(@api, 'name').should be_false
147
174
  end
148
175
 
149
176
  it "Update field (success)" do
@@ -178,9 +205,15 @@ describe "Flexi Record" do
178
205
  ]}
179
206
  ]
180
207
  }
181
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>data.to_json}) }
182
-
183
- Osm::FlexiRecord.update_field(@api, 1, 2, 'f_1', 'name').should be_true
208
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>data.to_json}) }
209
+
210
+ col = Osm::FlexiRecord::Column.new(
211
+ :flexi_record => @flexi_record,
212
+ :id => 'f_1',
213
+ :name => 'name',
214
+ :editable => true
215
+ )
216
+ col.update(@api).should be_true
184
217
  end
185
218
 
186
219
  it "Update field (failure)" do
@@ -203,11 +236,26 @@ describe "Flexi Record" do
203
236
  ]}
204
237
  ]
205
238
  }
206
- HTTParty.should_receive(:post) { DummyHttpResult.new(:response=>{:code=>'200', :body=>data.to_json}) }
207
-
208
- Osm::FlexiRecord.update_field(@api, 1, 2, 'f_1', 'name').should be_false
239
+ HTTParty.should_receive(:post) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>data.to_json}) }
240
+
241
+ col = Osm::FlexiRecord::Column.new(
242
+ :flexi_record => @flexi_record,
243
+ :id => 'f_1',
244
+ :name => 'name',
245
+ :editable => true
246
+ )
247
+ col.update(@api).should be_false
209
248
  end
210
249
 
250
+ it "Update field (uneditable)" do
251
+ col = Osm::FlexiRecord::Column.new(
252
+ :flexi_record => @flexi_record,
253
+ :id => 'f_1',
254
+ :name => 'name',
255
+ :editable => false
256
+ )
257
+ expect{ col.update(@api) }.to raise_error(Osm::Forbidden)
258
+ end
211
259
 
212
260
  it "Delete field (success)" do
213
261
  url = "https://www.onlinescoutmanager.co.uk/extras.php?action=deleteColumn&sectionid=1&extraid=2"
@@ -238,9 +286,16 @@ describe "Flexi Record" do
238
286
  {"rows" => []}
239
287
  ]
240
288
  }
241
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>data.to_json}) }
242
289
 
243
- Osm::FlexiRecord.delete_field(@api, 1, 2, 'f_1').should be_true
290
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>data.to_json}) }
291
+
292
+ col = Osm::FlexiRecord::Column.new(
293
+ :flexi_record => @flexi_record,
294
+ :id => 'f_1',
295
+ :name => 'name',
296
+ :editable => true
297
+ )
298
+ col.delete(@api).should be_true
244
299
  end
245
300
 
246
301
  it "Delete field (failure)" do
@@ -263,11 +318,26 @@ describe "Flexi Record" do
263
318
  ]}
264
319
  ]
265
320
  }
266
- HTTParty.should_receive(:post) { DummyHttpResult.new(:response=>{:code=>'200', :body=>data.to_json}) }
267
-
268
- Osm::FlexiRecord.delete_field(@api, 1, 2, 'f_1').should be_false
321
+ HTTParty.should_receive(:post) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>data.to_json}) }
322
+
323
+ col = Osm::FlexiRecord::Column.new(
324
+ :flexi_record => @flexi_record,
325
+ :id => 'f_1',
326
+ :name => 'name',
327
+ :editable => true
328
+ )
329
+ col.delete(@api).should be_false
269
330
  end
270
331
 
332
+ it "Delete field (uneditable)" do
333
+ col = Osm::FlexiRecord::Column.new(
334
+ :flexi_record => @flexi_record,
335
+ :id => 'f_1',
336
+ :name => 'name',
337
+ :editable => false
338
+ )
339
+ expect{ col.delete(@api) }.to raise_error(Osm::Forbidden)
340
+ end
271
341
 
272
342
  it "Fetch Data" do
273
343
  data = {
@@ -288,8 +358,9 @@ describe "Flexi Record" do
288
358
  }]
289
359
  }
290
360
  FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/extras.php?action=getExtraRecords&sectionid=1&extraid=2&termid=3&section=cubs", :body => data.to_json)
361
+ Osm::Section.stub(:get) { Osm::Section.new(:id => 1, :type => :cubs) }
291
362
 
292
- records = Osm::FlexiRecord.get_data(@api, Osm::Section.new(:id => 1, :type => :cubs), 2, 3)
363
+ records = @flexi_record.get_data(@api, 3)
293
364
  records.is_a?(Array).should be_true
294
365
  records.size.should == 1
295
366
  record = records[0]
@@ -317,36 +388,68 @@ describe "Flexi Record" do
317
388
  'token' => @CONFIGURATION[:api][:osm][:token],
318
389
  'userid' => 'user_id',
319
390
  'secret' => 'secret',
320
- 'termid' => 1,
321
- 'scoutid' => 2,
391
+ 'termid' => 3,
392
+ 'scoutid' => 4,
322
393
  'column' => 'f_1',
323
394
  'value' => 'value',
324
- 'sectionid' => 3,
325
- 'extraid' => 4,
395
+ 'sectionid' => 1,
396
+ 'extraid' => 2,
326
397
  }
327
398
 
328
399
  data = {
329
400
  'items' => [
330
- {'f_1' => 'value', 'scoutid' => '2'},
401
+ {'f_1' => 'value', 'scoutid' => '4'},
331
402
  ]
332
403
  }
333
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>data.to_json}) }
334
- Osm::Term.stub(:get_current_term_for_section) { Osm::Term.new(:id => 1) }
335
-
336
- Osm::FlexiRecord.update_data(@api, 3, 4, 2, 'f_1', 'value').should be_true
404
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>data.to_json}) }
405
+ Osm::Term.stub(:get_current_term_for_section) { Osm::Term.new(:id => 3) }
406
+
407
+ fr = Osm::FlexiRecord.new(:section_id => 1, :id => 2)
408
+ fr.stub(:get_columns) { [Osm::FlexiRecord::Column.new(:id => 'f_1', :editable => true)] }
409
+ fr_data = Osm::FlexiRecord::Data.new(
410
+ :flexi_record => fr,
411
+ :member_id => 4,
412
+ :grouping_id => 5,
413
+ :fields => {'f_1' => 'value'}
414
+ )
415
+ fr_data.update(@api).should be_true
337
416
  end
338
417
 
339
418
  it "Update data (failed)" do
340
419
  data = {
341
420
  'items' => [
342
- {'f_1' => 'old value', 'scoutid' => '2'},
421
+ {'f_1' => 'old value', 'scoutid' => '4'},
343
422
  ]
344
423
  }
345
424
 
346
- HTTParty.stub(:post) { DummyHttpResult.new(:response=>{:code=>'200', :body=>data.to_json}) }
425
+ HTTParty.stub(:post) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>data.to_json}) }
426
+ Osm::Term.stub(:get_current_term_for_section) { Osm::Term.new(:id => 1) }
427
+
428
+ fr = Osm::FlexiRecord.new(:section_id => 1, :id => 2)
429
+ fr.stub(:get_columns) { [Osm::FlexiRecord::Column.new(:id => 'f_1', :editable => true)] }
430
+
431
+ fr_data = Osm::FlexiRecord::Data.new(
432
+ :flexi_record => fr,
433
+ :member_id => 4,
434
+ :grouping_id => 5,
435
+ :fields => {'f_1' => 'value'}
436
+ )
437
+ fr_data.update(@api).should be_false
438
+ end
439
+
440
+ it "Update data (uneditable field)" do
347
441
  Osm::Term.stub(:get_current_term_for_section) { Osm::Term.new(:id => 1) }
348
442
 
349
- Osm::FlexiRecord.update_data(@api, 3, 4, 2, 'f_1', 'value').should be_false
443
+ fr = Osm::FlexiRecord.new(:section_id => 1, :id => 2)
444
+ fr.stub(:get_columns) { [Osm::FlexiRecord::Column.new(:id => 'f_1', :editable => false)] }
445
+
446
+ fr_data = Osm::FlexiRecord::Data.new(
447
+ :flexi_record => fr,
448
+ :member_id => 4,
449
+ :grouping_id => 5,
450
+ :fields => {'f_1' => 'value'}
451
+ )
452
+ fr_data.update(@api).should be_true
350
453
  end
351
454
 
352
455
 
@@ -381,8 +484,9 @@ describe "Flexi Record" do
381
484
  }]
382
485
  }
383
486
  FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/extras.php?action=getExtraRecords&sectionid=1&extraid=2&termid=3&section=cubs", :body => data.to_json)
487
+ Osm::Section.stub(:get) { Osm::Section.new(:id => 1, :type => :cubs) }
384
488
 
385
- records = Osm::FlexiRecord.get_data(@api, Osm::Section.new(:id => 1, :type => :cubs), 2, 3)
489
+ records = @flexi_record.get_data(@api, 3)
386
490
  records.is_a?(Array).should be_true
387
491
  records.size.should == 1
388
492
  record = records[0]
@@ -392,7 +496,6 @@ describe "Flexi Record" do
392
496
  record.fields['lastname'].should == 'Last'
393
497
  end
394
498
 
395
-
396
499
  end
397
500
 
398
501
  end