osm 0.0.26 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. data/CHANGELOG.md +57 -0
  2. data/README.md +13 -7
  3. data/lib/osm.rb +47 -22
  4. data/lib/osm/activity.rb +52 -57
  5. data/lib/osm/api.rb +115 -1031
  6. data/lib/osm/api_access.rb +73 -36
  7. data/lib/osm/due_badges.rb +27 -12
  8. data/lib/osm/evening.rb +118 -55
  9. data/lib/osm/event.rb +275 -17
  10. data/lib/osm/flexi_record.rb +131 -0
  11. data/lib/osm/grouping.rb +37 -15
  12. data/lib/osm/member.rb +100 -41
  13. data/lib/osm/model.rb +95 -0
  14. data/lib/osm/register.rb +177 -0
  15. data/lib/osm/section.rb +163 -71
  16. data/lib/osm/term.rb +135 -21
  17. data/spec/osm/activity_spec.rb +7 -4
  18. data/spec/osm/api_access_spec.rb +44 -36
  19. data/spec/osm/api_spec.rb +32 -1147
  20. data/spec/osm/due_badges_spec.rb +8 -1
  21. data/spec/osm/evening_spec.rb +119 -54
  22. data/spec/osm/event_spec.rb +363 -13
  23. data/spec/osm/flexi_record_spec.rb +128 -0
  24. data/spec/osm/grouping_spec.rb +9 -5
  25. data/spec/osm/member_spec.rb +111 -36
  26. data/spec/osm/model_spec.rb +140 -0
  27. data/spec/osm/osm_spec.rb +7 -31
  28. data/spec/osm/register_spec.rb +103 -0
  29. data/spec/osm/section_spec.rb +208 -92
  30. data/spec/osm/term_spec.rb +164 -28
  31. data/spec/spec_helper.rb +22 -0
  32. data/version.rb +1 -1
  33. metadata +22 -29
  34. data/lib/osm/event_attendance.rb +0 -55
  35. data/lib/osm/flexi_record_data.rb +0 -51
  36. data/lib/osm/flexi_record_field.rb +0 -42
  37. data/lib/osm/register_data.rb +0 -64
  38. data/lib/osm/register_field.rb +0 -42
  39. data/lib/osm/role.rb +0 -133
  40. data/spec/osm/api_strangeness_spec.rb +0 -83
  41. data/spec/osm/event_attendance_spec.rb +0 -34
  42. data/spec/osm/flexi_record_data_spec.rb +0 -40
  43. data/spec/osm/flexi_record_field_spec.rb +0 -23
  44. data/spec/osm/register_data_spec.rb +0 -35
  45. data/spec/osm/register_field_spec.rb +0 -24
  46. data/spec/osm/role_spec.rb +0 -118
@@ -1,8 +1,6 @@
1
1
  module Osm
2
2
 
3
- class Term
4
- include ::ActiveAttr::MassAssignmentSecurity
5
- include ::ActiveAttr::Model
3
+ class Term < Osm::Model
6
4
 
7
5
  # @!attribute [rw] id
8
6
  # @return [Fixnum] the id for the term
@@ -30,34 +28,150 @@ module Osm
30
28
  validates_presence_of :finish
31
29
 
32
30
 
33
- # @!method initialize
34
- # Initialize a new Term
35
- # @param [Hash] attributes the hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)
31
+ # Get the terms that the OSM user can access
32
+ # @param [Osm::Api] api The api to use to make the request
33
+ # @!macro options_get
34
+ # @return [Array<Osm::Term>]
35
+ def self.get_all(api, options={})
36
+ cache_key = ['terms', api.user_id]
37
+
38
+ if !options[:no_cache] && cache_exist?(api, cache_key)
39
+ return cache_read(api, cache_key)
40
+ end
36
41
 
42
+ data = api.perform_query('api.php?action=getTerms')
43
+
44
+ result = Array.new
45
+ data.each_key do |key|
46
+ data[key].each do |term_data|
47
+ term = Osm::Term.new(
48
+ :id => Osm::to_i_or_nil(term_data['termid']),
49
+ :section_id => Osm::to_i_or_nil(term_data['sectionid']),
50
+ :name => term_data['name'],
51
+ :start => Osm::parse_date(term_data['startdate']),
52
+ :finish => Osm::parse_date(term_data['enddate']),
53
+ )
54
+ result.push term
55
+ cache_write(api, ['term', term.id], term)
56
+ end
57
+ end
58
+
59
+ cache_write(api, cache_key, result)
60
+ return result
61
+ end
37
62
 
38
- # Initialize a new Term from api data
39
- # @param [Hash] data the hash of data provided by the API
40
- def self.from_api(data)
41
- new(
42
- :id => Osm::to_i_or_nil(data['termid']),
43
- :section_id => Osm::to_i_or_nil(data['sectionid']),
44
- :name => data['name'],
45
- :start => Osm::parse_date(data['startdate']),
46
- :finish => Osm::parse_date(data['enddate']),
47
- )
63
+ # Get the terms that the OSM user can access for a given section
64
+ # @param [Osm::Api] api The api to use to make the request
65
+ # @param [Fixnum] section the section (or its ID) of the section to get terms for
66
+ # @!macro options_get
67
+ # @return [Array<Osm::Term>, nil] An array of terms or nil if the user can not access that section
68
+ def self.get_for_section(api, section, options={})
69
+ section_id = section.to_i
70
+ return nil unless get_user_permissions(api).keys.include?(section_id)
71
+ return get_all(api, options).select{ |term| term.section_id == section_id }
48
72
  end
49
73
 
50
- # Get the term's data for use with the API
51
- # @return [Hash]
52
- def to_api
53
- {
74
+ # Get a term
75
+ # @param [Osm::Api] The api to use to make the request
76
+ # @param [Fixnum] term_id the id of the required term
77
+ # @!macro options_get
78
+ # @return nil if an error occured or the user does not have access to that term
79
+ # @return [Osm::Section]
80
+ def self.get(api, term_id, options={})
81
+ cache_key = ['term', term_id]
82
+
83
+ if !options[:no_cache] && cache_exist?(api, cache_key)
84
+ return cache_read(api, cache_key)
85
+ end
86
+
87
+ terms = get_all(api, options)
88
+ return nil unless terms.is_a? Array
89
+
90
+ terms.each do |term|
91
+ if term.id == term_id
92
+ return (get_user_permissions(api).keys.include?(term.section_id) ? term : nil)
93
+ end
94
+ end
95
+ return nil
96
+ end
97
+
98
+ # Get the current term for a given section
99
+ # @param [Osm::Api] api The api to use to make the request
100
+ # @param [Osm::Section, Fixnum] section The section (or its ID) to get terms for
101
+ # @!macro options_get
102
+ # @return [Osm::Term, nil] The current term or nil if the user can not access that section
103
+ def self.get_current_term_for_section(api, section, options={})
104
+ section_id = section.to_i
105
+ terms = get_for_section(api, section_id, options)
106
+
107
+ return nil if terms.nil?
108
+ terms.each do |term|
109
+ return term if term.current?
110
+ end
111
+
112
+ raise Error, 'There is no current term for the section.'
113
+ end
114
+
115
+ # Create a term in OSM
116
+ # @param [Osm::Api] api The api to use to make the request
117
+ # @param [Hash] options - the configuration of the new term
118
+ # @option options [Osm::Section, Fixnum] :section (required) section or section_id to add the term to
119
+ # @option options [String] :name (required) the name for the term
120
+ # @option options [Date] :start (required) the date for the start of term
121
+ # @option options [Date] :finish (required) the date for the finish of term
122
+ # @return [Boolean] if the operation suceeded or not
123
+ def self.create(api, options={})
124
+ raise ArgumentError, ":section can't be nil" if options[:section].nil?
125
+ raise ArgumentError, ":name can't be nil" if options[:name].nil?
126
+ raise ArgumentError, ":start can't be nil" if options[:start].nil?
127
+ raise ArgumentError, ":finish can't be nil" if options[:finish].nil?
128
+
129
+ api_data = {
130
+ 'term' => options[:name],
131
+ 'start' => options[:start].strftime(Osm::OSM_DATE_FORMAT),
132
+ 'end' => options[:finish].strftime(Osm::OSM_DATE_FORMAT),
133
+ 'termid' => '0'
134
+ }
135
+
136
+ data = api.perform_query("users.php?action=addTerm&sectionid=#{options[:section].to_i}", api_data)
137
+
138
+ # The cached terms for the section will be out of date - remove them
139
+ get_all(api, options).each do |term|
140
+ cache_delete(api, ['term', term.id]) if term.section_id == section_id
141
+ end
142
+ cache_delete(api, ['terms', api.user_id])
143
+
144
+ return data.is_a?(Hash) && data['terms'].is_a?(Hash)
145
+ end
146
+
147
+
148
+ # Update a term in OSM
149
+ # @param [Osm::Api] The api to use to make the request
150
+ # @return [Boolean] if the operation suceeded or not
151
+ def update(api)
152
+ raise ObjectIsInvalid, 'term is invalid' unless valid?
153
+
154
+ data = api.perform_query("users.php?action=addTerm&sectionid=#{section_id}", {
54
155
  'term' => name,
55
156
  'start' => start.strftime(Osm::OSM_DATE_FORMAT),
56
157
  'end' => finish.strftime(Osm::OSM_DATE_FORMAT),
57
158
  'termid' => id
58
- }
159
+ })
160
+
161
+ # The cached terms for the section will be out of date - remove them
162
+ self.class.cache_delete(api, ['term', id])
163
+ self.class.cache_delete(api, ['terms', api.user_id])
164
+
165
+ return data.is_a?(Hash) && data['terms'].is_a?(Hash)
59
166
  end
60
167
 
168
+
169
+
170
+ # @!method initialize
171
+ # Initialize a new Term
172
+ # @param [Hash] attributes the hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)
173
+
174
+
61
175
  # Determine if the term is completly before the passed date
62
176
  # @param [Date] date
63
177
  # @return [Boolean] if the term is completly before the passed date
@@ -1,10 +1,10 @@
1
1
  # encoding: utf-8
2
2
  require 'spec_helper'
3
3
 
4
- describe "Activity" do
4
+ describe "Using The API" do
5
5
 
6
- it "Create from API data" do
7
- data = {
6
+ it "Get One" do
7
+ body = {
8
8
  'details' => {
9
9
  'activityid' => '1',
10
10
  'version' => '0',
@@ -53,7 +53,10 @@ describe "Activity" do
53
53
  }
54
54
  ]
55
55
  }
56
- activity = Osm::Activity.from_api(data)
56
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/programme.php?action=getActivity&id=1", :body => body.to_json)
57
+
58
+
59
+ activity = Osm::Activity.get(@api, 1)
57
60
 
58
61
  activity.id.should == 1
59
62
  activity.version.should == 0
@@ -4,52 +4,60 @@ require 'spec_helper'
4
4
 
5
5
  describe "API Access" do
6
6
 
7
- it "Create from API data" do
7
+ it "Create" do
8
8
  data = {
9
- 'apiid' => '1',
10
- 'name' => 'Name',
11
- 'permissions' => {'permission' => '10'},
9
+ :id => 1,
10
+ :name => 'Name',
11
+ :permissions => {:permission => [:read]},
12
12
  }
13
- api_access = Osm::ApiAccess.from_api(data)
13
+ api_access = Osm::ApiAccess.new(data)
14
14
 
15
15
  api_access.id.should == 1
16
16
  api_access.name.should == 'Name'
17
- api_access.permissions.should == {:permission => 10}
17
+ api_access.permissions.should == {:permission => [:read]}
18
18
  api_access.valid?.should be_true
19
19
  end
20
20
 
21
21
 
22
- it "Allows interegation of the permissions hash" do
23
- api_access = Osm::ApiAccess.new({
24
- :id => 1,
25
- :name => 'Name',
26
- :permissions => {
27
- :read_only => 10,
28
- :read_write => 20,
29
- },
30
- })
31
-
32
- api_access.can_read?(:read_only).should == true
33
- api_access.can_read?(:read_write).should == true
34
-
35
- api_access.can_write?(:read_only).should == false
36
- api_access.can_write?(:read_write).should == true
37
-
38
- api_access.can_read?(:non_existant).should == false
39
- api_access.can_write?(:non_existant).should == false
40
- end
41
-
42
-
43
- it "Tells us if it's the our api" do
44
- Osm::Api.stub(:api_id) { '1' }
45
-
46
- apis = {
47
- :ours => Osm::ApiAccess.new({:id => 1, :name => 'Name', :permissions => {}}),
48
- :not_ours => Osm::ApiAccess.new({:id => 2, :name => 'Name', :permissions => {}}),
49
- }
22
+ describe "Using the API" do
23
+
24
+ before :each do
25
+ body = {
26
+ 'apis' => [
27
+ {
28
+ 'apiid' => '1',
29
+ 'name' => 'API Name',
30
+ 'permissions' => { 'read' => '10', 'readwrite' => '20' }
31
+ }, {
32
+ 'apiid' => '2',
33
+ 'name' => 'API 2 Name',
34
+ 'permissions' => { 'read' => '10', 'readwrite' => '20' }
35
+ }
36
+ ]
37
+ }
38
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/users.php?action=getAPIAccess&sectionid=1", :body => body.to_json)
39
+ end
40
+
41
+ it "Get All" do
42
+ api_accesses = Osm::ApiAccess.get_all(@api, 1)
43
+
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]}
49
+ end
50
+
51
+ it "Get One" do
52
+ api_access = Osm::ApiAccess.get(@api, 1, 2)
53
+ api_access.id.should == 2
54
+ end
55
+
56
+ it "Get Ours" do
57
+ api_access = Osm::ApiAccess.get_ours(@api, 1)
58
+ api_access.id.should == 1
59
+ end
50
60
 
51
- apis[:ours].our_api?.should == true
52
- apis[:not_ours].our_api?.should == false
53
61
  end
54
62
 
55
63
  end
@@ -25,47 +25,35 @@ end
25
25
 
26
26
  describe "API" do
27
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
- :cache => OsmTest::Cache,
35
- }.freeze
36
- Osm::Api.configure(@api_config)
37
- end
38
28
 
39
29
  it "Create" do
40
- api = Osm::Api.new
41
-
42
- api.nil?.should == false
43
- Osm::Api.api_id.should == @api_config[:api_id]
44
- Osm::Api.api_name.should == @api_config[:api_name]
30
+ @api.should_not be_nil
31
+ @api.api_id.should == @CONFIGURATION[:api][:osm][:id]
32
+ @api.api_name.should == @CONFIGURATION[:api][:osm][:name]
45
33
  end
46
34
 
47
- it "Raises errors on bad arguents to configure" do
48
- # Missing options
49
- expect{ Osm::Api.configure(@api_config.select{ |k,v| (k != :api_id )}) }.to raise_error(ArgumentError, ':api_id does not exist in options hash')
50
- expect{ Osm::Api.configure(@api_config.select{ |k,v| (k != :api_token)}) }.to raise_error(ArgumentError, ':api_token does not exist in options hash')
51
- expect{ Osm::Api.configure(@api_config.select{ |k,v| (k != :api_name)}) }.to raise_error(ArgumentError, ':api_name does not exist in options hash')
52
- 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')
53
-
54
- # Invalid site
55
- 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')
56
-
57
- # Invalid default_cache_ttl
58
- expect{ Osm::Api.configure(@api_config.merge(:default_cache_ttl => -1)) }.to raise_error(ArgumentError, ':default_cache_ttl must be greater than 0')
59
- expect{ Osm::Api.configure(@api_config.merge(:default_cache_ttl => 'invalid')) }.to raise_error(ArgumentError, ':default_cache_ttl must be greater than 0')
35
+ it "Raises errors on bad arguments to configure" do
36
+ expect{ Osm::Api.configure(@CONFIGURATION[:api].select{ |k,v| (k != :default_site)}) }.to raise_error(ArgumentError, ':default_site does not exist in options hash or is invalid, this should be set to either :osm or :ogm')
37
+ expect{ Osm::Api.configure(@CONFIGURATION[:api].merge(:default_site => :invalid)) }.to raise_error(ArgumentError, ':default_site does not exist in options hash or is invalid, this should be set to either :osm or :ogm')
38
+
39
+ expect{ Osm::Api.configure(@CONFIGURATION[:api].select{ |k,v| (k != :ogm) && (k != :osm)}) }.to raise_error(ArgumentError, ':osm and/or :ogm must be present')
40
+ expect{ Osm::Api.configure(@CONFIGURATION[:api].merge(:osm => '')) }.to raise_error(ArgumentError, ':osm must be a Hash')
41
+ expect{ Osm::Api.configure(@CONFIGURATION[:api].merge(:osm => @CONFIGURATION[:api][:osm].select{ |k,v| (k != :id)})) }.to raise_error(ArgumentError, ':osm must contain a key :id')
42
+ expect{ Osm::Api.configure(@CONFIGURATION[:api].merge(:osm => @CONFIGURATION[:api][:osm].select{ |k,v| (k != :token)})) }.to raise_error(ArgumentError, ':osm must contain a key :token')
43
+ expect{ Osm::Api.configure(@CONFIGURATION[:api].merge(:osm => @CONFIGURATION[:api][:osm].select{ |k,v| (k != :name)})) }.to raise_error(ArgumentError, ':osm must contain a key :name')
44
+ expect{ Osm::Api.configure(@CONFIGURATION[:api].merge(:ogm => '')) }.to raise_error(ArgumentError, ':ogm must be a Hash')
45
+ expect{ Osm::Api.configure(@CONFIGURATION[:api].merge(:ogm => @CONFIGURATION[:api][:ogm].select{ |k,v| (k != :id)})) }.to raise_error(ArgumentError, ':ogm must contain a key :id')
46
+ expect{ Osm::Api.configure(@CONFIGURATION[:api].merge(:ogm => @CONFIGURATION[:api][:ogm].select{ |k,v| (k != :token)})) }.to raise_error(ArgumentError, ':ogm must contain a key :token')
47
+ expect{ Osm::Api.configure(@CONFIGURATION[:api].merge(:ogm => @CONFIGURATION[:api][:ogm].select{ |k,v| (k != :name)})) }.to raise_error(ArgumentError, ':ogm must contain a key :name')
60
48
  end
61
49
 
62
50
 
63
51
  it "Raises errors on bad arguments to create" do
64
- # Both userid and secret (or neither) must be passed
65
- expect{ Osm::Api.new('1') }.to raise_error(ArgumentError, 'You must pass a secret if you are passing a userid')
66
- expect{ Osm::Api.new(nil, '1') }.to raise_error(ArgumentError, 'You must pass a userid if you are passing a secret')
52
+ # Both userid and secret must be passed
53
+ expect{ Osm::Api.new('1', nil) }.to raise_error(ArgumentError, 'You must pass a secret (get this by using the authorize method)')
54
+ expect{ Osm::Api.new(nil, '1') }.to raise_error(ArgumentError, 'You must pass a user_id (get this by using the authorize method)')
67
55
 
68
- expect{ Osm::Api.new('1', '2', :invalid_site) }.to raise_error(ArgumentError, 'site is invalid, if passed it should be either :scout or :guide')
56
+ expect{ Osm::Api.new('1', '2', :invalid_site) }.to raise_error(ArgumentError, 'site is invalid, if passed it should be either :osm or :ogm, if not passed then you forgot to run Api.configure')
69
57
  end
70
58
 
71
59
 
@@ -75,1154 +63,51 @@ describe "API" do
75
63
 
76
64
  url = 'https://www.onlinescoutmanager.co.uk/users.php?action=authorise'
77
65
  post_data = {
78
- 'apiid' => @api_config[:api_id],
79
- 'token' => @api_config[:api_token],
66
+ 'apiid' => @CONFIGURATION[:api][:osm][:id],
67
+ 'token' => @CONFIGURATION[:api][:osm][:token],
80
68
  'email' => user_email,
81
69
  'password' => user_password,
82
70
  }
83
71
  HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"userid":"id","secret":"secret"}'}) }
84
72
 
85
- Osm::Api.new.authorize(user_email, user_password).should == {'userid' => 'id', 'secret' => 'secret'}
73
+ Osm::Api.authorize(user_email, user_password).should == {:user_id => 'id', :secret => 'secret'}
86
74
  end
87
75
 
88
76
  it "sets a new API user" do
89
- api = Osm::Api.new
90
- api.set_user('1', '2')
77
+ @api.set_user('1', '2').is_a?(Osm::Api).should be_true
91
78
 
92
- HTTParty.should_receive(:post).with("https://www.onlinescoutmanager.co.uk/api.php?action=getUserRoles", {:body => {
93
- 'apiid' => @api_config[:api_id],
94
- 'token' => @api_config[:api_token],
79
+ HTTParty.should_receive(:post).with("https://www.onlinescoutmanager.co.uk/test", {:body => {
80
+ 'apiid' => @CONFIGURATION[:api][:osm][:id],
81
+ 'token' => @CONFIGURATION[:api][:osm][:token],
95
82
  'userid' => '1',
96
83
  'secret' => '2',
97
84
  }}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'[]'}) }
98
- api.get_roles
85
+ @api.perform_query('test')
99
86
  end
100
87
 
101
88
 
102
- describe "Using the API:" do
103
- it "Fetches the user's roles" do
104
- body = [
105
- {"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}},
106
- {"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}}
107
- ]
108
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getUserRoles", :body => body.to_json)
109
-
110
- roles = Osm::Api.new('1', '2').get_roles
111
- roles.size.should == 2
112
- roles[0].is_a?(Osm::Role).should be_true
113
- roles[0].section.id.should_not == roles[1].section.id
114
- roles[0].group_id.should == roles[1].group_id
115
- end
116
-
117
-
118
- it "Fetch the user's notepads" do
119
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getNotepads", :body => {"1" => "Section 1", "2" => "Section 2"}.to_json)
120
- Osm::Api.new('1', '2').get_notepads.should == {1 => 'Section 1', 2 => 'Section 2'}
121
- end
122
-
123
-
124
- it "Fetch the notepad for a section" do
125
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getNotepads", :body => {"1" => "Section 1", "2" => "Section 2"}.to_json)
126
- Osm::Api.new('1', '2').get_notepad(1).should == 'Section 1'
127
- Osm::Api.new('1', '2').get_notepad(Osm::Section.new(:id => 2)).should == 'Section 2'
128
- end
129
-
130
- it "Fetch the notepad for a section (invalid section/id)" do
131
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getNotepads", :body => {"1" => "Section 1"}.to_json)
132
-
133
- expect{ Osm::Api.new('1', '2').get_notepad(-10) }.to raise_error(ArgumentError, 'Invalid section ID')
134
- expect{ Osm::Api.new('1', '2').get_notepad(0) }.to raise_error(ArgumentError, 'Invalid section ID')
135
-
136
- expect{ Osm::Api.new('1', '2').get_notepad('1') }.to raise_error(ArgumentError, 'Invalid type for section')
137
- end
138
-
139
-
140
- it "Fetch a section's details" do
141
- body = [
142
- {"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}},
143
- {"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}}
144
- ]
145
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getUserRoles", :body => body.to_json)
146
-
147
- section = Osm::Api.new('1', '2').get_section(2)
148
- section.is_a?(Osm::Section).should be_true
149
- section.id.should == 2
150
- end
151
-
152
-
153
- it "Fetch a section's groupings (sixes, patrols etc.)" do
154
- body = {"patrols" => [
155
- {"patrolid" => "101","name" => "Group 1","active" => 1},
156
- {"patrolid" => "106","name" => "Group 2","active" => 1},
157
- {"patrolid" => "107","name" => "Group 3","active" => 0},
158
- ]}
159
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/users.php?action=getPatrols&sectionid=2", :body => body.to_json)
160
-
161
- groupings = Osm::Api.new('1', '2').get_groupings(2)
162
- groupings.size.should == 3
163
- groupings[0].is_a?(Osm::Grouping).should be_true
164
- end
165
-
166
-
167
- it "Fetch terms" do
168
- body = {
169
- "9" => [
170
- {"termid" => "1", "name" => "Term 1", "sectionid" => "9", "startdate" => (Date.today + 31).strftime('%Y-%m-%d'), "enddate" => (Date.today + 90).strftime('%Y-%m-%d')}
171
- ],
172
- "10" => [
173
- {"termid" => "2", "name" => "Term 2", "sectionid" => "10", "startdate" => (Date.today + 31).strftime('%Y-%m-%d'), "enddate" => (Date.today + 90).strftime('%Y-%m-%d')},
174
- {"termid" => "3", "name" => "Term 3", "sectionid" => "10", "startdate" => (Date.today + 91).strftime('%Y-%m-%d'), "enddate" => (Date.today + 180).strftime('%Y-%m-%d')}
175
- ]
176
- }
177
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getTerms", :body => body.to_json)
178
-
179
- terms = Osm::Api.new('1', '2').get_terms
180
- terms.size.should == 3
181
- terms[0].is_a?(Osm::Term).should be_true
182
- end
183
-
184
-
185
- it "Fetch a term" do
186
- body = {
187
- "9" => [
188
- {"termid" => "1", "name" => "Term 1", "sectionid" => "9", "startdate" => (Date.today + 31).strftime('%Y-%m-%d'), "enddate" => (Date.today + 90).strftime('%Y-%m-%d')}
189
- ],
190
- "10" => [
191
- {"termid" => "2", "name" => "Term 2", "sectionid" => "10", "startdate" => (Date.today + 31).strftime('%Y-%m-%d'), "enddate" => (Date.today + 90).strftime('%Y-%m-%d')},
192
- {"termid" => "3", "name" => "Term 3", "sectionid" => "10", "startdate" => (Date.today + 91).strftime('%Y-%m-%d'), "enddate" => (Date.today + 180).strftime('%Y-%m-%d')}
193
- ]
194
- }
195
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getTerms", :body => body.to_json)
196
-
197
- term = Osm::Api.new('1', '2').get_term(2)
198
- term.is_a?(Osm::Term).should be_true
199
- term.id.should == 2
200
- end
201
-
202
-
203
- it "Fetch the term's programme for a section" do
204
- 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" => ""}]
205
- activities = {"5" => [
206
- {"activityid" => "6", "title" => "Activity 6", "notes" => "", "eveningid" => "5"},
207
- {"activityid" => "7", "title" => "Activity 7", "notes" => "", "eveningid" => "5"}
208
- ]}
209
- body = {"items" => items, "activities" => activities}
210
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/programme.php?action=getProgramme&sectionid=3&termid=4", :body => body.to_json)
211
-
212
- programme = Osm::Api.new('1', '2').get_programme(3, 4)
213
- programme.size.should == 1
214
- programme[0].is_a?(Osm::Evening).should be_true
215
- programme[0].activities.size.should == 2
216
- end
217
-
218
-
219
- it "Fetch an activity" do
220
- body = {
221
- 'details' => {
222
- 'activityid' => "1",
223
- 'version' => '0',
224
- 'groupid' => '1',
225
- 'userid' => '1',
226
- 'title' => "Activity 1",
227
- 'description' => '',
228
- 'resources' => '',
229
- 'instructions' => '',
230
- 'runningtime' => '',
231
- 'location' => 'indoors',
232
- 'shared' => '0',
233
- 'rating' => '0',
234
- 'facebook' => ''
235
- },
236
- 'editable'=>false,
237
- 'rating'=>'0',
238
- 'used'=>'2',
239
- 'versions' => [
240
- {
241
- 'value' => '0',
242
- 'userid' => '1',
243
- 'firstname' => 'Alice',
244
- 'label' => 'Current version - Alice',
245
- 'selected' => 'selected'
246
- }
247
- ],
248
- 'sections'=> ['beavers', 'cubs', 'scouts', 'explorers'],
249
- 'tags' => ""
250
- }
251
-
252
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/programme.php?action=getActivity&id=1", :body => body.to_json)
253
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/programme.php?action=getActivity&id=1&version=0", :body => body.to_json)
254
-
255
- activity = Osm::Api.new('1', '2').get_activity(1)
256
- activity.id.should == 1
257
-
258
- activity0 = Osm::Api.new('1', '2').get_activity(1, 0)
259
- activity0.id.should == 1
260
- end
261
-
262
-
263
- it "Fetch members' details" do
264
- body = {
265
- 'identifier' => 'scoutid',
266
- 'items' => [{
267
- 'scoutid' => '1', 'sectionid' => '1', 'type' => '',
268
- 'firstname' => 'John', 'lastname' => 'Doe',
269
- 'email1' => '', 'email2' => '', 'email3' => '', 'email4' => '',
270
- 'phone1' => '', 'phone2' => '', 'phone3' => '', 'phone4' => '', 'address' => '', 'address2' => '',
271
- 'dob' => '2001-02-03', 'started' => '2006-01-01', 'joining_in_yrs' => '-1',
272
- 'parents' => '', 'notes' => '', 'medical' => '', 'religion' => '', 'school' => '', 'ethnicity' => '',
273
- 'subs' => '', 'patrolid' => '1', 'patrolleader' => '0', 'joined' => '2006-01-01',
274
- 'age' => '6 / 0', 'yrs' => '9', 'patrol' => '1'
275
- }]
276
- }
277
-
278
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/users.php?action=getUserDetails&sectionid=1&termid=2", :body => body.to_json)
279
- members = Osm::Api.new('1', '2').get_members(1, 2)
280
- members[0].id.should == 1
281
- end
282
-
283
-
284
- it "Fetch the API Access for a section" do
285
- body = {
286
- 'apis' => [{
287
- 'apiid' => '1',
288
- 'name' => 'Test API',
289
- 'permissions' => {}
290
- }]
291
- }
292
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/users.php?action=getAPIAccess&sectionid=1", :body => body.to_json)
293
- apis = Osm::Api.new('1', '2').get_api_access(1)
294
- apis[0].id.should == 1
295
- end
296
-
297
-
298
- it "Fetch our API Access for a section" do
299
- body = {
300
- 'apis' => [{
301
- 'apiid' => '1',
302
- 'name' => 'Test API',
303
- 'permissions' => {}
304
- },{
305
- 'apiid' => '2',
306
- 'name' => 'Test API 2',
307
- 'permissions' => {}
308
- }]
309
- }
310
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/users.php?action=getAPIAccess&sectionid=1", :body => body.to_json)
311
- api = Osm::Api.new('1', '2').get_our_api_access(1)
312
- api.id.should == 1
313
- end
314
-
315
- it "Fetch our API Access for a section (not in returned data)" do
316
- body = {
317
- 'apis' => []
318
- }
319
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/users.php?action=getAPIAccess&sectionid=1", :body => body.to_json)
320
- api = Osm::Api.new('1', '2').get_our_api_access(1)
321
- api.should be_nil
322
- end
323
-
324
-
325
- it "Fetch events for a section" do
326
- body = {
327
- 'identifier' => 'eventid',
328
- 'label' => 'name',
329
- 'items' => [{
330
- 'eventid' => '1',
331
- 'name' => 'An Event',
332
- 'startdate' => '2001-02-03',
333
- 'enddate' => nil,
334
- 'starttime' => '00:00:00',
335
- 'endtime' => '00:00:00',
336
- 'cost' => '0.00',
337
- 'location' => '',
338
- 'notes' => '',
339
- 'sectionid' => 1,
340
- 'googlecalendar' => nil,
341
- 'archived' => '0'
342
- }]
343
- }
344
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/events.php?action=getEvents&sectionid=1&showArchived=true", :body => body.to_json)
345
- events = Osm::Api.new('1', '2').get_events(1)
346
- events[0].id.should == 1
347
- end
348
-
349
- it "Fetch an event" do
350
- body = {
351
- 'identifier' => 'eventid',
352
- 'label' => 'name',
353
- 'items' => [{
354
- 'eventid' => '2',
355
- 'name' => 'An Event',
356
- 'startdate' => '2001-02-03',
357
- 'enddate' => nil,
358
- 'starttime' => '00:00:00',
359
- 'endtime' => '00:00:00',
360
- 'cost' => '0.00',
361
- 'location' => '',
362
- 'notes' => '',
363
- 'sectionid' => 1,
364
- 'googlecalendar' => nil,
365
- 'archived' => '0'
366
- }]
367
- }
368
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/events.php?action=getEvents&sectionid=1&showArchived=true", :body => body.to_json)
369
- event = Osm::Api.new('1', '2').get_event(1, 2)
370
- event.id.should == 2
371
- end
372
-
373
- it "Fetch an event's fields" do
374
- events_body = {
375
- 'identifier' => 'eventid',
376
- 'label' => 'name',
377
- 'items' => [{
378
- 'eventid' => '2',
379
- 'name' => 'An Event',
380
- 'startdate' => '2001-02-03',
381
- 'enddate' => nil,
382
- 'starttime' => '00:00:00',
383
- 'endtime' => '00:00:00',
384
- 'cost' => '0.00',
385
- 'location' => '',
386
- 'notes' => '',
387
- 'sectionid' => 1,
388
- 'googlecalendar' => nil,
389
- 'archived' => '0'
390
- }]
391
- }
392
- fields_body = {
393
- 'config' => '[{"id":"f_1","name":"Field 1"}]'
394
- }
395
-
396
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/events.php?action=getEvents&sectionid=1&showArchived=true", :body => events_body.to_json)
397
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/events.php?action=getEvent&sectionid=1&eventid=2", :body => fields_body.to_json)
398
- api = Osm::Api.new('1', '2')
399
-
400
- fields = api.get_event_fields(api.get_event(1, 2), 3)
401
- fields.should == {'f_1' => 'Field 1'}
402
- end
403
-
404
- it "Fetch an event's attendance" do
405
- events_body = {
406
- 'identifier' => 'eventid',
407
- 'label' => 'name',
408
- 'items' => [{
409
- 'eventid' => '2',
410
- 'name' => 'An Event',
411
- 'startdate' => '2001-02-03',
412
- 'enddate' => nil,
413
- 'starttime' => '00:00:00',
414
- 'endtime' => '00:00:00',
415
- 'cost' => '0.00',
416
- 'location' => '',
417
- 'notes' => '',
418
- 'sectionid' => 1,
419
- 'googlecalendar' => nil,
420
- 'archived' => '0'
421
- }]
422
- }
423
- attendance_body = {
424
- 'identifier' => 'scoutid',
425
- 'eventid' => '2',
426
- 'items' => [
427
- {
428
- 'scoutid' => '1',
429
- 'attending' => 'Yes',
430
- 'firstname' => 'First',
431
- 'lastname' => 'Last',
432
- 'dob' => '1980-01-02',
433
- 'patrolid' => '2',
434
- 'f_1' => '',
435
- }
436
- ]
437
- }
438
-
439
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/events.php?action=getEvents&sectionid=1&showArchived=true", :body => events_body.to_json)
440
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/events.php?action=getEventAttendance&eventid=2&sectionid=1&termid=3", :body => attendance_body.to_json)
441
- api = Osm::Api.new('1', '2')
442
-
443
- attendance = api.get_event_attendance(api.get_event(1, 2), 3)
444
- attendance.is_a?(Array).should be_true
445
- end
446
-
447
- it "Update an event's attendance" do
448
- events_body = {
449
- 'identifier' => 'eventid',
450
- 'label' => 'name',
451
- 'items' => [{
452
- 'eventid' => '2',
453
- 'name' => 'An Event',
454
- 'startdate' => '2001-02-03',
455
- 'enddate' => nil,
456
- 'starttime' => '00:00:00',
457
- 'endtime' => '00:00:00',
458
- 'cost' => '0.00',
459
- 'location' => '',
460
- 'notes' => '',
461
- 'sectionid' => 1,
462
- 'googlecalendar' => nil,
463
- 'archived' => '0'
464
- }]
465
- }
466
- attendance_body = {
467
- 'identifier' => 'scoutid',
468
- 'eventid' => '2',
469
- 'items' => [
470
- {
471
- 'scoutid' => '1',
472
- 'attending' => 'Yes',
473
- 'firstname' => 'First',
474
- 'lastname' => 'Last',
475
- 'dob' => '1980-01-02',
476
- 'patrolid' => '2',
477
- 'f_1' => '',
478
- }
479
- ]
480
- }
481
-
482
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/events.php?action=getEvents&sectionid=1&showArchived=true", :body => events_body.to_json)
483
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/events.php?action=getEventAttendance&eventid=2&sectionid=1&termid=3", :body => attendance_body.to_json)
484
-
485
- api = Osm::Api.new('1', '2')
486
- e = api.get_event(1, 2)
487
- ea = api.get_event_attendance(e, 3)[0]
488
- ea.fields['f_1'] = 'TEST'
489
-
490
- HTTParty.should_receive(:post).with(
491
- "https://www.onlinescoutmanager.co.uk/events.php?action=updateScout",
492
- {:body => {
493
- 'scoutid' => 1,
494
- 'column' => 'f_1',
495
- 'value' => 'TEST',
496
- 'sectionid' => 1,
497
- 'row' => 0,
498
- 'eventid' => 2,
499
- 'apiid' => '1',
500
- 'token' => 'API TOKEN',
501
- 'userid' => '1',
502
- 'secret' => '2',
503
- }}
504
- ) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
505
- api.update_event_attendance(e, ea, 'f_1').should be_true
506
- end
507
-
508
- it "Fetch events for a section honoring archived option" do
509
- body = {
510
- 'identifier' => 'eventid',
511
- 'label' => 'name',
512
- 'items' => [{
513
- 'eventid' => '1',
514
- 'name' => 'An Event',
515
- 'startdate' => '2001-02-03',
516
- 'enddate' => nil,
517
- 'starttime' => '00:00:00',
518
- 'endtime' => '00:00:00',
519
- 'cost' => '0.00',
520
- 'location' => '',
521
- 'notes' => '',
522
- 'sectionid' => 1,
523
- 'googlecalendar' => nil,
524
- 'archived' => '0'
525
- },{
526
- 'eventid' => '2',
527
- 'name' => 'An Archived Event',
528
- 'startdate' => '2001-02-03',
529
- 'enddate' => nil,
530
- 'starttime' => '00:00:00',
531
- 'endtime' => '00:00:00',
532
- 'cost' => '0.00',
533
- 'location' => '',
534
- 'notes' => '',
535
- 'sectionid' => 1,
536
- 'googlecalendar' => nil,
537
- 'archived' => '1'
538
- }]
539
- }
540
-
541
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/events.php?action=getEvents&sectionid=1&showArchived=true", :body => body.to_json)
542
- Osm::Api.new('1', '2').get_events(1).size.should == 1
543
- Osm::Api.new('1', '2').get_events(1, {:include_archived => true}).size.should == 2
544
- end
545
-
546
-
547
- it "Fetch due badges for a section" do
548
- badges_body = []
549
- roles_body = [{
550
- "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}}",
551
- "groupname" => "1st Somewhere", "groupid" => "1 ","groupNormalised" => "1",
552
- "sectionid" => "1", "sectionname" => "Section 1", "section" => "cubs", "isDefault" => "1",
553
- "permissions" => {"badge"=>100,"member"=>100,"user"=>100,"register"=>100,"contact"=>100,"programme"=>100,"originator"=>1,"events"=>100,"finance"=>100,"flexi"=>100}
554
- }]
555
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getUserRoles", :body => roles_body.to_json)
556
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=outstandingBadges&section=cubs&sectionid=1&termid=2", :body => badges_body.to_json)
557
-
558
- due_badges = Osm::Api.new('1', '2').get_due_badges(1, 2)
559
- due_badges.should_not be_nil
560
- end
561
-
562
-
563
- it "Fetch badge requirements for an evening" do
564
- badges_body = [{'a'=>'a'},{'a'=>'A'}]
565
- FakeWeb.register_uri(:post, 'https://www.onlinescoutmanager.co.uk/users.php?action=getActivityRequirements&date=2000-01-02&sectionid=3&section=cubs', :body => badges_body.to_json)
566
-
567
- section = Osm::Section.new(:id => 3, :type => :cubs)
568
- evening = Date.new(2000, 1, 2)
569
-
570
- Osm::Api.new('1', '2').get_badge_requirements_for_evening(section, evening).should == badges_body
571
- end
572
-
573
-
574
- it "Fetch badge stock levels for a section" do
575
- badges_body = {
576
- 'stock' => {
577
- 'sectionid' => '1',
578
- 'badge_1' => '1',
579
- 'badge_2' => '2'
580
- }
581
- }
582
-
583
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=getInitialBadges&type=core&sectionid=1&section=test&termid=2", :body => badges_body.to_json)
584
-
585
- section = Osm::Section.new(:id => 1, :type => :test)
586
- term = Osm::Term.new(:id => 2)
587
- stock = Osm::Api.new('1', '2').get_badge_stock_levels(section, term)
588
- stock.should == {'badge_1' => 1, 'badge_2' => 2}
589
- end
590
-
591
- it "Fetch badge stock levels for a section (data from API missing)" do
592
- badges_body = {}
593
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=getInitialBadges&type=core&sectionid=1&section=test&termid=2", :body => badges_body.to_json)
594
-
595
- section = Osm::Section.new(:id => 1, :type => :test)
596
- term = Osm::Term.new(:id => 2)
597
- stock = Osm::Api.new('1', '2').get_badge_stock_levels(section, term)
598
- stock.should == {}
599
- end
600
-
601
- it "Fetch the register structure for a section" do
602
- data = [
603
- {"rows" => [{"name"=>"First name","field"=>"firstname","width"=>"100px"},{"name"=>"Last name","field"=>"lastname","width"=>"100px"},{"name"=>"Total","field"=>"total","width"=>"60px"}],"noscroll"=>true},
604
- {"rows" => []}
605
- ]
606
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/users.php?action=registerStructure&sectionid=1&termid=2", :body => data.to_json)
607
-
608
- register_structure = Osm::Api.new('1', '2').get_register_structure(1, 2)
609
- register_structure.is_a?(Array).should be_true
610
- end
611
-
612
- it "Fetch the register data for a section" do
613
- data = {
614
- 'identifier' => 'scoutid',
615
- 'label' => "name",
616
- 'items' => []
617
- }
618
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/users.php?action=register&sectionid=1&termid=2", :body => data.to_json)
619
-
620
- register = Osm::Api.new('1', '2').get_register_data(1, 2)
621
- register.is_a?(Array).should be_true
622
- end
623
-
624
-
625
- it "Fetch the flexi record fields for a section" do
626
- data = {
627
- "extraid" => "2",
628
- "sectionid" => "1",
629
- "name" => "A Flexi Record",
630
- "config" => "[{\"id\":\"f_1\",\"name\":\"Field 1\",\"width\":\"150\"},{\"id\":\"f_2\",\"name\":\"Field 2\",\"width\":\"150\"}]",
631
- "total" => "none",
632
- "extrafields" => "[]",
633
- "structure" => [
634
- {
635
- "rows" => [
636
- {"name" => "First name","field" => "firstname","width" => "150px"},
637
- {"name" => "Last name","field" => "lastname","width" => "150px"},
638
- ],
639
- "noscroll" => true
640
- },
641
- {"rows" => [
642
- {"name" => "Field 1","field" => "f_1","width" => "150px","editable" => true},
643
- {"name" => "Filed 2","field" => "f_2","width" => "150px","editable" => true},
644
- ]}
645
- ]
646
- }
647
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/extras.php?action=getExtra&sectionid=1&extraid=2", :body => data.to_json)
648
-
649
- fields = Osm::Api.new('1', '2').get_flexi_record_fields(1, 2)
650
- fields.is_a?(Array).should be_true
651
- end
652
-
653
- it "Fetch the flexi record data for a section" do
654
- data = {
655
- 'identifier' => 'scoutid',
656
- 'label' => "name",
657
- 'items' => [{
658
- "scoutid" => "1",
659
- "firstname" => "First",
660
- "lastname" => "Last",
661
- "dob" => "",
662
- "patrolid" => "2",
663
- "total" => "",
664
- "completed" => "",
665
- "f_1" => "A",
666
- "f_2" => "B",
667
- "age" => "",
668
- "patrol" => "Green"
669
- }]
670
- }
671
- 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)
672
-
673
- records = Osm::Api.new('1', '2').get_flexi_record_data(Osm::Section.new(:id => 1, :type => :cubs), 2, 3)
674
- records.is_a?(Array).should be_true
675
- end
676
-
677
-
678
- it "Create an evening (succeded)" do
679
- url = 'https://www.onlinescoutmanager.co.uk/programme.php?action=addActivityToProgramme'
680
- post_data = {
681
- 'apiid' => @api_config[:api_id],
682
- 'token' => @api_config[:api_token],
683
- 'userid' => 'user',
684
- 'secret' => 'secret',
685
- 'meetingdate' => '2000-01-02',
686
- 'sectionid' => 1,
687
- 'activityid' => -1,
688
- }
689
-
690
- api = Osm::Api.new('user', 'secret')
691
- api.stub(:get_terms) { [] }
692
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"result":0}'}) }
693
- api.create_evening(1, Date.new(2000, 1, 2)).should be_true
694
- end
695
-
696
- it "Create an evening (failed)" do
697
- url = 'https://www.onlinescoutmanager.co.uk/programme.php?action=addActivityToProgramme'
698
- post_data = {
699
- 'apiid' => @api_config[:api_id],
700
- 'token' => @api_config[:api_token],
701
- 'userid' => 'user',
702
- 'secret' => 'secret',
703
- 'meetingdate' => '2000-01-02',
704
- 'sectionid' => 1,
705
- 'activityid' => -1,
706
- }
707
-
708
- api = Osm::Api.new('user', 'secret')
709
- api.stub(:get_terms) { [] }
710
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"result":1}'}) }
711
- api.create_evening(1, Date.new(2000, 1, 2)).should be_false
712
- end
713
-
714
-
715
- it "Update an evening (succeded)" do
716
- url = 'https://www.onlinescoutmanager.co.uk/programme.php?action=editEvening'
717
- post_data = {
718
- 'apiid' => @api_config[:api_id],
719
- 'token' => @api_config[:api_token],
720
- 'userid' => 'user',
721
- 'secret' => 'secret',
722
- 'eveningid' => 1, 'sectionid' => 2, 'meetingdate' => '2000-01-02', 'starttime' => nil,
723
- 'endtime' => nil, 'title' => 'Unnamed meeting', 'notesforparents' =>'', 'prenotes' => '',
724
- 'postnotes' => '', 'games' => '', 'leaders' => '', 'activity' => '[]',
725
- }
726
- api = Osm::Api.new('user', 'secret')
727
- api.stub(:get_terms) { [] }
728
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"result":0}'}) }
729
-
730
- evening = Osm::Evening.new(:id=>1, :section_id=>2, :meeting_date=>Date.new(2000, 01, 02))
731
- api.update_evening(evening).should be_true
732
- end
733
-
734
- it "Update an evening (failed)" do
735
- url = 'https://www.onlinescoutmanager.co.uk/programme.php?action=editEvening'
736
- post_data = {
737
- 'apiid' => @api_config[:api_id],
738
- 'token' => @api_config[:api_token],
739
- 'userid' => 'user',
740
- 'secret' => 'secret',
741
- 'eveningid' => 1, 'sectionid' => 2, 'meetingdate' => '2000-01-02', 'starttime' => nil,
742
- 'endtime' => nil, 'title' => 'Unnamed meeting', 'notesforparents' =>'', 'prenotes' => '',
743
- 'postnotes' => '', 'games' => '', 'leaders' => '', 'activity' => '[]',
744
- }
745
- api = Osm::Api.new('user', 'secret')
746
- api.stub(:get_terms) { [] }
747
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"result":1}'}) }
748
-
749
- evening = Osm::Evening.new(:id=>1, :section_id=>2, :meeting_date=>Date.new(2000, 01, 02))
750
- api.update_evening(evening).should be_false
751
- end
752
-
753
- it "Update an evening (invalid evening)" do
754
- api = Osm::Api.new('user', 'secret')
755
- evening = Osm::Evening.new
756
- expect{ api.update_evening(evening) }.to raise_error(Osm::ArgumentIsInvalid)
757
- end
758
-
759
-
760
- it "Create an event (succeded)" do
761
- url = 'https://www.onlinescoutmanager.co.uk/events.php?action=addEvent&sectionid=1'
762
- post_data = {
763
- 'apiid' => @api_config[:api_id],
764
- 'token' => @api_config[:api_token],
765
- 'userid' => 'user',
766
- 'secret' => 'secret',
767
- 'name' => 'Test event',
768
- 'startdate' => '2000-01-02',
769
- 'enddate' => '2001-02-03',
770
- 'starttime' => '03:04:05',
771
- 'endtime' => '04:05:06',
772
- 'cost' => '1.23',
773
- 'location' => 'Somewhere',
774
- 'notes' => 'none'
775
- }
776
-
777
- api = Osm::Api.new('user', 'secret')
778
- api.stub(:get_events) { [] }
779
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"id":2}'}) }
780
-
781
- api.create_event(Osm::Event.new({
782
- :section_id => 1,
783
- :name => 'Test event',
784
- :start => DateTime.new(2000, 01, 02, 03, 04, 05),
785
- :finish => DateTime.new(2001, 02, 03, 04, 05, 06),
786
- :cost => '1.23',
787
- :location => 'Somewhere',
788
- :notes => 'none'
789
- })).should == 2
790
- end
791
-
792
- it "Create an event (failed)" do
793
- url = 'https://www.onlinescoutmanager.co.uk/events.php?action=addEvent&sectionid=1'
794
- post_data = {
795
- 'apiid' => @api_config[:api_id],
796
- 'token' => @api_config[:api_token],
797
- 'userid' => 'user',
798
- 'secret' => 'secret',
799
- 'name' => 'Test event',
800
- 'startdate' => '2000-01-02',
801
- 'enddate' => '2001-02-03',
802
- 'starttime' => '03:04:05',
803
- 'endtime' => '04:05:06',
804
- 'cost' => '1.23',
805
- 'location' => 'Somewhere',
806
- 'notes' => 'none'
807
- }
808
-
809
- api = Osm::Api.new('user', 'secret')
810
- api.stub(:get_events) { [] }
811
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
812
-
813
- api.create_event(Osm::Event.new({
814
- :section_id => 1,
815
- :name => 'Test event',
816
- :start => DateTime.new(2000, 01, 02, 03, 04, 05),
817
- :finish => DateTime.new(2001, 02, 03, 04, 05, 06),
818
- :cost => '1.23',
819
- :location => 'Somewhere',
820
- :notes => 'none'
821
- })).should be_nil
822
- end
823
-
824
- it "Update an event (succeded)" do
825
- url = 'https://www.onlinescoutmanager.co.uk/events.php?action=addEvent&sectionid=1'
826
- post_data = {
827
- 'apiid' => @api_config[:api_id],
828
- 'token' => @api_config[:api_token],
829
- 'userid' => 'user',
830
- 'secret' => 'secret',
831
- 'name' => 'Test event',
832
- 'startdate' => '2000-01-02',
833
- 'enddate' => '2001-02-03',
834
- 'starttime' => '03:04:05',
835
- 'endtime' => '04:05:06',
836
- 'cost' => '1.23',
837
- 'location' => 'Somewhere',
838
- 'notes' => 'none',
839
- 'eventid' => 2
840
- }
841
-
842
- api = Osm::Api.new('user', 'secret')
843
- api.stub(:get_events) { [] }
844
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"id":2}'}) }
845
-
846
- api.update_event(Osm::Event.new({
847
- :section_id => 1,
848
- :name => 'Test event',
849
- :start => DateTime.new(2000, 01, 02, 03, 04, 05),
850
- :finish => DateTime.new(2001, 02, 03, 04, 05, 06),
851
- :cost => '1.23',
852
- :location => 'Somewhere',
853
- :notes => 'none',
854
- :id => 2
855
- })).should be_true
856
- end
857
-
858
- it "Update an event (failed)" do
859
- url = 'https://www.onlinescoutmanager.co.uk/events.php?action=addEvent&sectionid=1'
860
- post_data = {
861
- 'apiid' => @api_config[:api_id],
862
- 'token' => @api_config[:api_token],
863
- 'userid' => 'user',
864
- 'secret' => 'secret',
865
- 'name' => 'Test event',
866
- 'startdate' => '2000-01-02',
867
- 'enddate' => '2001-02-03',
868
- 'starttime' => '03:04:05',
869
- 'endtime' => '04:05:06',
870
- 'cost' => '1.23',
871
- 'location' => 'Somewhere',
872
- 'notes' => 'none',
873
- 'eventid' => 2
874
- }
875
-
876
- api = Osm::Api.new('user', 'secret')
877
- api.stub(:get_events) { [] }
878
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
879
-
880
- api.update_event(Osm::Event.new({
881
- :section_id => 1,
882
- :name => 'Test event',
883
- :start => DateTime.new(2000, 01, 02, 03, 04, 05),
884
- :finish => DateTime.new(2001, 02, 03, 04, 05, 06),
885
- :cost => '1.23',
886
- :location => 'Somewhere',
887
- :notes => 'none',
888
- :id => 2
889
- })).should be_false
890
- end
891
-
892
- it "Delete an event (succeded)" do
893
- url = 'https://www.onlinescoutmanager.co.uk/events.php?action=deleteEvent&sectionid=1&eventid=2'
894
- post_data = {
895
- 'apiid' => @api_config[:api_id],
896
- 'token' => @api_config[:api_token],
897
- 'userid' => 'user',
898
- 'secret' => 'secret',
899
- }
900
-
901
- api = Osm::Api.new('user', 'secret')
902
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"ok":true}'}) }
903
-
904
- api.delete_event(Osm::Event.new({
905
- :section_id => 1,
906
- :name => 'Test event',
907
- :id => 2
908
- })).should be_true
909
- end
910
-
911
- it "Delete an event (failed)" do
912
- url = 'https://www.onlinescoutmanager.co.uk/events.php?action=deleteEvent&sectionid=1&eventid=2'
913
- post_data = {
914
- 'apiid' => @api_config[:api_id],
915
- 'token' => @api_config[:api_token],
916
- 'userid' => 'user',
917
- 'secret' => 'secret',
918
- }
919
-
920
- api = Osm::Api.new('user', 'secret')
921
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"ok":false}'}) }
922
-
923
- api.delete_event(Osm::Event.new({
924
- :section_id => 1,
925
- :name => 'Test event',
926
- :id => 2
927
- })).should be_false
928
- end
929
-
930
- it "Add field to an event (succeded)" do
931
- url = 'https://www.onlinescoutmanager.co.uk/events.php?action=addColumn&sectionid=1&eventid=2'
932
- post_data = {
933
- 'apiid' => @api_config[:api_id],
934
- 'token' => @api_config[:api_token],
935
- 'userid' => 'user',
936
- 'secret' => 'secret',
937
- 'columnName' => 'Test field',
938
- }
939
-
940
- api = Osm::Api.new('user', 'secret')
941
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"eventid":"2"}'}) }
942
-
943
- api.add_event_field(Osm::Event.new({
944
- :section_id => 1,
945
- :name => 'Test event',
946
- :id => 2
947
- }), 'Test field').should be_true
948
- end
949
-
950
- it "Add field to an event (failed)" do
951
- url = 'https://www.onlinescoutmanager.co.uk/events.php?action=addColumn&sectionid=1&eventid=2'
952
- post_data = {
953
- 'apiid' => @api_config[:api_id],
954
- 'token' => @api_config[:api_token],
955
- 'userid' => 'user',
956
- 'secret' => 'secret',
957
- 'columnName' => 'Test field',
958
- }
959
-
960
- api = Osm::Api.new('user', 'secret')
961
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
962
-
963
- api.add_event_field(Osm::Event.new({
964
- :section_id => 1,
965
- :name => 'Test event',
966
- :id => 2
967
- }), 'Test field').should be_false
968
- end
969
-
970
-
971
- it "Create a term (succeded)" do
972
- url = 'https://www.onlinescoutmanager.co.uk/users.php?action=addTerm&sectionid=1'
973
- post_data = {
974
- 'apiid' => @api_config[:api_id],
975
- 'token' => @api_config[:api_token],
976
- 'userid' => 'user',
977
- 'secret' => 'secret',
978
- 'term' => 'A Term',
979
- 'start' => '2010-01-01',
980
- 'end' => '2010-12-31',
981
- 'termid' => '0'
982
- }
983
-
984
- api = Osm::Api.new('user', 'secret')
985
- api.stub(:get_terms) { [] }
986
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"terms":{}}'}) }
987
-
988
- api.create_term({
989
- :section => 1,
990
- :name => 'A Term',
991
- :start => Date.new(2010, 01, 01),
992
- :finish => Date.new(2010, 12, 31),
993
- }).should be_true
994
- end
995
-
996
- it "Create a term (failed)" do
997
- url = 'https://www.onlinescoutmanager.co.uk/users.php?action=addTerm&sectionid=1'
998
- post_data = {
999
- 'apiid' => @api_config[:api_id],
1000
- 'token' => @api_config[:api_token],
1001
- 'userid' => 'user',
1002
- 'secret' => 'secret',
1003
- 'term' => 'A Term',
1004
- 'start' => '2010-01-01',
1005
- 'end' => '2010-12-31',
1006
- 'termid' => '0'
1007
- }
1008
-
1009
- api = Osm::Api.new('user', 'secret')
1010
- api.stub(:get_terms) { [] }
1011
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
1012
-
1013
- api.create_term({
1014
- :section => 1,
1015
- :name => 'A Term',
1016
- :start => Date.new(2010, 01, 01),
1017
- :finish => Date.new(2010, 12, 31),
1018
- }).should be_false
1019
- end
1020
-
1021
- it "Update register attendance" do
1022
- url = 'https://www.onlinescoutmanager.co.uk/users.php?action=registerUpdate&sectionid=1&termid=2'
1023
- post_data = {
1024
- 'apiid' => @api_config[:api_id],
1025
- 'token' => @api_config[:api_token],
1026
- 'userid' => 'user',
1027
- 'secret' => 'secret',
1028
- 'scouts' => '["3"]',
1029
- 'selectedDate' => '2000-01-02',
1030
- 'present' => 'Yes',
1031
- 'section' => :cubs,
1032
- 'sectionid' => 1,
1033
- 'completedBadges' => '[{"a":"A"},{"b":"B"}]'
1034
- }
1035
-
1036
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'[]'}) }
1037
- Osm::Api.new('user', 'secret').update_register({
1038
- :section => Osm::Section.new(:id=>1, :type=>:cubs),
1039
- :term => 2,
1040
- :evening => Date.new(2000, 1, 2),
1041
- :attendance => 'Yes',
1042
- :members => 3,
1043
- :completed_badge_requirements => [{'a'=>'A'}, {'b'=>'B'}]
1044
- }).should be_true
1045
- end
1046
-
1047
-
1048
- it "Update a term (succeded)" do
1049
- url = 'https://www.onlinescoutmanager.co.uk/users.php?action=addTerm&sectionid=1'
1050
- post_data = {
1051
- 'apiid' => @api_config[:api_id],
1052
- 'token' => @api_config[:api_token],
1053
- 'userid' => 'user',
1054
- 'secret' => 'secret',
1055
- 'term' => 'A Term',
1056
- 'start' => '2010-01-01',
1057
- 'end' => '2010-12-31',
1058
- 'termid' => 2
1059
- }
1060
- api = Osm::Api.new('user', 'secret')
1061
- api.stub(:get_terms) { [] }
1062
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"terms":{}}'}) }
1063
-
1064
- term = Osm::Term.new(:id=>2, :section_id=>1, :name=>'A Term', :start=>Date.new(2010, 01, 01), :finish=>Date.new(2010, 12, 31))
1065
- api.update_term(term).should be_true
1066
- end
1067
-
1068
- it "Update a term (failed)" do
1069
- url = 'https://www.onlinescoutmanager.co.uk/users.php?action=addTerm&sectionid=1'
1070
- post_data = {
1071
- 'apiid' => @api_config[:api_id],
1072
- 'token' => @api_config[:api_token],
1073
- 'userid' => 'user',
1074
- 'secret' => 'secret',
1075
- 'term' => 'A Term',
1076
- 'start' => '2010-01-01',
1077
- 'end' => '2010-12-31',
1078
- 'termid' => 2
1079
- }
1080
- api = Osm::Api.new('user', 'secret')
1081
- api.stub(:get_terms) { [] }
1082
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
1083
-
1084
- term = Osm::Term.new(:id=>2, :section_id=>1, :name=>'A Term', :start=>Date.new(2010, 01, 01), :finish=>Date.new(2010, 12, 31))
1085
- api.update_term(term).should be_false
1086
- end
1087
-
1088
- it "Update a term (invalid term)" do
1089
- api = Osm::Api.new('user', 'secret')
1090
- term = Osm::Term.new
1091
- expect{ api.update_term(term) }.to raise_error(Osm::ArgumentIsInvalid)
1092
- end
1093
-
1094
- end
1095
-
1096
-
1097
- describe "Options Hash" do
1098
- it "Uses the API's user and secret when not passed" do
1099
- url = "https://www.onlinescoutmanager.co.uk/api.php?action=getNotepads"
1100
- post_data = {
1101
- 'apiid' => @api_config[:api_id],
1102
- 'token' => @api_config[:api_token],
1103
- 'userid' => 'user',
1104
- 'secret' => 'secret',
1105
- }
1106
-
1107
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"1":"Section 1"}'}) }
1108
- Osm::Api.new('user', 'secret').get_notepads.should == {1 => 'Section 1'}
1109
- end
1110
-
1111
- it "Uses the user and secret passed in" do
1112
- url = "https://www.onlinescoutmanager.co.uk/api.php?action=getNotepads"
1113
- post_data = {
1114
- 'apiid' => @api_config[:api_id],
1115
- 'token' => @api_config[:api_token],
1116
- 'userid' => 'user',
1117
- 'secret' => 'secret',
1118
- }
1119
- api = Osm::Api.new('1', '2')
1120
-
1121
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"1":"Section 1"}'}) }
1122
- api.should_receive(:warn).with('[DEPRECATION OF OPTION] use of the api_data option is deprecated.')
1123
-
1124
- api.get_notepads({}, {'userid'=>'user', 'secret'=>'secret'}).should == {1 => 'Section 1'}
1125
- end
1126
- end
1127
-
1128
-
1129
- describe "Caching behaviour:" do
1130
- it "Controls access to items in the cache (forbids if unknown)" do
1131
- api1 = Osm::Api.new('1', 'secret')
1132
- api2 = Osm::Api.new('2', 'secret')
1133
-
1134
- 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')}]}
1135
-
1136
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getTerms", :body => body.to_json)
1137
- terms = api1.get_terms
1138
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getTerms", :body => {}.to_json)
1139
- api2.get_term(terms[0].id).should be_nil
1140
- end
1141
-
1142
- it "Controls access to items in the cache (allows if known)" do
1143
- api1 = Osm::Api.new('1', 'secret')
1144
- api2 = Osm::Api.new('2', 'secret')
1145
-
1146
- 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')}]}
1147
-
1148
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getTerms", :body => body.to_json)
1149
- terms = api1.get_terms
1150
- api1.get_term(terms[0].id).should_not be_nil
1151
- api2.get_term(terms[0].id).should_not be_nil
1152
- end
1153
-
1154
-
1155
- it "Fetches from the cache when the cache holds it" do
1156
- url = "https://www.onlinescoutmanager.co.uk/api.php?action=getNotepads"
1157
- post_data = {
1158
- 'apiid' => @api_config[:api_id],
1159
- 'token' => @api_config[:api_token],
1160
- 'userid' => 'user',
1161
- 'secret' => 'secret',
1162
- }
1163
-
1164
- # Fetch first time (and 'prime' the cache)
1165
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=> {"1" => "Section 1"}.to_json}) }
1166
- Osm::Api.new('user', 'secret').get_notepad(1).should == 'Section 1'
1167
-
1168
- # Fetch second time
1169
- HTTParty.should_not_receive(:post)
1170
- Osm::Api.new('user', 'secret').get_notepad(1).should == 'Section 1'
1171
- end
1172
-
1173
- it "Doesn't fetch from the cache when told not to" do
1174
- url = "https://www.onlinescoutmanager.co.uk/api.php?action=getNotepads"
1175
- post_data = {
1176
- 'apiid' => @api_config[:api_id],
1177
- 'token' => @api_config[:api_token],
1178
- 'userid' => 'user',
1179
- 'secret' => 'secret',
1180
- }
1181
-
1182
- # Fetch first time (and 'prime' the cache)
1183
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"1":"Section 1"}'}) }
1184
- Osm::Api.new('user', 'secret').get_notepad(1).should == 'Section 1'
1185
-
1186
- # Fetch second time
1187
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"1":"New content."}'}) }
1188
- Osm::Api.new('user', 'secret').get_notepad(1, {:no_cache => true}).should == 'New content.'
1189
- end
1190
-
1191
-
1192
- it "Uses the provided cache_prepend_to_key text" do
1193
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getNotepads", :body => {"1" => "Section 1"}.to_json)
1194
-
1195
- OsmTest::Cache.should_receive('exist?').with('OSMAPI-notepads-1')
1196
- Osm::Api.new('1', '2').get_notepads.should == {1 => 'Section 1'}
1197
-
1198
- Osm::Api.configure(@api_config.merge(:cache_prepend_to_key => 'AB'))
1199
- OsmTest::Cache.should_receive('exist?').with('AB-notepads-1')
1200
- Osm::Api.new('1', '2').get_notepads.should == {1 => 'Section 1'}
1201
- end
1202
- end
1203
-
1204
89
 
1205
90
  describe "OSM and Internet error conditions:" do
1206
91
  it "Raises a connection error if the HTTP status code was not 'OK'" do
1207
92
  HTTParty.stub(:post) { DummyHttpResult.new(:response=>{:code=>'500'}) }
1208
- expect{ Osm::Api.new.authorize('email@example.com', 'password') }.to raise_error(Osm::ConnectionError, 'HTTP Status code was 500')
93
+ expect{ Osm::Api.authorize('email@example.com', 'password') }.to raise_error(Osm::ConnectionError, 'HTTP Status code was 500')
1209
94
  end
1210
95
 
1211
96
 
1212
97
  it "Raises a connection error if it can't connect to OSM" do
1213
98
  HTTParty.stub(:post) { raise SocketError }
1214
- expect{ Osm::Api.new.authorize('email@example.com', 'password') }.to raise_error(Osm::ConnectionError, 'A problem occured on the internet.')
99
+ expect{ Osm::Api.authorize('email@example.com', 'password') }.to raise_error(Osm::ConnectionError, 'A problem occured on the internet.')
1215
100
  end
1216
101
 
1217
102
 
1218
103
  it "Raises an error if OSM returns an error (as a hash)" do
1219
104
  HTTParty.stub(:post) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"error":"Error message"}'}) }
1220
- expect{ Osm::Api.new.authorize('email@example.com', 'password') }.to raise_error(Osm::Error, 'Error message')
105
+ expect{ Osm::Api.authorize('email@example.com', 'password') }.to raise_error(Osm::Error, 'Error message')
1221
106
  end
1222
107
 
1223
108
  it "Raises an error if OSM returns an error (as a plain string)" do
1224
109
  HTTParty.stub(:post) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'Error message'}) }
1225
- expect{ Osm::Api.new.authorize('email@example.com', 'password') }.to raise_error(Osm::Error, 'Error message')
110
+ expect{ Osm::Api.authorize('email@example.com', 'password') }.to raise_error(Osm::Error, 'Error message')
1226
111
  end
1227
112
  end
1228
113