code42 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE +203 -0
- data/README.md +182 -0
- data/Rakefile +1 -0
- data/code42.gemspec +26 -0
- data/examples/Gemfile +6 -0
- data/examples/computers.rb +88 -0
- data/examples/diagnostic.rb +82 -0
- data/examples/orgs.rb +88 -0
- data/examples/parser.rb +51 -0
- data/examples/parser_test.rb +64 -0
- data/examples/users.rb +88 -0
- data/lib/code42.rb +46 -0
- data/lib/code42/attribute.rb +13 -0
- data/lib/code42/attribute_serializer.rb +129 -0
- data/lib/code42/client.rb +301 -0
- data/lib/code42/computer.rb +15 -0
- data/lib/code42/connection.rb +124 -0
- data/lib/code42/diagnostic.rb +8 -0
- data/lib/code42/error.rb +21 -0
- data/lib/code42/org.rb +38 -0
- data/lib/code42/ping.rb +14 -0
- data/lib/code42/resource.rb +58 -0
- data/lib/code42/role.rb +26 -0
- data/lib/code42/role_collection.rb +35 -0
- data/lib/code42/settings.rb +42 -0
- data/lib/code42/token.rb +31 -0
- data/lib/code42/token_validation.rb +10 -0
- data/lib/code42/user.rb +40 -0
- data/lib/code42/version.rb +3 -0
- data/spec/cassettes/Code42_Client/_create_org/returns_created_org.yml +47 -0
- data/spec/cassettes/Code42_Client/_create_user/returns_created_user.yml +44 -0
- data/spec/cassettes/Code42_Client/_create_user/when_sending_an_invalid_email/raises_an_exception.yml +37 -0
- data/spec/cassettes/Code42_Client/_find_org_by_name/returns_the_org_with_the_specified_name.yml +101 -0
- data/spec/cassettes/Code42_Client/_get_token/returns_valid_tokens.yml +38 -0
- data/spec/cassettes/Code42_Client/_get_token/when_providing_invalid_credentials/should_raise_an_exception.yml +37 -0
- data/spec/cassettes/Code42_Client/_org/when_ID_is_not_passed/returns_my_org.yml +54 -0
- data/spec/cassettes/Code42_Client/_org/when_ID_is_passed_in/returns_a_specific_org.yml +54 -0
- data/spec/cassettes/Code42_Client/_ping/returns_a_ping.yml +48 -0
- data/spec/cassettes/Code42_Client/_user/when_ID_is_not_passed/returns_my_user.yml +53 -0
- data/spec/cassettes/Code42_Client/_user/when_ID_is_passed_in/returns_a_specific_user.yml +53 -0
- data/spec/cassettes/Code42_Client/_user_roles/returns_an_enumerable.yml +50 -0
- data/spec/cassettes/Code42_Client/_validate_token/returns_a_valid_response.yml +83 -0
- data/spec/cassettes/Crashplan_Client/_create_org/returns_created_org.yml +47 -0
- data/spec/cassettes/Crashplan_Client/_create_user/returns_created_user.yml +44 -0
- data/spec/cassettes/Crashplan_Client/_create_user/when_sending_an_invalid_email/raises_an_exception.yml +37 -0
- data/spec/cassettes/Crashplan_Client/_find_org_by_name/returns_the_org_with_the_specified_name.yml +55 -0
- data/spec/cassettes/Crashplan_Client/_get_token/returns_valid_tokens.yml +38 -0
- data/spec/cassettes/Crashplan_Client/_get_token/when_providing_invalid_credentials/should_raise_an_exception.yml +37 -0
- data/spec/cassettes/Crashplan_Client/_org/when_ID_is_not_passed/returns_my_org.yml +54 -0
- data/spec/cassettes/Crashplan_Client/_org/when_ID_is_passed_in/returns_a_specific_org.yml +54 -0
- data/spec/cassettes/Crashplan_Client/_ping/returns_a_ping.yml +48 -0
- data/spec/cassettes/Crashplan_Client/_user/when_ID_is_not_passed/returns_my_user.yml +99 -0
- data/spec/cassettes/Crashplan_Client/_user/when_ID_is_passed_in/returns_a_specific_user.yml +53 -0
- data/spec/cassettes/Crashplan_Client/_user_roles/returns_an_enumerable.yml +50 -0
- data/spec/cassettes/Crashplan_Client/_validate_token/returns_a_valid_response.yml +128 -0
- data/spec/crashplan/client_spec.rb +135 -0
- data/spec/crashplan/connection_spec.rb +45 -0
- data/spec/crashplan/org_spec.rb +63 -0
- data/spec/crashplan/ping_spec.rb +14 -0
- data/spec/crashplan/resource_spec.rb +56 -0
- data/spec/crashplan/role_spec.rb +28 -0
- data/spec/crashplan/settings_spec.rb +118 -0
- data/spec/crashplan/token_spec.rb +33 -0
- data/spec/crashplan/user_spec.rb +21 -0
- data/spec/fixtures/auth/bad_password.json +1 -0
- data/spec/fixtures/authToken.json +10 -0
- data/spec/fixtures/org.1.json +36 -0
- data/spec/fixtures/org.create.json +36 -0
- data/spec/fixtures/org.my.json +36 -0
- data/spec/fixtures/ping.json +7 -0
- data/spec/fixtures/user.1.json +27 -0
- data/spec/fixtures/user.create.json +27 -0
- data/spec/fixtures/user.my.json +27 -0
- data/spec/fixtures/user_roles.json +32 -0
- data/spec/fixtures/validate_token.json +10 -0
- data/spec/spec_helper.rb +67 -0
- metadata +268 -0
@@ -0,0 +1,135 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Code42::Client, :vcr do
|
4
|
+
subject(:client) do
|
5
|
+
Code42::Client.new(
|
6
|
+
host: 'localhost',
|
7
|
+
port: 7280,
|
8
|
+
https: false,
|
9
|
+
api_root: '/api',
|
10
|
+
username: 'admin',
|
11
|
+
password: 'admin'
|
12
|
+
)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#validate_token" do
|
16
|
+
it "returns a valid response" do
|
17
|
+
token = client.get_token
|
18
|
+
validation = client.validate_token token
|
19
|
+
validation.should be_valid
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#user_roles" do
|
24
|
+
it "returns an enumerable" do
|
25
|
+
roles = client.user_roles
|
26
|
+
roles.should respond_to(:each)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "#get_token" do
|
31
|
+
it "returns valid tokens" do
|
32
|
+
auth = client.get_token
|
33
|
+
auth.cookie_token.should have(26).characters
|
34
|
+
auth.url_token.should have(26).characters
|
35
|
+
end
|
36
|
+
|
37
|
+
context "when providing invalid credentials" do
|
38
|
+
it "should raise an exception" do
|
39
|
+
client.settings.password = 'badpassword'
|
40
|
+
expect{client.get_token}.to raise_error Code42::Error::AuthenticationError
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "#create_org" do
|
46
|
+
let(:org_attributes) do
|
47
|
+
{
|
48
|
+
:orgName => 'IBM'
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
52
|
+
it "returns created org" do
|
53
|
+
org = client.create_org(org_attributes)
|
54
|
+
org.name.should eq 'IBM'
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "#create_user" do
|
60
|
+
let(:user_attributes) do
|
61
|
+
{
|
62
|
+
:orgId => 2,
|
63
|
+
:username => 'testuser'
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
67
|
+
it "returns created user" do
|
68
|
+
user = client.create_user(user_attributes)
|
69
|
+
user.username.should eq 'testuser'
|
70
|
+
user.id.should eq 3
|
71
|
+
end
|
72
|
+
|
73
|
+
context "when sending an invalid email" do
|
74
|
+
it "raises an exception" do
|
75
|
+
user_attributes[:email] = 'testuser'
|
76
|
+
expect { client.create_user(user_attributes) }.to raise_error Code42::Error::EmailInvalid
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "#user" do
|
82
|
+
context "when ID is not passed" do
|
83
|
+
it "returns my user" do
|
84
|
+
user = client.user
|
85
|
+
user.id.should == 1
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context "when ID is passed in" do
|
90
|
+
it "returns a specific user" do
|
91
|
+
user = client.user(2)
|
92
|
+
user.id.should == 2
|
93
|
+
end
|
94
|
+
|
95
|
+
it "passes params to code42 server" do
|
96
|
+
client.should_receive(:object_from_response).with(Code42::User, :get, "user/2", :incAll => true)
|
97
|
+
user = client.user(2, :incAll => true)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe "#org" do
|
103
|
+
context "when ID is not passed" do
|
104
|
+
it "returns my org" do
|
105
|
+
org = client.org
|
106
|
+
org.id.should == 1
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context "when ID is passed in" do
|
111
|
+
it "returns a specific org" do
|
112
|
+
org = client.org(1)
|
113
|
+
org.id.should == 1
|
114
|
+
end
|
115
|
+
|
116
|
+
it "passes params to code42 server" do
|
117
|
+
client.should_receive(:object_from_response).with(Code42::Org, :get, "org/2", :incAll => true)
|
118
|
+
user = client.org(2, :incAll => true)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
describe "#find_org_by_name" do
|
124
|
+
it "returns the org with the specified name" do
|
125
|
+
org = client.find_org_by_name 'PROServer Demo'
|
126
|
+
org.name.should == 'PROServer Demo'
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe "#ping" do
|
131
|
+
it "returns a ping" do
|
132
|
+
client.ping.should be_true
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Code42::Connection do
|
4
|
+
subject(:connection) do
|
5
|
+
Code42::Connection.new(
|
6
|
+
host: 'example.com',
|
7
|
+
port: 1234,
|
8
|
+
scheme: 'http',
|
9
|
+
username: 'fred',
|
10
|
+
password: 'letmein',
|
11
|
+
path_prefix: '/api/v2'
|
12
|
+
)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#token" do
|
16
|
+
it "sets authorization headers" do
|
17
|
+
connection.token = "0jdeqya6xroz713tn1hxp6d8p1-1t5839d7lwfxr0g84jf1nqp2vi"
|
18
|
+
expect(connection.headers['Authorization']).to eq "TOKEN 0jdeqya6xroz713tn1hxp6d8p1-1t5839d7lwfxr0g84jf1nqp2vi"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should set host to example.com" do
|
23
|
+
expect(connection.host).to eq 'example.com'
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should set port to 1234" do
|
27
|
+
expect(connection.port).to eq 1234
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should set scheme to http" do
|
31
|
+
expect(connection.scheme).to eq 'http'
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should set api_root to /api/v2" do
|
35
|
+
expect(connection.path_prefix).to eq '/api/v2'
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should set username to fred" do
|
39
|
+
expect(connection.username).to eq 'fred'
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should set password to letmein" do
|
43
|
+
expect(connection.password).to eq 'letmein'
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Code42::Org do
|
4
|
+
let(:valid_attributes) do
|
5
|
+
{
|
6
|
+
"orgId" => 123,
|
7
|
+
"orgUid" => "ADMIN",
|
8
|
+
"orgName" => "ADMIN",
|
9
|
+
"status" => "Active",
|
10
|
+
"active" => true,
|
11
|
+
"blocked" => false,
|
12
|
+
"parentOrgId" => nil,
|
13
|
+
"type" => "ENTERPRISE",
|
14
|
+
"creationDate" => "2006-05-23T15:11:39.159-05:00",
|
15
|
+
"modificationDate" => "2010-03-09T15:06:37.724-06:00"
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
subject(:org) do
|
20
|
+
Code42::Org.from_response(valid_attributes)
|
21
|
+
end
|
22
|
+
|
23
|
+
describe ".serialize" do
|
24
|
+
it "serializes data correctly" do
|
25
|
+
data = {
|
26
|
+
name: 'Target'
|
27
|
+
}
|
28
|
+
serialized = Code42::Org.serialize(data)
|
29
|
+
serialized.should be_a(Hash)
|
30
|
+
serialized['orgName'].should eq 'Target'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#id" do
|
35
|
+
it "returns the correct id" do
|
36
|
+
org.id.should eq 123
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "#created_at" do
|
41
|
+
it "returns a DateTime object" do
|
42
|
+
org.created_at.should be_a DateTime
|
43
|
+
end
|
44
|
+
|
45
|
+
it "returns the correct date" do
|
46
|
+
org.created_at.day.should eq 23
|
47
|
+
org.created_at.month.should eq 5
|
48
|
+
org.created_at.year.should eq 2006
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "#updated_at" do
|
53
|
+
it "returns a DateTime object" do
|
54
|
+
org.updated_at.should be_a DateTime
|
55
|
+
end
|
56
|
+
|
57
|
+
it "returns the correct date" do
|
58
|
+
org.updated_at.day.should eq 9
|
59
|
+
org.updated_at.month.should eq 3
|
60
|
+
org.updated_at.year.should eq 2010
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Code42
|
4
|
+
describe Resource do
|
5
|
+
describe '#serialize' do
|
6
|
+
before do
|
7
|
+
Resource.class_eval do
|
8
|
+
attribute :created_at, :from => 'creationDate', :as => DateTime
|
9
|
+
attribute :updated_at, :from => 'modificationDate', :as => DateTime
|
10
|
+
attribute :name
|
11
|
+
attribute :permissions
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'serializes attributes correctly' do
|
16
|
+
resource = Resource.new(
|
17
|
+
name: 'Test',
|
18
|
+
created_at: DateTime.new(2013,1,1),
|
19
|
+
updated_at: DateTime.new(2013,1,1),
|
20
|
+
permissions: {
|
21
|
+
foo_id: 1,
|
22
|
+
bar: 2
|
23
|
+
}
|
24
|
+
)
|
25
|
+
serialized = resource.serialize
|
26
|
+
serialized.should include('name' => 'Test')
|
27
|
+
serialized.should include('creationDate' => '2013-01-01T00:00:00+00:00')
|
28
|
+
serialized.should include('modificationDate' => '2013-01-01T00:00:00+00:00')
|
29
|
+
serialized.should include('permissions')
|
30
|
+
serialized['permissions'].should include('fooId' => 1)
|
31
|
+
serialized['permissions'].should include('bar' => 2)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '#deserialize' do
|
36
|
+
before do
|
37
|
+
class LineItem < Resource
|
38
|
+
end
|
39
|
+
Resource.class_eval do
|
40
|
+
attribute :products, :as => LineItem, :collection => true
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'deserialize a single object response with nested objects' do
|
45
|
+
attrs =
|
46
|
+
{'products' => [
|
47
|
+
{'product' => {'name' => 1}},
|
48
|
+
{'product' => {'name' => 1}}
|
49
|
+
]}
|
50
|
+
res = Resource.new
|
51
|
+
deserialized = res.class.deserialize(attrs)
|
52
|
+
expect(deserialized[:products].count).to eq 2
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Code42::Role do
|
4
|
+
describe "#deserialize_and_initialize" do
|
5
|
+
let(:response_data) do
|
6
|
+
data = <<-JSON
|
7
|
+
{
|
8
|
+
"roleId": 3,
|
9
|
+
"roleName": "PROe User",
|
10
|
+
"locked": true,
|
11
|
+
"creationDate": "2013-01-31T15:51:07.810-06:00",
|
12
|
+
"modificationDate": "2013-01-31T15:51:07.810-06:00",
|
13
|
+
"permissions":
|
14
|
+
[
|
15
|
+
{"permission": "admin.cpp.login"},
|
16
|
+
{"permission": "admin.console.login"}
|
17
|
+
]
|
18
|
+
}
|
19
|
+
JSON
|
20
|
+
JSON.parse(data)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should translate roleId to id" do
|
24
|
+
role = Code42::Role.deserialize_and_initialize(response_data)
|
25
|
+
role.id.should eq 3
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Code42::Settings do
|
4
|
+
subject(:settings) { Code42::Settings.new }
|
5
|
+
|
6
|
+
describe "#host" do
|
7
|
+
it "should return configured host" do
|
8
|
+
settings.host = "example.com"
|
9
|
+
settings.host.should == "example.com"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#base_url" do
|
14
|
+
it "should build base_url properly" do
|
15
|
+
settings.https = true
|
16
|
+
settings.host = 'example.com'
|
17
|
+
settings.port = 123
|
18
|
+
settings.api_root = '/api'
|
19
|
+
settings.base_url.should eq 'https://example.com:123/api'
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should raise an exception if settings invalid" do
|
23
|
+
settings.host = 'example.com'
|
24
|
+
expect { settings.base_url }.to raise_error(Code42::Error)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#valid?" do
|
29
|
+
it "should return true if required properties are defined" do
|
30
|
+
settings.host = 'example.com'
|
31
|
+
settings.port = 123
|
32
|
+
settings.api_root = '/api'
|
33
|
+
settings.should be_valid
|
34
|
+
settings.should be_valid
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should return false if required properties aren't defined" do
|
38
|
+
settings.host = 'example.com'
|
39
|
+
settings.api_root = '/api'
|
40
|
+
settings.should_not be_valid
|
41
|
+
settings.should_not be_valid
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "#scheme" do
|
46
|
+
it "should be http if https is false" do
|
47
|
+
settings.https = false
|
48
|
+
settings.scheme.should eq 'http'
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should be https if https is true" do
|
52
|
+
settings.https = true
|
53
|
+
settings.scheme.should == 'https'
|
54
|
+
settings.scheme.should eq 'https'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "#port" do
|
59
|
+
it "should return configured port" do
|
60
|
+
settings.port = 123
|
61
|
+
settings.port.should eq 123
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#https" do
|
66
|
+
it "should return the https boolean" do
|
67
|
+
settings.https = true
|
68
|
+
settings.https.should be_true
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should default to true" do
|
72
|
+
settings = Code42::Settings.new
|
73
|
+
settings.https.should be_true
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "#api_root" do
|
78
|
+
it "should return the api root" do
|
79
|
+
settings.api_root = '/api/v3'
|
80
|
+
settings.api_root.should eq '/api/v3'
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "#username" do
|
85
|
+
it "should return the username" do
|
86
|
+
settings.username = 'bob'
|
87
|
+
settings.username.should eq 'bob'
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe "#password" do
|
92
|
+
it "should return the password" do
|
93
|
+
settings.password = 'bob'
|
94
|
+
settings.password.should eq 'bob'
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "#all" do
|
99
|
+
it "should return a hash of all settings" do
|
100
|
+
settings = Code42::Settings.new(
|
101
|
+
host: 'example.com',
|
102
|
+
port: 123,
|
103
|
+
api_root: '/api',
|
104
|
+
https: true,
|
105
|
+
username: 'fred',
|
106
|
+
password: 'secret'
|
107
|
+
)
|
108
|
+
settings.all.should eq({
|
109
|
+
host: 'example.com',
|
110
|
+
port: 123,
|
111
|
+
https: true,
|
112
|
+
api_root: '/api',
|
113
|
+
username: 'fred',
|
114
|
+
password: 'secret'
|
115
|
+
})
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|