amee 2.6.0 → 2.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/Gemfile +18 -0
  2. data/README +2 -2
  3. data/Rakefile +31 -0
  4. data/VERSION +1 -0
  5. data/amee-ruby.gemspec +147 -0
  6. data/examples/view_profile_item.rb +32 -0
  7. data/lib/amee.rb +0 -1
  8. data/lib/amee/profile_item.rb +27 -6
  9. data/spec/amee_spec.rb +25 -0
  10. data/spec/cache_spec.rb +152 -0
  11. data/spec/connection_spec.rb +295 -0
  12. data/spec/data_category_spec.rb +259 -0
  13. data/spec/data_item_spec.rb +224 -0
  14. data/spec/data_item_value_history_spec.rb +311 -0
  15. data/spec/data_item_value_spec.rb +231 -0
  16. data/spec/data_object_spec.rb +9 -0
  17. data/spec/drill_down_spec.rb +163 -0
  18. data/spec/fixtures/AD63A83B4D41.json +1 -0
  19. data/spec/fixtures/AD63A83B4D41.xml +1 -0
  20. data/spec/fixtures/create_item.json +1 -0
  21. data/spec/fixtures/create_item.xml +1 -0
  22. data/spec/fixtures/data.json +1 -0
  23. data/spec/fixtures/data.xml +1 -0
  24. data/spec/fixtures/data_home_energy_quantity.xml +146 -0
  25. data/spec/fixtures/data_home_energy_quantity_biodiesel.xml +177 -0
  26. data/spec/fixtures/data_transport_car_generic_drill_fuel_diesel.xml +33 -0
  27. data/spec/fixtures/empty.json +1 -0
  28. data/spec/fixtures/empty.xml +1 -0
  29. data/spec/fixtures/parse_test.xml +22 -0
  30. data/spec/fixtures/v0_data_transport_transport_drill_transportType_Car1.xml +20 -0
  31. data/spec/item_definition_spec.rb +313 -0
  32. data/spec/item_value_definition_spec.rb +253 -0
  33. data/spec/logger_spec.rb +16 -0
  34. data/spec/object_spec.rb +44 -0
  35. data/spec/parse_helper_spec.rb +72 -0
  36. data/spec/profile_category_spec.rb +565 -0
  37. data/spec/profile_item_spec.rb +451 -0
  38. data/spec/profile_item_value_spec.rb +196 -0
  39. data/spec/profile_object_spec.rb +24 -0
  40. data/spec/profile_spec.rb +88 -0
  41. data/spec/rails_spec.rb +48 -0
  42. data/spec/spec.opts +2 -0
  43. data/spec/spec_helper.rb +56 -0
  44. data/spec/user_spec.rb +249 -0
  45. metadata +189 -55
  46. data/lib/amee/version.rb +0 -10
@@ -0,0 +1,24 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe AMEE::Profile::Object do
4
+
5
+ it "should have a full path under /profiles" do
6
+ AMEE::Profile::Object.new.full_path.should == "/profiles"
7
+ end
8
+
9
+ it "can have a profile UID" do
10
+ obj = AMEE::Profile::Object.new(:profile_uid => 'ABC123')
11
+ obj.profile_uid.should == "ABC123"
12
+ end
13
+
14
+ it "should create correct path if profile UID is set" do
15
+ obj = AMEE::Profile::Object.new(:profile_uid => 'ABC123')
16
+ obj.full_path.should == "/profiles/ABC123"
17
+ end
18
+
19
+ it "can have a profile date" do
20
+ obj = AMEE::Profile::Object.new(:profile_date => DateTime.new(2008,01))
21
+ obj.profile_date.should == DateTime.new(2008,01)
22
+ end
23
+
24
+ end
@@ -0,0 +1,88 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe AMEE::Profile::Profile do
4
+
5
+ it "should have common AMEE object properties" do
6
+ AMEE::Profile::Profile.new.is_a?(AMEE::Profile::Object).should be_true
7
+ end
8
+
9
+ it "should initialize AMEE::Object data on creation" do
10
+ uid = 'ABCD1234'
11
+ profile = AMEE::Profile::Profile.new(:uid => uid)
12
+ profile.uid.should == uid
13
+ end
14
+
15
+ end
16
+
17
+ describe AMEE::Profile::Profile, "with an authenticated connection" do
18
+
19
+ it "should provide access to list of profiles" do
20
+ connection = flexmock "connection"
21
+ connection.should_receive(:get).with("/profiles", {}).and_return(flexmock(:body => '<?xml version="1.0" encoding="UTF-8"?><Resources><ProfilesResource><Profiles><Profile created="2008-07-24 10:45:08.0" modified="2008-07-24 10:45:08.0" uid="B59C2AA75C7F"><Path/><Name/><Environment uid="5F5887BCF726"/><Permission created="2008-07-24 10:45:08.0" modified="2008-07-24 10:45:08.0" uid="44406616CCAB"><Environment uid="5F5887BCF726"/><Name>amee</Name><Username>floppy</Username></Permission></Profile><Profile created="2008-07-22 13:10:59.0" modified="2008-07-22 13:10:59.0" uid="59D8E437FA70"><Path/><Name/><Environment uid="5F5887BCF726"/><Permission created="2008-07-22 13:10:59.0" modified="2008-07-22 13:10:59.0" uid="EFE285A403D0"><Environment uid="5F5887BCF726"/><Name>amee</Name><Username>floppy</Username></Permission></Profile></Profiles><Pager><Start>0</Start><From>1</From><To>2</To><Items>2</Items><CurrentPage>1</CurrentPage><RequestedPage>1</RequestedPage><NextPage>-1</NextPage><PreviousPage>-1</PreviousPage><LastPage>1</LastPage><ItemsPerPage>10</ItemsPerPage><ItemsFound>2</ItemsFound></Pager></ProfilesResource></Resources>'))
22
+ profiles = AMEE::Profile::Profile.list(connection)
23
+ profiles.size.should be(2)
24
+ end
25
+
26
+ it "should fail gracefully with incorrect profile list data" do
27
+ connection = flexmock "connection"
28
+ json = '{}'
29
+ connection.should_receive(:get).with("/profiles", {}).and_return(flexmock(:body => json))
30
+ lambda{AMEE::Profile::Profile.list(connection)}.should raise_error(AMEE::BadData)
31
+ end
32
+
33
+ it "should parse JSON data correctly" do
34
+ connection = flexmock "connection"
35
+ connection.should_receive(:get).with("/profiles", {}).and_return(flexmock(:body => '{"pager":{"to":1,"lastPage":1,"start":0,"nextPage":-1,"items":1,"itemsPerPage":10,"from":1,"previousPage":-1,"requestedPage":1,"currentPage":1,"itemsFound":1},"profiles":[{"modified":"2008-07-24 10:50:23.0","created":"2008-07-24 10:49:23.0","uid":"A508956A847F","permission":{"modified":"2008-07-24 10:49:23.0","created":"2008-07-24 10:49:23.0","user":{"uid":"1A6307E2B531","username":"floppy"},"group":{"uid":"AC65FFA5F9D9","name":"amee"},"environmentUid":"5F5887BCF726","uid":"787915F05BBD"},"environment":{"uid":"5F5887BCF726"},"path":"A508956A847F","name":"A508956A847F"}]}'))
36
+ profile = AMEE::Profile::Profile.list(connection)[0]
37
+ profile.uid.should == "A508956A847F"
38
+ profile.name.should == "A508956A847F"
39
+ profile.path.should == "/A508956A847F"
40
+ profile.full_path.should == "/profiles/A508956A847F"
41
+ profile.created.should == DateTime.new(2008,7,24,10,49,23)
42
+ profile.modified.should == DateTime.new(2008,7,24,10,50,23)
43
+ end
44
+
45
+ it "should parse XML data correctly" do
46
+ connection = flexmock "connection"
47
+ connection.should_receive(:get).with("/profiles", {}).and_return(flexmock(:body => '<?xml version="1.0" encoding="UTF-8"?><Resources><ProfilesResource><Profiles><Profile created="2008-07-24 10:45:08.0" modified="2008-07-24 10:55:08.0" uid="B59C2AA75C7F"><Path/><Name/><Environment uid="5F5887BCF726"/><Permission created="2008-07-24 10:45:08.0" modified="2008-07-24 10:45:08.0" uid="44406616CCAB"><Environment uid="5F5887BCF726"/><Name>amee</Name><Username>floppy</Username></Permission></Profile><Profile created="2008-07-22 13:10:59.0" modified="2008-07-22 13:10:59.0" uid="59D8E437FA70"><Path/><Name/><Environment uid="5F5887BCF726"/><Permission created="2008-07-22 13:10:59.0" modified="2008-07-22 13:10:59.0" uid="EFE285A403D0"><Environment uid="5F5887BCF726"/><Name>amee</Name><Username>floppy</Username></Permission></Profile></Profiles><Pager><Start>0</Start><From>1</From><To>2</To><Items>2</Items><CurrentPage>1</CurrentPage><RequestedPage>1</RequestedPage><NextPage>-1</NextPage><PreviousPage>-1</PreviousPage><LastPage>1</LastPage><ItemsPerPage>10</ItemsPerPage><ItemsFound>2</ItemsFound></Pager></ProfilesResource></Resources>'))
48
+ profile = AMEE::Profile::Profile.list(connection)[0]
49
+ profile.uid.should == "B59C2AA75C7F"
50
+ profile.name.should == "B59C2AA75C7F"
51
+ profile.path.should == "/B59C2AA75C7F"
52
+ profile.full_path.should == "/profiles/B59C2AA75C7F"
53
+ profile.created.should == DateTime.new(2008,7,24,10,45,8)
54
+ profile.modified.should == DateTime.new(2008,7,24,10,55,8)
55
+ end
56
+
57
+ it "should be able to create new profiles (XML)" do
58
+ connection = flexmock "connection"
59
+ connection.should_receive(:post).with("/profiles", :profile => true).and_return(flexmock(:body => '<?xml version="1.0" encoding="UTF-8"?><Resources><ProfilesResource><Profile created="Wed May 23 13:34:45 BST 2007" modified="Wed May 23 13:34:45 BST 2007" uid="04C3F8A10B30"><Path/><Name/><Site uid="C420F0C34227"/><Permission created="Wed May 23 13:34:45 BST 2007" modified="Wed May 23 13:34:45 BST 2007" uid="F06E9C383BFD"><Site uid="C420F0C34227"/><Name>Administrators</Name><Username>root</Username></Permission></Profile></ProfilesResource></Resources>'))
60
+ profile = AMEE::Profile::Profile.create(connection)
61
+ profile.uid.should == "04C3F8A10B30"
62
+ profile.name.should == "04C3F8A10B30"
63
+ profile.path.should == "/04C3F8A10B30"
64
+ profile.full_path.should == "/profiles/04C3F8A10B30"
65
+ profile.created.should == DateTime.new(2007,5,23,12,34,45)
66
+ profile.modified.should == DateTime.new(2007,5,23,12,34,45)
67
+ end
68
+
69
+ it "should be able to create new profiles (JSON)" do
70
+ connection = flexmock "connection"
71
+ connection.should_receive(:post).with("/profiles", :profile => true).and_return(flexmock(:body => '{"profile":{"modified":"Wed May 23 13:36:19 BST 2007","created":"Wed May 23 13:36:19 BST 2007","site":{"uid":"C420F0C34227"},"uid":"F3A7EAE5C99B","permission":{"modified":"Wed May 23 13:36:19 BST 2007","created":"Wed May 23 13:36:19 BST 2007","user":{"uid":"7C41EA37BA4F","username":"root"},"group":{"uid":"3B71F24E93BC","name":"Administrators"},"siteUid":"C420F0C34227","uid":"CC153E13FB7E"},"path":"F3A7EAE5C99B","name":"F3A7EAE5C99B"}}'))
72
+ profile = AMEE::Profile::Profile.create(connection)
73
+ profile.uid.should == "F3A7EAE5C99B"
74
+ profile.name.should == "F3A7EAE5C99B"
75
+ profile.path.should == "/F3A7EAE5C99B"
76
+ profile.full_path.should == "/profiles/F3A7EAE5C99B"
77
+ profile.created.should == DateTime.new(2007,5,23,12,36,19)
78
+ profile.modified.should == DateTime.new(2007,5,23,12,36,19)
79
+ end
80
+
81
+ it "should fail gracefully if new profile creation fails" do
82
+ connection = flexmock "connection"
83
+ json = '{}'
84
+ connection.should_receive(:post).with("/profiles", :profile => true).and_return(flexmock(:body => json))
85
+ lambda{AMEE::Profile::Profile.create(connection)}.should raise_error(AMEE::BadData)
86
+ end
87
+
88
+ end
@@ -0,0 +1,48 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+ require 'amee/rails'
3
+ require 'active_record'
4
+
5
+ describe AMEE::Rails do
6
+
7
+ class AMEETest < ActiveRecord::Base
8
+ include AMEE::Rails
9
+ end
10
+
11
+ it "should add the has_amee_profile method to a class which includes it" do
12
+ AMEETest.respond_to?(:has_amee_profile).should be_true
13
+ end
14
+
15
+ describe "using has_amee_profile" do
16
+
17
+ class HasProfileTest < AMEETest
18
+ has_amee_profile
19
+ end
20
+
21
+ before(:each) do
22
+ @test = disconnected HasProfileTest
23
+ end
24
+
25
+ it "should have an amee_connection function" do
26
+ @test.klass.method_defined?(:amee_connection).should be_true
27
+ end
28
+
29
+ it "should have an amee_create function" do
30
+ @test.klass.method_defined?(:amee_create).should be_true
31
+ end
32
+
33
+ it "should have save_with_amee and save_without_amee functions" do
34
+ @test.klass.method_defined?(:save_with_amee).should be_true
35
+ @test.klass.method_defined?(:save_without_amee).should be_true
36
+ end
37
+
38
+ it "should have an amee_save function" do
39
+ @test.klass.method_defined?(:amee_save).should be_true
40
+ end
41
+
42
+ it "should have an amee_destroy function" do
43
+ @test.klass.method_defined?(:amee_destroy).should be_true
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format RspecSpinner::Bar
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'rspec_spinner'
4
+
5
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
6
+ require 'amee'
7
+
8
+ Spec::Runner.configure do |config|
9
+ config.mock_with :flexmock
10
+ end
11
+
12
+ # Stub activerecord for rails tests
13
+ # Taken from http://muness.blogspot.com/2006/12/unit-testing-rails-activerecord-classes.html
14
+ class ActiveRecordUnitTestHelper
15
+ attr_accessor :klass
16
+
17
+ def initialize klass
18
+ self.klass = klass
19
+ self
20
+ end
21
+
22
+ def where attributes
23
+ klass.stubs(:columns).returns(columns(attributes))
24
+ instance = klass.new(attributes)
25
+ instance.id = attributes[:id] if attributes[:id] #the id attributes works differently on active record classes
26
+ instance
27
+ end
28
+
29
+ protected
30
+ def columns attributes
31
+ attributes.keys.collect{|attribute| column attribute.to_s, attributes[attribute]}
32
+ end
33
+
34
+ def column column_name, value
35
+ ActiveRecord::ConnectionAdapters::Column.new(column_name, nil, ActiveRecordUnitTestHelper.active_record_type(value.class), false)
36
+ end
37
+
38
+ def self.active_record_type klass
39
+ return case klass.name
40
+ when "Fixnum" then "integer"
41
+ when "Float" then "float"
42
+ when "Time" then "time"
43
+ when "Date" then "date"
44
+ when "String" then "string"
45
+ when "Object" then "boolean"
46
+ end
47
+ end
48
+ end
49
+
50
+ def disconnected klass
51
+ ActiveRecordUnitTestHelper.new(klass)
52
+ end
53
+
54
+ def fixture(filename)
55
+ File.read File.dirname(__FILE__)+'/fixtures/'+filename
56
+ end
@@ -0,0 +1,249 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe AMEE::Admin::User do
4
+
5
+ before(:each) do
6
+ @user = AMEE::Admin::User.new
7
+ end
8
+
9
+ it "should have common AMEE object properties" do
10
+ @user.is_a?(AMEE::Object).should be_true
11
+ end
12
+
13
+ it "should have a username" do
14
+ @user.should respond_to(:username)
15
+ end
16
+
17
+ it "should have an email" do
18
+ @user.should respond_to(:email)
19
+ end
20
+
21
+ it "should have a name" do
22
+ @user.should respond_to(:name)
23
+ end
24
+
25
+ it "should have an api version" do
26
+ @user.should respond_to(:api_version)
27
+ end
28
+
29
+ it "should initialize AMEE::Object data on creation" do
30
+ uid = 'ABCD1234'
31
+ @user = AMEE::Admin::User.new(:uid => uid)
32
+ @user.uid.should == uid
33
+ end
34
+
35
+ it "can be created with hash of data" do
36
+ data = {
37
+ :username => "test_login",
38
+ :name => "test_name",
39
+ :email => "test_email",
40
+ :api_version => "2.0"
41
+ }
42
+ @user = AMEE::Admin::User.new(data)
43
+ @user.name.should == data[:name]
44
+ @user.username.should == data[:username]
45
+ @user.email.should == data[:email]
46
+ @user.api_version.should == data[:api_version].to_f
47
+ end
48
+
49
+ end
50
+
51
+ describe AMEE::Admin::User, "with an authenticated connection with environment" do
52
+
53
+ before :all do
54
+ @env = "5F5887BCF726"
55
+ @options = {
56
+ :username => "_rubytest",
57
+ :name => "Test User created by Ruby Gem",
58
+ :email => "ruby@amee.cc",
59
+ :apiVersion => 2.0,
60
+ :password => "test_pw"
61
+ }
62
+ @users_path = "/environments/#{@env}/users"
63
+ @path = "/environments/#{@env}/users/_rubytest"
64
+ @new_options = {
65
+ :username => "_rubytest_changed",
66
+ :name => "Test User created by Ruby Gem, then changed",
67
+ :email => "ruby_changed@amee.cc",
68
+ :APIVersion => 1.0
69
+ }
70
+ @changed_path = "/environments/#{@env}/users/_rubytest_changed"
71
+ end
72
+
73
+ it "can create a new user" do
74
+ connection = flexmock "connection"
75
+ connection.should_receive(:post).with(@users_path, @options).and_return(flexmock(:body => '<?xml version="1.0" encoding="UTF-8" standalone="no"?><Resources><UsersResource><User created="Fri Sep 11 16:41:16 BST 2009" modified="Fri Sep 11 16:41:16 BST 2009" uid="65ED20D9C5EF"><Status>ACTIVE</Status><Type>STANDARD</Type><GroupNames/><ApiVersion>2.0</ApiVersion><Locale>en_GB</Locale><Name>Test User created by Ruby Gem</Name><Username>_rubytest</Username><Email>ruby@amee.cc</Email><Environment uid="5F5887BCF726"/></User></UsersResource></Resources>'))
76
+ @user = AMEE::Admin::User.create(connection, @env, @options)
77
+ end
78
+
79
+ it "should parse XML correctly" do
80
+ connection = flexmock "connection"
81
+ connection.should_receive(:get).with(@path, {}).and_return(flexmock(:body => '<?xml version="1.0" encoding="UTF-8" standalone="no"?><Resources><UserResource><Environment uid="5F5887BCF726"/><User created="2009-09-11 16:41:16.0" modified="2009-09-11 16:41:16.0" uid="65ED20D9C5EF"><Status>ACTIVE</Status><Type>STANDARD</Type><GroupNames/><ApiVersion>2.0</ApiVersion><Locale>en_GB</Locale><Name>Test User created by Ruby Gem</Name><Username>_rubytest</Username><Email>ruby@amee.cc</Email><Environment uid="5F5887BCF726"/></User></UserResource></Resources>'))
82
+ @data = AMEE::Admin::User.get(connection, @path)
83
+ @data.uid.should == "65ED20D9C5EF"
84
+ @data.created.should == DateTime.new(2009,9,11,16,41,16)
85
+ @data.modified.should == DateTime.new(2009,9,11,16,41,16)
86
+ @data.username.should == @options[:username]
87
+ @data.name.should == @options[:name]
88
+ @data.email.should == @options[:email]
89
+ @data.api_version.should be_close(@options[:apiVersion], 1e-9)
90
+ @data.status.should == "ACTIVE"
91
+ end
92
+
93
+ it "should parse JSON correctly" do
94
+ connection = flexmock "connection"
95
+ connection.should_receive(:get).with(@path, {}).and_return(flexmock(:body => '{"user":{"apiVersion":"2.0","uid":"2C4D789B1D46","username":"_rubytest","environment":{"uid":"5F5887BCF726"},"created":"Fri Sep 11 16:29:42 BST 2009","email":"ruby@amee.cc","status":"ACTIVE","name":"Test User created by Ruby Gem","locale":"en_GB","groupNames":[],"type":"STANDARD","modified":"Fri Sep 11 16:29:42 BST 2009"}}'))
96
+ @data = AMEE::Admin::User.get(connection, @path)
97
+ @data.uid.should == "2C4D789B1D46"
98
+ @data.created.should == DateTime.new(2009,9,11,15,29,42)
99
+ @data.modified.should == DateTime.new(2009,9,11,15,29,42)
100
+ @data.username.should == @options[:username]
101
+ @data.name.should == @options[:name]
102
+ @data.email.should == @options[:email]
103
+ @data.api_version.should be_close(@options[:apiVersion], 1e-9)
104
+ @data.status.should == "ACTIVE"
105
+ end
106
+
107
+ it "should fail gracefully with incorrect data" do
108
+ connection = flexmock "connection"
109
+ xml = '<?xml version="1.0" encoding="UTF-8"?><Resources></Resources>'
110
+ connection.should_receive(:get).with(@path, {}).and_return(flexmock(:body => xml))
111
+ lambda{AMEE::Admin::User.get(connection, @path)}.should raise_error(AMEE::BadData)
112
+ end
113
+
114
+ it "should fail gracefully with incorrect JSON data" do
115
+ connection = flexmock "connection"
116
+ json = '{}'
117
+ connection.should_receive(:get).with(@path, {}).and_return(flexmock(:body => json))
118
+ lambda{AMEE::Admin::User.get(connection, @path)}.should raise_error(AMEE::BadData)
119
+ end
120
+
121
+ it "should fail gracefully on other errors" do
122
+ connection = flexmock "connection"
123
+ connection.should_receive(:get).with(@path, {}).and_raise("unidentified error")
124
+ lambda{AMEE::Admin::User.get(connection, @path)}.should raise_error(AMEE::BadData)
125
+ end
126
+
127
+ it "can update an existing user" do
128
+ connection = flexmock "connection"
129
+ connection.should_receive(:get).with(@path, {}).and_return(flexmock(:body => '<?xml version="1.0" encoding="UTF-8" standalone="no"?><Resources><UserResource><Environment uid="5F5887BCF726"/><User created="2009-09-11 16:41:16.0" modified="2009-09-11 16:41:16.0" uid="65ED20D9C5EF"><Status>ACTIVE</Status><Type>STANDARD</Type><GroupNames/><ApiVersion>2.0</ApiVersion><Locale>en_GB</Locale><Name>Test User created by Ruby Gem</Name><Username>_rubytest</Username><Email>ruby@amee.cc</Email><Environment uid="5F5887BCF726"/></User></UserResource></Resources>'))
130
+ @data = AMEE::Admin::User.get(connection, @path)
131
+ connection.should_receive(:put).with(@data.full_path, @new_options).and_return(flexmock(:code => 200, :body => ""))
132
+ connection.should_receive(:get).with(@data.full_path, {}).and_return(flexmock(:body => '<?xml version="1.0" encoding="UTF-8" standalone="no"?><Resources><UserResource><Environment uid="5F5887BCF726"/><User created="2009-09-11 16:41:16.0" modified="2009-09-11 16:55:14.0" uid="65ED20D9C5EF"><Status>ACTIVE</Status><Type>STANDARD</Type><GroupNames/><ApiVersion>1.0</ApiVersion><Locale>en_GB</Locale><Name>Test User created by Ruby Gem, then changed</Name><Username>_rubytest_changed</Username><Email>ruby_changed@amee.cc</Email><Environment uid="5F5887BCF726"/></User></UserResource></Resources>'))
133
+ @new_data = @data.update(@new_options)
134
+ @new_data.created.should_not == @new_data.modified.should
135
+ @new_data.username.should == @new_options[:username]
136
+ @new_data.name.should == @new_options[:name]
137
+ @new_data.email.should == @new_options[:email]
138
+ @new_data.api_version.should be_close(@new_options[:APIVersion],1e-9)
139
+ end
140
+
141
+ it "can delete an existing user" do
142
+ connection = flexmock "connection"
143
+ connection.should_receive(:get).with(@changed_path, {}).and_return(flexmock(:body => '<?xml version="1.0" encoding="UTF-8" standalone="no"?><Resources><UserResource><Environment uid="5F5887BCF726"/><User created="2009-09-11 16:41:16.0" modified="2009-09-11 16:55:14.0" uid="65ED20D9C5EF"><Status>ACTIVE</Status><Type>STANDARD</Type><GroupNames/><ApiVersion>2.0</ApiVersion><Locale>en_GB</Locale><Name>Test User created by Ruby Gem, then changed</Name><Username>_rubytest</Username><Email>ruby_changed@amee.cc</Email><Environment uid="5F5887BCF726"/></User></UserResource></Resources>'))
144
+ @data = AMEE::Admin::User.get(connection, @changed_path)
145
+ connection.should_receive(:delete).with(@data.full_path).and_return(flexmock(:code => 200, :body => ""))
146
+ lambda{@new_data = @data.delete}.should_not raise_error
147
+ end
148
+
149
+ end
150
+
151
+ describe AMEE::Admin::User, "with an authenticated connection without environment" do
152
+
153
+ before :all do
154
+ @env = nil
155
+ @options = {
156
+ :username => "_rubytest",
157
+ :name => "Test User created by Ruby Gem",
158
+ :email => "ruby@amee.cc",
159
+ :apiVersion => 2.0,
160
+ :password => "test_pw"
161
+ }
162
+ @users_path = "/admin/users"
163
+ @path = "/admin/users/_rubytest"
164
+ @new_options = {
165
+ :username => "_rubytest_changed",
166
+ :name => "Test User created by Ruby Gem, then changed",
167
+ :email => "ruby_changed@amee.cc",
168
+ :APIVersion => 1.0
169
+ }
170
+ @changed_path = "/admin/users/_rubytest_changed"
171
+ end
172
+
173
+ it "can create a new user" do
174
+ connection = flexmock "connection"
175
+ connection.should_receive(:post).with(@users_path, @options).and_return(flexmock(:body => '<?xml version="1.0" encoding="UTF-8" standalone="no"?><Resources><UsersResource><User created="Fri Sep 11 16:41:16 BST 2009" modified="Fri Sep 11 16:41:16 BST 2009" uid="65ED20D9C5EF"><Status>ACTIVE</Status><Type>STANDARD</Type><GroupNames/><ApiVersion>2.0</ApiVersion><Locale>en_GB</Locale><Name>Test User created by Ruby Gem</Name><Username>_rubytest</Username><Email>ruby@amee.cc</Email><Environment uid="5F5887BCF726"/></User></UsersResource></Resources>'))
176
+ @user = AMEE::Admin::User.create(connection, @env, @options)
177
+ end
178
+
179
+ it "should parse XML correctly" do
180
+ connection = flexmock "connection"
181
+ connection.should_receive(:get).with(@path, {}).and_return(flexmock(:body => '<?xml version="1.0" encoding="UTF-8" standalone="no"?><Resources><UserResource><Environment uid="5F5887BCF726"/><User created="2009-09-11 16:41:16.0" modified="2009-09-11 16:41:16.0" uid="65ED20D9C5EF"><Status>ACTIVE</Status><Type>STANDARD</Type><GroupNames/><ApiVersion>2.0</ApiVersion><Locale>en_GB</Locale><Name>Test User created by Ruby Gem</Name><Username>_rubytest</Username><Email>ruby@amee.cc</Email><Environment uid="5F5887BCF726"/></User></UserResource></Resources>'))
182
+ @data = AMEE::Admin::User.get(connection, @path)
183
+ @data.uid.should == "65ED20D9C5EF"
184
+ @data.created.should == DateTime.new(2009,9,11,16,41,16)
185
+ @data.modified.should == DateTime.new(2009,9,11,16,41,16)
186
+ @data.username.should == @options[:username]
187
+ @data.name.should == @options[:name]
188
+ @data.email.should == @options[:email]
189
+ @data.api_version.should be_close(@options[:apiVersion], 1e-9)
190
+ @data.status.should == "ACTIVE"
191
+ end
192
+
193
+ it "should parse JSON correctly" do
194
+ connection = flexmock "connection"
195
+ connection.should_receive(:get).with(@path, {}).and_return(flexmock(:body => '{"user":{"apiVersion":"2.0","uid":"2C4D789B1D46","username":"_rubytest","environment":{"uid":"5F5887BCF726"},"created":"Fri Sep 11 16:29:42 BST 2009","email":"ruby@amee.cc","status":"ACTIVE","name":"Test User created by Ruby Gem","locale":"en_GB","groupNames":[],"type":"STANDARD","modified":"Fri Sep 11 16:29:42 BST 2009"}}'))
196
+ @data = AMEE::Admin::User.get(connection, @path)
197
+ @data.uid.should == "2C4D789B1D46"
198
+ @data.created.should == DateTime.new(2009,9,11,15,29,42)
199
+ @data.modified.should == DateTime.new(2009,9,11,15,29,42)
200
+ @data.username.should == @options[:username]
201
+ @data.name.should == @options[:name]
202
+ @data.email.should == @options[:email]
203
+ @data.api_version.should be_close(@options[:apiVersion], 1e-9)
204
+ @data.status.should == "ACTIVE"
205
+ end
206
+
207
+ it "should fail gracefully with incorrect data" do
208
+ connection = flexmock "connection"
209
+ xml = '<?xml version="1.0" encoding="UTF-8"?><Resources></Resources>'
210
+ connection.should_receive(:get).with(@path, {}).and_return(flexmock(:body => xml))
211
+ lambda{AMEE::Admin::User.get(connection, @path)}.should raise_error(AMEE::BadData)
212
+ end
213
+
214
+ it "should fail gracefully with incorrect JSON data" do
215
+ connection = flexmock "connection"
216
+ json = '{}'
217
+ connection.should_receive(:get).with(@path, {}).and_return(flexmock(:body => json))
218
+ lambda{AMEE::Admin::User.get(connection, @path)}.should raise_error(AMEE::BadData)
219
+ end
220
+
221
+ it "should fail gracefully on other errors" do
222
+ connection = flexmock "connection"
223
+ connection.should_receive(:get).with(@path, {}).and_raise("unidentified error")
224
+ lambda{AMEE::Admin::User.get(connection, @path)}.should raise_error(AMEE::BadData)
225
+ end
226
+
227
+ it "can update an existing user" do
228
+ connection = flexmock "connection"
229
+ connection.should_receive(:get).with(@path, {}).and_return(flexmock(:body => '<?xml version="1.0" encoding="UTF-8" standalone="no"?><Resources><UserResource><Environment uid="5F5887BCF726"/><User created="2009-09-11 16:41:16.0" modified="2009-09-11 16:41:16.0" uid="65ED20D9C5EF"><Status>ACTIVE</Status><Type>STANDARD</Type><GroupNames/><ApiVersion>2.0</ApiVersion><Locale>en_GB</Locale><Name>Test User created by Ruby Gem</Name><Username>_rubytest</Username><Email>ruby@amee.cc</Email><Environment uid="5F5887BCF726"/></User></UserResource></Resources>'))
230
+ @data = AMEE::Admin::User.get(connection, @path)
231
+ connection.should_receive(:put).with(@data.full_path, @new_options).and_return(flexmock(:code => 200, :body => ""))
232
+ connection.should_receive(:get).with(@data.full_path, {}).and_return(flexmock(:body => '<?xml version="1.0" encoding="UTF-8" standalone="no"?><Resources><UserResource><Environment uid="5F5887BCF726"/><User created="2009-09-11 16:41:16.0" modified="2009-09-11 16:55:14.0" uid="65ED20D9C5EF"><Status>ACTIVE</Status><Type>STANDARD</Type><GroupNames/><ApiVersion>1.0</ApiVersion><Locale>en_GB</Locale><Name>Test User created by Ruby Gem, then changed</Name><Username>_rubytest_changed</Username><Email>ruby_changed@amee.cc</Email><Environment uid="5F5887BCF726"/></User></UserResource></Resources>'))
233
+ @new_data = @data.update(@new_options)
234
+ @new_data.created.should_not == @new_data.modified.should
235
+ @new_data.username.should == @new_options[:username]
236
+ @new_data.name.should == @new_options[:name]
237
+ @new_data.email.should == @new_options[:email]
238
+ @new_data.api_version.should be_close(@new_options[:APIVersion],1e-9)
239
+ end
240
+
241
+ it "can delete an existing user" do
242
+ connection = flexmock "connection"
243
+ connection.should_receive(:get).with(@changed_path, {}).and_return(flexmock(:body => '<?xml version="1.0" encoding="UTF-8" standalone="no"?><Resources><UserResource><Environment uid="5F5887BCF726"/><User created="2009-09-11 16:41:16.0" modified="2009-09-11 16:55:14.0" uid="65ED20D9C5EF"><Status>ACTIVE</Status><Type>STANDARD</Type><GroupNames/><ApiVersion>2.0</ApiVersion><Locale>en_GB</Locale><Name>Test User created by Ruby Gem, then changed</Name><Username>_rubytest</Username><Email>ruby_changed@amee.cc</Email><Environment uid="5F5887BCF726"/></User></UserResource></Resources>'))
244
+ @data = AMEE::Admin::User.get(connection, @changed_path)
245
+ connection.should_receive(:delete).with(@data.full_path).and_return(flexmock(:code => 200, :body => ""))
246
+ lambda{@new_data = @data.delete}.should_not raise_error
247
+ end
248
+
249
+ end