osm 0.0.1.alpha

Sign up to get free protection for your applications and to get access to all the features.
data/osm.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require File.join(File.dirname(__FILE__), 'version')
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "osm"
7
+ s.version = Osm::VERSION
8
+ s.authors = ['Robert Gauld']
9
+ s.email = ['robert@robertgauld.co.uk']
10
+ s.homepage = ''
11
+ s.summary = %q{Use the Online Scout Manager API}
12
+ s.description = %q{Use the Online Scout Manager API (https://www.onlinescoutmanager.co.uk) to retrieve and save data.}
13
+
14
+ s.rubyforge_project = "osm"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_runtime_dependency 'rails', '>= 3.2.5' # Used to provide the cache (and the environment controls slightly different behavior in perform_query)
22
+ s.add_runtime_dependency 'activesupport', '>= 3.2' # Used to parse JSON from OSM
23
+ s.add_runtime_dependency 'httparty' # Used to make web requests to the API
24
+
25
+ s.add_development_dependency 'rake'
26
+ s.add_development_dependency 'rspec'
27
+ s.add_development_dependency 'fakeweb'
28
+
29
+ end
@@ -0,0 +1,64 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe "Activity" do
5
+
6
+ it "Create" do
7
+ data = {
8
+ 'details' => {
9
+ 'activityid' => '1',
10
+ 'version' => '0',
11
+ 'groupid' => '2',
12
+ 'userid' => '3',
13
+ 'title' => 'Activity Name',
14
+ 'description' => 'Description',
15
+ 'resources' => 'Resources',
16
+ 'instructions' => 'Instructions',
17
+ 'runningtime' => '15',
18
+ 'location' => 'indoors',
19
+ 'shared' => '0',
20
+ 'rating' => '4',
21
+ 'facebook' => ''
22
+ },
23
+ 'editable' => true,
24
+ 'deletable' => false,
25
+ 'used' => 3,
26
+ 'versions' => [
27
+ {
28
+ 'value' => '0',
29
+ 'userid' => '1',
30
+ 'firstname' => 'Alice',
31
+ 'label' => 'Current version - Alice',
32
+ 'selected' => 'selected'
33
+ }
34
+ ],
35
+ 'sections' => ['beavers', 'cubs'],
36
+ 'tags' => ['Tag 1', 'Tag2'],
37
+ 'files' => [],
38
+ 'badges' => []
39
+ }
40
+ activity = Osm::Activity.new(data)
41
+
42
+ activity.id.should == 1
43
+ activity.version.should == 0
44
+ activity.group_id.should == 2
45
+ activity.user_id.should == 3
46
+ activity.title.should == 'Activity Name'
47
+ activity.description.should == 'Description'
48
+ activity.resources.should == 'Resources'
49
+ activity.instructions.should == 'Instructions'
50
+ activity.running_time.should == 15
51
+ activity.location.should == :indoors
52
+ activity.shared.should == 0
53
+ activity.rating.should == 4
54
+ activity.editable.should == true
55
+ activity.deletable.should == false
56
+ activity.used.should == 3
57
+ activity.versions.should == [{:value => 0, :user_id => 1, :firstname => 'Alice', :label => 'Current version - Alice', :selected => true}]
58
+ activity.sections.should == [:beavers, :cubs]
59
+ activity.tags.should == ['Tag 1', 'Tag2']
60
+ activity.files.should == []
61
+ activity.badges.should == []
62
+ end
63
+
64
+ end
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+
5
+ describe "API Access" do
6
+
7
+ it "Create" do
8
+ data = {
9
+ 'apiid' => '1',
10
+ 'name' => 'Name',
11
+ 'permissions' => {'permission' => '100'},
12
+ }
13
+ api_access = Osm::ApiAccess.new(data)
14
+
15
+ api_access.id.should == 1
16
+ api_access.name.should == 'Name'
17
+ api_access.permissions.should == {:permission => 100}
18
+ end
19
+
20
+
21
+ it "Allows interegation of the permissions hash" do
22
+ api_access = Osm::ApiAccess.new({
23
+ 'apiid' => '1',
24
+ 'name' => 'Name',
25
+ 'permissions' => {
26
+ 'read_only' => 10,
27
+ 'read_write' => 20,
28
+ },
29
+ })
30
+
31
+ api_access.can_read?(:read_only).should == true
32
+ api_access.can_read?(:read_write).should == true
33
+
34
+ api_access.can_write?(:read_only).should == false
35
+ api_access.can_write?(:read_write).should == true
36
+
37
+ api_access.can_read?(:non_existant).should == false
38
+ api_access.can_write?(:non_existant).should == false
39
+ end
40
+
41
+
42
+ it "Tells us if it's the our api" do
43
+ Osm::Api.stub(:api_id) { '1' }
44
+
45
+ apis = {
46
+ :ours => Osm::ApiAccess.new({'apiid' => '1', 'name' => 'Name', 'permissions' => {}}),
47
+ :not_ours => Osm::ApiAccess.new({'apiid' => '2', 'name' => 'Name', 'permissions' => {}}),
48
+ }
49
+
50
+ apis[:ours].our_api?.should == true
51
+ apis[:not_ours].our_api?.should == false
52
+ end
53
+
54
+ end
@@ -0,0 +1,561 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
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
+ describe "API" do
27
+
28
+ before(:each) do
29
+ @api_config = {
30
+ :api_id => '1',
31
+ :api_token => 'API TOKEN',
32
+ :api_name => 'API NAME',
33
+ :api_site => :scout,
34
+ }.freeze
35
+ Osm::Api.configure(@api_config)
36
+ end
37
+
38
+ it "Create" do
39
+ api = Osm::Api.new
40
+
41
+ api.nil?.should == false
42
+ Osm::Api.api_id.should == @api_config[:api_id]
43
+ Osm::Api.api_name.should == @api_config[:api_name]
44
+ end
45
+
46
+ it "Raises errors on bad arguents to configure" do
47
+ # Missing options
48
+ expect{ Osm::Api.configure(@api_config.select{ |k,v| (k != :api_id )}) }.to raise_error(ArgumentError, ':api_id does not exist in options hash')
49
+ expect{ Osm::Api.configure(@api_config.select{ |k,v| (k != :api_token)}) }.to raise_error(ArgumentError, ':api_token does not exist in options hash')
50
+ expect{ Osm::Api.configure(@api_config.select{ |k,v| (k != :api_name)}) }.to raise_error(ArgumentError, ':api_name does not exist in options hash')
51
+ expect{ Osm::Api.configure(@api_config.select{ |k,v| (k != :api_site)}) }.to raise_error(ArgumentError, ':api_site does not exist in options hash or is invalid, this should be set to either :scout or :guide')
52
+
53
+ # Invalid site
54
+ expect{ Osm::Api.configure(@api_config.select{ |k,v| (k != :api_site)}.merge(:api_site => :invalid)) }.to raise_error(ArgumentError, ':api_site does not exist in options hash or is invalid, this should be set to either :scout or :guide')
55
+
56
+ # Invalid default_cache_ttl
57
+ expect{ Osm::Api.configure(@api_config.merge(:default_cache_ttl => -1)) }.to raise_error(ArgumentError, ':default_cache_ttl must be greater than 0')
58
+ expect{ Osm::Api.configure(@api_config.merge(:default_cache_ttl => 'invalid')) }.to raise_error(ArgumentError, ':default_cache_ttl must be greater than 0')
59
+ end
60
+
61
+
62
+ it "Raises errors on bad arguments to create" do
63
+ # Both userid and secret (or neither) must be passed
64
+ expect{ Osm::Api.new('1') }.to raise_error(ArgumentError, 'You must pass a secret if you are passing a userid')
65
+ expect{ Osm::Api.new(nil, '1') }.to raise_error(ArgumentError, 'You must pass a userid if you are passing a secret')
66
+
67
+ expect{ Osm::Api.new('1', '2', :invalid_site) }.to raise_error(ArgumentError, 'site is invalid, if passed it should be either :scout or :guide')
68
+ end
69
+
70
+
71
+ it "authorizes a user to use the OSM API" do
72
+ user_email = 'alice@example.com'
73
+ user_password = 'alice'
74
+
75
+ url = 'https://www.onlinescoutmanager.co.uk/users.php?action=authorise'
76
+ post_data = {
77
+ 'apiid' => @api_config[:api_id],
78
+ 'token' => @api_config[:api_token],
79
+ 'email' => user_email,
80
+ 'password' => user_password,
81
+ }
82
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"userid":"id","secret":"secret"}'}) }
83
+
84
+ Osm::Api.new.authorize(user_email, user_password).should == {'userid' => 'id', 'secret' => 'secret'}
85
+ end
86
+
87
+
88
+
89
+ describe "Using the API:" do
90
+ it "Fetches the user's roles" do
91
+ body = [
92
+ {"sectionConfig"=>"{\"subscription_level\":\"3\",\"subscription_expires\":\"2013-01-05\",\"sectionType\":\"cubs\",\"columnNames\":{\"phone1\":\"Home Phone\",\"phone2\":\"Parent 1 Phone\",\"address\":\"Member's Address\",\"phone3\":\"Parent 2 Phone\",\"address2\":\"Address 2\",\"phone4\":\"Alternate Contact Phone\",\"subs\":\"Gender\",\"email1\":\"Parent 1 Email\",\"medical\":\"Medical / Dietary\",\"email2\":\"Parent 2 Email\",\"ethnicity\":\"Gift Aid\",\"email3\":\"Member's Email\",\"religion\":\"Religion\",\"email4\":\"Email 4\",\"school\":\"School\"},\"numscouts\":10,\"hasUsedBadgeRecords\":true,\"hasProgramme\":true,\"extraRecords\":[{\"name\":\"Subs\",\"extraid\":\"529\"}],\"wizard\":\"false\",\"fields\":{\"email1\":true,\"email2\":true,\"email3\":true,\"email4\":false,\"address\":true,\"address2\":false,\"phone1\":true,\"phone2\":true,\"phone3\":true,\"phone4\":true,\"school\":false,\"religion\":true,\"ethnicity\":true,\"medical\":true,\"patrol\":true,\"subs\":true,\"saved\":true},\"intouch\":{\"address\":true,\"address2\":false,\"email1\":false,\"email2\":false,\"email3\":false,\"email4\":false,\"phone1\":true,\"phone2\":true,\"phone3\":true,\"phone4\":true,\"medical\":false},\"mobFields\":{\"email1\":false,\"email2\":false,\"email3\":false,\"email4\":false,\"address\":true,\"address2\":false,\"phone1\":true,\"phone2\":true,\"phone3\":true,\"phone4\":true,\"school\":false,\"religion\":false,\"ethnicity\":true,\"medical\":true,\"patrol\":true,\"subs\":false}}", "groupname"=>"1st Somewhere", "groupid"=>"1", "groupNormalised"=>"1", "sectionid"=>"1", "sectionname"=>"Section 1", "section"=>"cubs", "isDefault"=>"1", "permissions"=>{"badge"=>100, "member"=>100, "user"=>100, "register"=>100, "contact"=>100, "programme"=>100, "originator"=>1, "events"=>100, "finance"=>100, "flexi"=>100}},
93
+ {"sectionConfig"=>"{\"subscription_level\":\"3\",\"subscription_expires\":\"2013-01-05\",\"sectionType\":\"cubs\",\"columnNames\":{\"phone1\":\"Home Phone\",\"phone2\":\"Parent 1 Phone\",\"address\":\"Member's Address\",\"phone3\":\"Parent 2 Phone\",\"address2\":\"Address 2\",\"phone4\":\"Alternate Contact Phone\",\"subs\":\"Gender\",\"email1\":\"Parent 1 Email\",\"medical\":\"Medical / Dietary\",\"email2\":\"Parent 2 Email\",\"ethnicity\":\"Gift Aid\",\"email3\":\"Member's Email\",\"religion\":\"Religion\",\"email4\":\"Email 4\",\"school\":\"School\"},\"numscouts\":10,\"hasUsedBadgeRecords\":true,\"hasProgramme\":true,\"extraRecords\":[],\"wizard\":\"false\",\"fields\":{\"email1\":true,\"email2\":true,\"email3\":true,\"email4\":false,\"address\":true,\"address2\":false,\"phone1\":true,\"phone2\":true,\"phone3\":true,\"phone4\":true,\"school\":false,\"religion\":true,\"ethnicity\":true,\"medical\":true,\"patrol\":true,\"subs\":true,\"saved\":true},\"intouch\":{\"address\":true,\"address2\":false,\"email1\":false,\"email2\":false,\"email3\":false,\"email4\":false,\"phone1\":true,\"phone2\":true,\"phone3\":true,\"phone4\":true,\"medical\":false},\"mobFields\":{\"email1\":false,\"email2\":false,\"email3\":false,\"email4\":false,\"address\":true,\"address2\":false,\"phone1\":true,\"phone2\":true,\"phone3\":true,\"phone4\":true,\"school\":false,\"religion\":false,\"ethnicity\":true,\"medical\":true,\"patrol\":true,\"subs\":false}}", "groupname"=>"1st Somewhere", "groupid"=>"1", "groupNormalised"=>"1", "sectionid"=>"2", "sectionname"=>"Section 2", "section"=>"cubs", "isDefault"=>"0", "permissions"=>{"badge"=>100, "member"=>100, "user"=>100, "register"=>100, "contact"=>100, "programme"=>100, "originator"=>1, "events"=>100, "finance"=>100, "flexi"=>100}}
94
+ ]
95
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getUserRoles", :body => body.to_json)
96
+
97
+ roles = Osm::Api.new('1', '2').get_roles
98
+ roles.size.should == 2
99
+ roles[0].is_a?(Osm::Role).should be_true
100
+ roles[0].section.id.should_not == roles[1].section.id
101
+ roles[0].group_id.should == roles[1].group_id
102
+ end
103
+
104
+
105
+ it "Fetch the user's notepads" do
106
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getNotepads", :body => {"1" => "Section 1", "2" => "Section 2"}.to_json)
107
+ Osm::Api.new('1', '2').get_notepads.should == {1 => 'Section 1', 2 => 'Section 2'}
108
+ end
109
+
110
+
111
+ it "Fetch the notepad for a section" do
112
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getNotepads", :body => {"1" => "Section 1", "2" => "Section 2"}.to_json)
113
+ Osm::Api.new('1', '2').get_notepad(1).should == 'Section 1'
114
+ end
115
+
116
+
117
+ it "Fetch a section's details" do
118
+ body = [
119
+ {"sectionConfig"=>"{\"subscription_level\":\"3\",\"subscription_expires\":\"2013-01-05\",\"sectionType\":\"cubs\",\"columnNames\":{\"phone1\":\"Home Phone\",\"phone2\":\"Parent 1 Phone\",\"address\":\"Member's Address\",\"phone3\":\"Parent 2 Phone\",\"address2\":\"Address 2\",\"phone4\":\"Alternate Contact Phone\",\"subs\":\"Gender\",\"email1\":\"Parent 1 Email\",\"medical\":\"Medical / Dietary\",\"email2\":\"Parent 2 Email\",\"ethnicity\":\"Gift Aid\",\"email3\":\"Member's Email\",\"religion\":\"Religion\",\"email4\":\"Email 4\",\"school\":\"School\"},\"numscouts\":10,\"hasUsedBadgeRecords\":true,\"hasProgramme\":true,\"extraRecords\":[{\"name\":\"Subs\",\"extraid\":\"529\"}],\"wizard\":\"false\",\"fields\":{\"email1\":true,\"email2\":true,\"email3\":true,\"email4\":false,\"address\":true,\"address2\":false,\"phone1\":true,\"phone2\":true,\"phone3\":true,\"phone4\":true,\"school\":false,\"religion\":true,\"ethnicity\":true,\"medical\":true,\"patrol\":true,\"subs\":true,\"saved\":true},\"intouch\":{\"address\":true,\"address2\":false,\"email1\":false,\"email2\":false,\"email3\":false,\"email4\":false,\"phone1\":true,\"phone2\":true,\"phone3\":true,\"phone4\":true,\"medical\":false},\"mobFields\":{\"email1\":false,\"email2\":false,\"email3\":false,\"email4\":false,\"address\":true,\"address2\":false,\"phone1\":true,\"phone2\":true,\"phone3\":true,\"phone4\":true,\"school\":false,\"religion\":false,\"ethnicity\":true,\"medical\":true,\"patrol\":true,\"subs\":false}}", "groupname"=>"1st Somewhere", "groupid"=>"1", "groupNormalised"=>"1", "sectionid"=>"1", "sectionname"=>"Section 1", "section"=>"cubs", "isDefault"=>"1", "permissions"=>{"badge"=>100, "member"=>100, "user"=>100, "register"=>100, "contact"=>100, "programme"=>100, "originator"=>1, "events"=>100, "finance"=>100, "flexi"=>100}},
120
+ {"sectionConfig"=>"{\"subscription_level\":\"3\",\"subscription_expires\":\"2013-01-05\",\"sectionType\":\"cubs\",\"columnNames\":{\"phone1\":\"Home Phone\",\"phone2\":\"Parent 1 Phone\",\"address\":\"Member's Address\",\"phone3\":\"Parent 2 Phone\",\"address2\":\"Address 2\",\"phone4\":\"Alternate Contact Phone\",\"subs\":\"Gender\",\"email1\":\"Parent 1 Email\",\"medical\":\"Medical / Dietary\",\"email2\":\"Parent 2 Email\",\"ethnicity\":\"Gift Aid\",\"email3\":\"Member's Email\",\"religion\":\"Religion\",\"email4\":\"Email 4\",\"school\":\"School\"},\"numscouts\":10,\"hasUsedBadgeRecords\":true,\"hasProgramme\":true,\"extraRecords\":[],\"wizard\":\"false\",\"fields\":{\"email1\":true,\"email2\":true,\"email3\":true,\"email4\":false,\"address\":true,\"address2\":false,\"phone1\":true,\"phone2\":true,\"phone3\":true,\"phone4\":true,\"school\":false,\"religion\":true,\"ethnicity\":true,\"medical\":true,\"patrol\":true,\"subs\":true,\"saved\":true},\"intouch\":{\"address\":true,\"address2\":false,\"email1\":false,\"email2\":false,\"email3\":false,\"email4\":false,\"phone1\":true,\"phone2\":true,\"phone3\":true,\"phone4\":true,\"medical\":false},\"mobFields\":{\"email1\":false,\"email2\":false,\"email3\":false,\"email4\":false,\"address\":true,\"address2\":false,\"phone1\":true,\"phone2\":true,\"phone3\":true,\"phone4\":true,\"school\":false,\"religion\":false,\"ethnicity\":true,\"medical\":true,\"patrol\":true,\"subs\":false}}", "groupname"=>"1st Somewhere", "groupid"=>"1", "groupNormalised"=>"1", "sectionid"=>"2", "sectionname"=>"Section 2", "section"=>"cubs", "isDefault"=>"0", "permissions"=>{"badge"=>100, "member"=>100, "user"=>100, "register"=>100, "contact"=>100, "programme"=>100, "originator"=>1, "events"=>100, "finance"=>100, "flexi"=>100}}
121
+ ]
122
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getUserRoles", :body => body.to_json)
123
+
124
+ section = Osm::Api.new('1', '2').get_section(2)
125
+ section.is_a?(Osm::Section).should be_true
126
+ section.id.should == 2
127
+ end
128
+
129
+
130
+ it "Fetch a section's groupings (sixes, patrols etc.)" do
131
+ body = {"patrols" => [
132
+ {"patrolid" => "101","name" => "Group 1","active" => 1},
133
+ {"patrolid" => "106","name" => "Group 2","active" => 1},
134
+ {"patrolid" => "107","name" => "Group 3","active" => 0},
135
+ ]}
136
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/users.php?action=getPatrols&sectionid=2", :body => body.to_json)
137
+
138
+ groupings = Osm::Api.new('1', '2').get_groupings(2)
139
+ groupings.size.should == 3
140
+ groupings[0].is_a?(Osm::Grouping).should be_true
141
+ end
142
+
143
+
144
+ it "Fetch terms" do
145
+ body = {
146
+ "9" => [
147
+ {"termid" => "1", "name" => "Term 1", "sectionid" => "9", "startdate" => (Date.today + 31).strftime('%Y-%m-%d'), "enddate" => (Date.today + 90).strftime('%Y-%m-%d')}
148
+ ],
149
+ "10" => [
150
+ {"termid" => "2", "name" => "Term 2", "sectionid" => "10", "startdate" => (Date.today + 31).strftime('%Y-%m-%d'), "enddate" => (Date.today + 90).strftime('%Y-%m-%d')},
151
+ {"termid" => "3", "name" => "Term 3", "sectionid" => "10", "startdate" => (Date.today + 91).strftime('%Y-%m-%d'), "enddate" => (Date.today + 180).strftime('%Y-%m-%d')}
152
+ ]
153
+ }
154
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getTerms", :body => body.to_json)
155
+
156
+ terms = Osm::Api.new('1', '2').get_terms
157
+ terms.size.should == 3
158
+ terms[0].is_a?(Osm::Term).should be_true
159
+ end
160
+
161
+
162
+ it "Fetch a term" do
163
+ body = {
164
+ "9" => [
165
+ {"termid" => "1", "name" => "Term 1", "sectionid" => "9", "startdate" => (Date.today + 31).strftime('%Y-%m-%d'), "enddate" => (Date.today + 90).strftime('%Y-%m-%d')}
166
+ ],
167
+ "10" => [
168
+ {"termid" => "2", "name" => "Term 2", "sectionid" => "10", "startdate" => (Date.today + 31).strftime('%Y-%m-%d'), "enddate" => (Date.today + 90).strftime('%Y-%m-%d')},
169
+ {"termid" => "3", "name" => "Term 3", "sectionid" => "10", "startdate" => (Date.today + 91).strftime('%Y-%m-%d'), "enddate" => (Date.today + 180).strftime('%Y-%m-%d')}
170
+ ]
171
+ }
172
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getTerms", :body => body.to_json)
173
+
174
+ term = Osm::Api.new('1', '2').get_term(2)
175
+ term.is_a?(Osm::Term).should be_true
176
+ term.id.should == 2
177
+ end
178
+
179
+
180
+ it "Fetch the term's programme for a section" do
181
+ items = [{"eveningid" => "5", "sectionid" =>"3", "title" => "Weekly Meeting 1", "notesforparents" => "", "games" => "", "prenotes" => "", "postnotes" => "", "leaders" => "", "meetingdate" => "2001-02-03", "starttime" => "19:15:00", "endtime" => "20:30:00", "googlecalendar" => ""}]
182
+ activities = {"5" => [
183
+ {"activityid" => "6", "title" => "Activity 6", "notes" => "", "eveningid" => "5"},
184
+ {"activityid" => "7", "title" => "Activity 7", "notes" => "", "eveningid" => "5"}
185
+ ]}
186
+ body = {"items" => items, "activities" => activities}
187
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/programme.php?action=getProgramme&sectionid=3&termid=4", :body => body.to_json)
188
+
189
+ programme = Osm::Api.new('1', '2').get_programme(3, 4)
190
+ programme.size.should == 1
191
+ programme[0].is_a?(Osm::ProgrammeItem).should be_true
192
+ programme[0].activities.size.should == 2
193
+ end
194
+
195
+
196
+ it "Fetch an activity" do
197
+ body = {
198
+ 'details' => {
199
+ 'activityid' => "1",
200
+ 'version' => '0',
201
+ 'groupid' => '1',
202
+ 'userid' => '1',
203
+ 'title' => "Activity 1",
204
+ 'description' => '',
205
+ 'resources' => '',
206
+ 'instructions' => '',
207
+ 'runningtime' => '',
208
+ 'location' => 'indoors',
209
+ 'shared' => '0',
210
+ 'rating' => '0',
211
+ 'facebook' => ''
212
+ },
213
+ 'editable'=>false,
214
+ 'rating'=>'0',
215
+ 'used'=>'2',
216
+ 'versions' => [
217
+ {
218
+ 'value' => '0',
219
+ 'userid' => '1',
220
+ 'firstname' => 'Alice',
221
+ 'label' => 'Current version - Alice',
222
+ 'selected' => 'selected'
223
+ }
224
+ ],
225
+ 'sections'=> ['beavers', 'cubs', 'scouts', 'explorers'],
226
+ 'tags' => ""
227
+ }
228
+
229
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/programme.php?action=getActivity&id=1", :body => body.to_json)
230
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/programme.php?action=getActivity&id=1&version=0", :body => body.to_json)
231
+
232
+ activity = Osm::Api.new('1', '2').get_activity(1)
233
+ activity.id.should == 1
234
+
235
+ activity0 = Osm::Api.new('1', '2').get_activity(1, 0)
236
+ activity0.id.should == 1
237
+ end
238
+
239
+
240
+ it "Fetch members' details" do
241
+ body = {
242
+ 'identifier' => 'scoutid',
243
+ 'items' => [{
244
+ 'scoutid' => '1', 'sectionid' => '1', 'type' => '',
245
+ 'firstname' => 'John', 'lastname' => 'Doe',
246
+ 'email1' => '', 'email2' => '', 'email3' => '', 'email4' => '',
247
+ 'phone1' => '', 'phone2' => '', 'phone3' => '', 'phone4' => '', 'address' => '', 'address2' => '',
248
+ 'dob' => '2001-02-03', 'started' => '2006-01-01', 'joining_in_yrs' => '-1',
249
+ 'parents' => '', 'notes' => '', 'medical' => '', 'religion' => '', 'school' => '', 'ethnicity' => '',
250
+ 'subs' => '', 'patrolid' => '1', 'patrolleader' => '0', 'joined' => '2006-01-01',
251
+ 'age' => '6 / 0', 'yrs' => '9', 'patrol' => '1'
252
+ }]
253
+ }
254
+
255
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/users.php?action=getUserDetails&sectionid=1&termid=2", :body => body.to_json)
256
+ members = Osm::Api.new('1', '2').get_members(1, 2)
257
+ members[0].id.should == 1
258
+ end
259
+
260
+
261
+ it "Fetch the API Access for a section" do
262
+ body = {
263
+ 'apis' => [{
264
+ 'apiid' => '1',
265
+ 'name' => 'Test API',
266
+ 'permissions' => {}
267
+ }]
268
+ }
269
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/users.php?action=getAPIAccess&sectionid=1", :body => body.to_json)
270
+ apis = Osm::Api.new('1', '2').get_api_access(1)
271
+ apis[0].id.should == 1
272
+ end
273
+
274
+
275
+ it "Fetch our API Access for a section" do
276
+ body = {
277
+ 'apis' => [{
278
+ 'apiid' => '1',
279
+ 'name' => 'Test API',
280
+ 'permissions' => {}
281
+ },{
282
+ 'apiid' => '2',
283
+ 'name' => 'Test API 2',
284
+ 'permissions' => {}
285
+ }]
286
+ }
287
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/users.php?action=getAPIAccess&sectionid=1", :body => body.to_json)
288
+ api = Osm::Api.new('1', '2').get_our_api_access(1)
289
+ api.id.should == 1
290
+ end
291
+
292
+ it "Fetch our API Access for a section (not in returned data)" do
293
+ body = {
294
+ 'apis' => []
295
+ }
296
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/users.php?action=getAPIAccess&sectionid=1", :body => body.to_json)
297
+ api = Osm::Api.new('1', '2').get_our_api_access(1)
298
+ api.should be_nil
299
+ end
300
+
301
+
302
+ it "Fetch events for a section" do
303
+ body = {
304
+ 'identifier' => 'eventid',
305
+ 'label' => 'name',
306
+ 'items' => [{
307
+ 'eventid' => '1',
308
+ 'name' => 'An Event',
309
+ 'startdate' => '2001-02-03',
310
+ 'enddate' => nil,
311
+ 'starttime' => '00:00:00',
312
+ 'endtime' => '00:00:00',
313
+ 'cost' => '0.00',
314
+ 'location' => '',
315
+ 'notes' => '',
316
+ 'sectionid' => 1,
317
+ 'googlecalendar' => nil
318
+ }]
319
+ }
320
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/events.php?action=getEvents&sectionid=1", :body => body.to_json)
321
+ events = Osm::Api.new('1', '2').get_events(1)
322
+ events[0].id.should == 1
323
+ end
324
+
325
+
326
+ it "Fetch due badges for a section" do
327
+ badges_body = []
328
+ roles_body = [{
329
+ "sectionConfig" => "{\"subscription_level\":\"3\",\"subscription_expires\":\"2013-01-05\",\"sectionType\":\"cubs\",\"columnNames\":{\"phone1\":\"Home Phone\",\"phone2\":\"Parent 1 Phone\",\"address\":\"Member\'s Address\",\"phone3\":\"Parent 2 Phone\",\"address2\":\"Address 2\",\"phone4\":\"Alternate Contact Phone\",\"subs\":\"Gender\",\"email1\":\"Parent 1 Email\",\"medical\":\"Medical / Dietary\",\"email2\":\"Parent 2 Email\",\"ethnicity\":\"Gift Aid\",\"email3\":\"Member\'s Email\",\"religion\":\"Religion\",\"email4\":\"Email 4\",\"school\":\"School\"},\"numscouts\":10,\"hasUsedBadgeRecords\":true,\"hasProgramme\":true,\"extraRecords\":[{\"name\":\"Subs\",\"extraid\":\"529\"}],\"wizard\":\"false\",\"fields\":{\"email1\":true,\"email2\":true,\"email3\":true,\"email4\":false,\"address\":true,\"address2\":false,\"phone1\":true,\"phone2\":true,\"phone3\":true,\"phone4\":true,\"school\":false,\"religion\":true,\"ethnicity\":true,\"medical\":true,\"patrol\":true,\"subs\":true,\"saved\":true},\"intouch\":{\"address\":true,\"address2\":false,\"email1\":false,\"email2\":false,\"email3\":false,\"email4\":false,\"phone1\":true,\"phone2\":true,\"phone3\":true,\"phone4\":true,\"medical\":false},\"mobFields\":{\"email1\":false,\"email2\":false,\"email3\":false,\"email4\":false,\"address\":true,\"address2\":false,\"phone1\":true,\"phone2\":true,\"phone3\":true,\"phone4\":true,\"school\":false,\"religion\":false,\"ethnicity\":true,\"medical\":true,\"patrol\":true,\"subs\":false}}",
330
+ "groupname" => "1st Somewhere", "groupid" => "1 ","groupNormalised" => "1",
331
+ "sectionid" => "1", "sectionname" => "Section 1", "section" => "cubs", "isDefault" => "1",
332
+ "permissions" => {"badge"=>100,"member"=>100,"user"=>100,"register"=>100,"contact"=>100,"programme"=>100,"originator"=>1,"events"=>100,"finance"=>100,"flexi"=>100}
333
+ }]
334
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getUserRoles", :body => roles_body.to_json)
335
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=outstandingBadges&section=cubs&sectionid=1&termid=2", :body => badges_body.to_json)
336
+
337
+ due_badges = Osm::Api.new('1', '2').get_due_badges(1, 2)
338
+ due_badges.should_not be_nil
339
+ end
340
+
341
+
342
+ it "Fetch the register structure for a section" do
343
+ data = [
344
+ {"rows" => [{"name"=>"First name","field"=>"firstname","width"=>"100px"},{"name"=>"Last name","field"=>"lastname","width"=>"100px"},{"name"=>"Total","field"=>"total","width"=>"60px"}],"noscroll"=>true},
345
+ {"rows" => []}
346
+ ]
347
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/users.php?action=registerStructure&sectionid=1&termid=2", :body => data.to_json)
348
+
349
+ register_structure = Osm::Api.new('1', '2').get_register_structure(1, 2)
350
+ register_structure.is_a?(Array).should be_true
351
+ end
352
+
353
+ it "Fetch the register data for a section" do
354
+ data = {
355
+ 'identifier' => 'scoutid',
356
+ 'label' => "name",
357
+ 'items' => []
358
+ }
359
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/users.php?action=register&sectionid=1&termid=2", :body => data.to_json)
360
+
361
+ register = Osm::Api.new('1', '2').get_register(1, 2)
362
+ register.is_a?(Array).should be_true
363
+ end
364
+
365
+
366
+
367
+ it "Create an evening (succeded)" do
368
+ url = 'https://www.onlinescoutmanager.co.uk/programme.php?action=addActivityToProgramme'
369
+ post_data = {
370
+ 'apiid' => @api_config[:api_id],
371
+ 'token' => @api_config[:api_token],
372
+ 'userid' => 'user',
373
+ 'secret' => 'secret',
374
+ 'meetingdate' => '2000-01-02',
375
+ 'sectionid' => 1,
376
+ 'activityid' => -1,
377
+ }
378
+
379
+ api = Osm::Api.new('user', 'secret')
380
+ api.stub(:get_terms) { [] }
381
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"result":0}'}) }
382
+ api.create_evening(1, Date.new(2000, 1, 2)).should be_true
383
+ end
384
+
385
+ it "Create an evening (failed)" do
386
+ url = 'https://www.onlinescoutmanager.co.uk/programme.php?action=addActivityToProgramme'
387
+ post_data = {
388
+ 'apiid' => @api_config[:api_id],
389
+ 'token' => @api_config[:api_token],
390
+ 'userid' => 'user',
391
+ 'secret' => 'secret',
392
+ 'meetingdate' => '2000-01-02',
393
+ 'sectionid' => 1,
394
+ 'activityid' => -1,
395
+ }
396
+
397
+ api = Osm::Api.new('user', 'secret')
398
+ api.stub(:get_terms) { [] }
399
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"result":1}'}) }
400
+ api.create_evening(1, Date.new(2000, 1, 2)).should be_false
401
+ end
402
+
403
+
404
+ it "Update an evening (succeded)" do
405
+ url = 'https://www.onlinescoutmanager.co.uk/programme.php?action=editEvening'
406
+ post_data = {
407
+ 'apiid' => @api_config[:api_id],
408
+ 'token' => @api_config[:api_token],
409
+ 'userid' => 'user',
410
+ 'secret' => 'secret',
411
+ 'eveningid' => nil, 'sectionid' => nil, 'meetingdate' => nil, 'starttime' => nil,
412
+ 'endtime' => nil, 'title' => 'Unnamed meeting', 'notesforparents' =>'', 'prenotes' => '',
413
+ 'postnotes' => '', 'games' => '', 'leaders' => '', 'activity' => '[]', 'googlecalendar' => '',
414
+ }
415
+ api = Osm::Api.new('user', 'secret')
416
+ api.stub(:get_terms) { [] }
417
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"result":0}'}) }
418
+
419
+ programme_item = Osm::ProgrammeItem.new({}, [])
420
+ api.update_evening(programme_item).should be_true
421
+ end
422
+
423
+ it "Update an evening (failed)" do
424
+ url = 'https://www.onlinescoutmanager.co.uk/programme.php?action=editEvening'
425
+ post_data = {
426
+ 'apiid' => @api_config[:api_id],
427
+ 'token' => @api_config[:api_token],
428
+ 'userid' => 'user',
429
+ 'secret' => 'secret',
430
+ 'eveningid' => nil, 'sectionid' => nil, 'meetingdate' => nil, 'starttime' => nil,
431
+ 'endtime' => nil, 'title' => 'Unnamed meeting', 'notesforparents' =>'', 'prenotes' => '',
432
+ 'postnotes' => '', 'games' => '', 'leaders' => '', 'activity' => '[]', 'googlecalendar' => '',
433
+ }
434
+ api = Osm::Api.new('user', 'secret')
435
+ api.stub(:get_terms) { [] }
436
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"result":1}'}) }
437
+
438
+ programme_item = Osm::ProgrammeItem.new({}, [])
439
+ api.update_evening(programme_item).should be_false
440
+ end
441
+ end
442
+
443
+
444
+ describe "Options Hash" do
445
+ it "Uses the API's user and secret when not passed" do
446
+ url = "https://www.onlinescoutmanager.co.uk/api.php?action=getNotepads"
447
+ post_data = {
448
+ 'apiid' => @api_config[:api_id],
449
+ 'token' => @api_config[:api_token],
450
+ 'userid' => 'user',
451
+ 'secret' => 'secret',
452
+ }
453
+
454
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"1":"Section 1"}'}) }
455
+ Osm::Api.new('user', 'secret').get_notepads.should == {1 => 'Section 1'}
456
+ end
457
+
458
+ it "Uses the user and secret passed in" do
459
+ url = "https://www.onlinescoutmanager.co.uk/api.php?action=getNotepads"
460
+ post_data = {
461
+ 'apiid' => @api_config[:api_id],
462
+ 'token' => @api_config[:api_token],
463
+ 'userid' => 'user',
464
+ 'secret' => 'secret',
465
+ }
466
+
467
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"1":"Section 1"}'}) }
468
+ Osm::Api.new('1', '2').get_notepads(:api_data => {'userid'=>'user', 'secret'=>'secret'}).should == {1 => 'Section 1'}
469
+ end
470
+ end
471
+
472
+
473
+ describe "Caching behaviour:" do
474
+ it "Controls access to items in the cache (forbids if unknown)" do
475
+ api1 = Osm::Api.new('1', 'secret')
476
+ api2 = Osm::Api.new('2', 'secret')
477
+
478
+ body = {"9" => [{"termid" => "1", "name" => "Term 1", "sectionid" => "9", "startdate" => (Date.today + 31).strftime('%Y-%m-%d'), "enddate" => (Date.today + 90).strftime('%Y-%m-%d')}]}
479
+
480
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getTerms", :body => body.to_json)
481
+ terms = api1.get_terms
482
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getTerms", :body => {}.to_json)
483
+ api2.get_term(terms[0].id).should be_nil
484
+ end
485
+
486
+ it "Controls access to items in the cache (allows if known)" do
487
+ api1 = Osm::Api.new('1', 'secret')
488
+ api2 = Osm::Api.new('2', 'secret')
489
+
490
+ body = {"9" => [{"termid" => "1", "name" => "Term 1", "sectionid" => "9", "startdate" => (Date.today + 31).strftime('%Y-%m-%d'), "enddate" => (Date.today + 90).strftime('%Y-%m-%d')}]}
491
+
492
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getTerms", :body => body.to_json)
493
+ terms = api1.get_terms
494
+ api1.get_term(terms[0].id).should_not be_nil
495
+ api2.get_term(terms[0].id).should_not be_nil
496
+ end
497
+
498
+
499
+ it "Fetches from the cache when the cache holds it" do
500
+ url = "https://www.onlinescoutmanager.co.uk/api.php?action=getNotepads"
501
+ post_data = {
502
+ 'apiid' => @api_config[:api_id],
503
+ 'token' => @api_config[:api_token],
504
+ 'userid' => 'user',
505
+ 'secret' => 'secret',
506
+ }
507
+
508
+ # Fetch first time (and 'prime' the cache)
509
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=> {"1" => "Section 1"}.to_json}) }
510
+ Osm::Api.new('user', 'secret').get_notepad(1).should == 'Section 1'
511
+
512
+ # Fetch second time
513
+ HTTParty.should_not_receive(:post)
514
+ Osm::Api.new('user', 'secret').get_notepad(1).should == 'Section 1'
515
+ end
516
+
517
+ it "Doesn't fetch from the cache when told not to" do
518
+ url = "https://www.onlinescoutmanager.co.uk/api.php?action=getNotepads"
519
+ post_data = {
520
+ 'apiid' => @api_config[:api_id],
521
+ 'token' => @api_config[:api_token],
522
+ 'userid' => 'user',
523
+ 'secret' => 'secret',
524
+ }
525
+
526
+ # Fetch first time (and 'prime' the cache)
527
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"1":"Section 1"}'}) }
528
+ Osm::Api.new('user', 'secret').get_notepad(1).should == 'Section 1'
529
+
530
+ # Fetch second time
531
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"1":"New content."}'}) }
532
+ Osm::Api.new('user', 'secret').get_notepad(1, {:no_cache => true}).should == 'New content.'
533
+ end
534
+ end
535
+
536
+
537
+ describe "OSM and Internet error conditions:" do
538
+ it "Raises a connection error if the HTTP status code was not 'OK'" do
539
+ HTTParty.stub(:post) { DummyHttpResult.new(:response=>{:code=>'500'}) }
540
+ expect{ Osm::Api.new.authorize('email@example.com', 'password') }.to raise_error(Osm::ConnectionError, 'HTTP Status code was 500')
541
+ end
542
+
543
+
544
+ it "Raises a connection error if it can't connect to OSM" do
545
+ HTTParty.stub(:post) { raise SocketError }
546
+ expect{ Osm::Api.new.authorize('email@example.com', 'password') }.to raise_error(Osm::ConnectionError, 'A problem occured on the internet.')
547
+ end
548
+
549
+
550
+ it "Raises an error if OSM returns an error (as a hash)" do
551
+ HTTParty.stub(:post) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"error":"Error message"}'}) }
552
+ expect{ Osm::Api.new.authorize('email@example.com', 'password') }.to raise_error(Osm::Error, 'Error message')
553
+ end
554
+
555
+ it "Raises an error if OSM returns an error (as a plain string)" do
556
+ HTTParty.stub(:post) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'Error message'}) }
557
+ expect{ Osm::Api.new.authorize('email@example.com', 'password') }.to raise_error(Osm::Error, 'Error message')
558
+ end
559
+ end
560
+
561
+ end