wss_agent 18.10.2 → 18.10.3

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 (48) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +15 -15
  3. data/.rspec +2 -2
  4. data/.travis.yml +6 -6
  5. data/Gemfile +4 -4
  6. data/LICENSE.txt +201 -201
  7. data/README.md +88 -88
  8. data/Rakefile +8 -8
  9. data/bin/wss_agent +13 -13
  10. data/lib/config/custom_default.yml +5 -5
  11. data/lib/config/default.yml +14 -14
  12. data/lib/data/ca-certificates.crt +4049 -4049
  13. data/lib/wss_agent.rb +50 -50
  14. data/lib/wss_agent/cli.rb +56 -56
  15. data/lib/wss_agent/client.rb +108 -108
  16. data/lib/wss_agent/configure.rb +115 -115
  17. data/lib/wss_agent/gem_sha1.rb +73 -73
  18. data/lib/wss_agent/project.rb +39 -39
  19. data/lib/wss_agent/response.rb +57 -57
  20. data/lib/wss_agent/response_inventory.rb +28 -28
  21. data/lib/wss_agent/response_policies.rb +77 -77
  22. data/lib/wss_agent/specifications.rb +202 -202
  23. data/lib/wss_agent/version.rb +4 -4
  24. data/spec/fixtures/vcr_cassettes/WssAgent_CLI/update/when_not_found_token/should_display_error_message.yml +2984 -2984
  25. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/server_error/response_should_be_success.yml +2984 -2984
  26. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/server_error/should_response_json_data.yml +2984 -2984
  27. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/server_error/should_return_message_response.yml +2984 -2984
  28. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/server_error/should_return_status_of_response.yml +2984 -2984
  29. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/server_timeout/response_should_be_success.yml +2984 -2984
  30. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/server_timeout/should_response_json_data.yml +2984 -2984
  31. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/server_timeout/should_return_message_response.yml +2984 -2984
  32. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/server_timeout/should_return_status_of_response.yml +2984 -2984
  33. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/success/response_should_be_success.yml +2984 -2984
  34. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/success/should_response_json_data.yml +2984 -2984
  35. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/success/should_return_message_response.yml +2984 -2984
  36. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/success/should_return_status_of_response.yml +2984 -2984
  37. data/spec/fixtures/vcr_cassettes/WssAgent_Specifications/_check_policies/should_check_policies.yml +50 -50
  38. data/spec/fixtures/vcr_cassettes/WssAgent_Specifications/_update/should_update_list_gems_on_server.yml +50 -50
  39. data/spec/fixtures/vcr_cassettes/WssAgent_Specifications/_update/when_check_policies_is_true/and_check_policies_return_a_violation/should_not_update_inventory.yml +2984 -2984
  40. data/spec/fixtures/vcr_cassettes/WssAgent_Specifications/_update/when_check_policies_is_true/and_check_policies_returns_without_a_violation/should_update_inventory.yml +2984 -2984
  41. data/spec/spec_helper.rb +36 -36
  42. data/spec/support/exit_code_matches.rb +37 -37
  43. data/spec/wss_agent/cli_spec.rb +58 -58
  44. data/spec/wss_agent/client_spec.rb +151 -151
  45. data/spec/wss_agent/configure_spec.rb +180 -180
  46. data/spec/wss_agent/specifications_spec.rb +162 -162
  47. data/wss_agent.gemspec +38 -38
  48. metadata +3 -3
@@ -1,180 +1,180 @@
1
- require 'spec_helper'
2
-
3
- describe WssAgent::Configure do
4
- let(:default_config) do
5
- {
6
- 'url' => 'https://saas.whitesourcesoftware.com/agent',
7
- 'token' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
8
- 'check_policies' => false,
9
- 'force_check_all_dependencies' => false,
10
- 'agent' => 'bundler-plugin',
11
- 'agent_version' => '1.0',
12
- 'product' => '',
13
- 'product_version' => '',
14
- 'coordinates' => { 'artifact_id' => '', 'version' => '' }
15
- }
16
- end
17
-
18
- describe '.default_path' do
19
- it 'should return path of default config' do
20
- expect(WssAgent::Configure.default_path).to match(/\/lib\/config\/default\.yml\Z/)
21
- end
22
- end
23
-
24
- describe '.exist_default_config?' do
25
- it 'should be true' do
26
- expect(WssAgent::Configure.exist_default_config?).to be true
27
- end
28
- end
29
-
30
- describe '.default' do
31
- it 'should default config' do
32
- expect(WssAgent::Configure.default).to eq(default_config)
33
- end
34
- end
35
-
36
- describe '.current_path' do
37
- it 'should return locally config path' do
38
- expect(WssAgent::Configure.current_path).to match(/wss_agent\.yml\Z/)
39
- end
40
- end
41
-
42
- describe '.check_policies' do
43
- context 'default' do
44
- it 'should be "false"' do
45
- expect(WssAgent::Configure['check_policies']).to be false
46
- end
47
- end
48
- end
49
-
50
- describe '.current' do
51
- context 'when locally config is found' do
52
- before do
53
- allow(WssAgent::Configure).to receive(:current_path)
54
- .and_return(File.join(File.expand_path('../..', __FILE__), 'fixtures/wss_agent.yml'))
55
- end
56
- it 'should return locally config' do
57
- expect(WssAgent::Configure.current).to eq('url' => 'https://saas.whitesourcesoftware.com',
58
- 'token' => '11111111-1111-1111-1111-111111111112',
59
- 'check_policies' => false,
60
- 'force_check_all_dependencies' => false,
61
- 'agent' => 'bundler-plugin',
62
- 'agent_version' => '1.0',
63
- 'coordinates' => { 'artifact_id' => '', 'version' => '' },
64
- 'product' => 'Test product',
65
- 'product_version' => '1.0.1')
66
- end
67
- end
68
- end
69
-
70
- describe '.force_update' do
71
- it 'set value to false' do
72
- expect(WssAgent::Configure['force_update']).to be_falsey
73
- end
74
- it 'set value to true' do
75
- allow(
76
- WssAgent::Configure
77
- ).to receive_messages(current: { 'force_update' => true })
78
- expect(WssAgent::Configure['force_update']).to be_truthy
79
- end
80
-
81
- context 'default value' do
82
- it 'should be false' do
83
- expect(WssAgent::Configure['force_update']).to be_falsey
84
- end
85
- end
86
- end
87
-
88
- describe '.url' do
89
- context 'when url is empty' do
90
- before do
91
- allow(WssAgent::Configure).to receive_messages(current: {})
92
- end
93
- it 'should cause an exception' do
94
- expect { WssAgent::Configure.url }.to raise_error(WssAgent::ApiUrlNotFound)
95
- end
96
- end
97
- context 'when url is exist' do
98
- it 'should return url' do
99
- expect(WssAgent::Configure.url).to eq('https://saas.whitesourcesoftware.com')
100
- end
101
- end
102
- end
103
-
104
- describe '.token' do
105
- context 'when token is not found' do
106
- it 'should cause an exception' do
107
- expect { WssAgent::Configure.token }.to raise_error(WssAgent::TokenNotFound)
108
- end
109
- end
110
- context 'when token is found' do
111
- let(:config) do
112
- {
113
- 'url' => 'https://saas.whitesourcesoftware.com',
114
- 'token' => '11111111-1111-1111-1111-111111111111'
115
- }
116
- end
117
- before do
118
- allow(WssAgent::Configure).to receive_messages(current: config)
119
- end
120
-
121
- it 'should return token' do
122
- expect(WssAgent::Configure.token).to eq('11111111-1111-1111-1111-111111111111')
123
- end
124
- end
125
- end
126
-
127
- describe '.user_key' do
128
- context 'when user_key is not exist' do
129
- it 'should return empty string' do
130
- expect(WssAgent::Configure.user_key).to eq('')
131
- end
132
- it { expect(WssAgent::Configure.user_key?).to be_falsey }
133
- end
134
- context 'when user_key is exist' do
135
- before do
136
- allow(WssAgent::Configure).to receive_messages(user_key: '11111111')
137
- end
138
-
139
- it 'should return user_key' do
140
- expect(WssAgent::Configure.user_key).to eq('11111111')
141
- end
142
-
143
- it { expect(WssAgent::Configure.user_key?).to be_truthy }
144
- end
145
- end
146
-
147
- describe '.agent' do
148
- it 'should be "bundler-plugin"' do
149
- expect(WssAgent::Configure['agent']).to eq('bundler-plugin')
150
- end
151
- end
152
-
153
- describe '.agent_version' do
154
- it 'should be "1.0"' do
155
- expect(WssAgent::Configure['agent_version']).to eq('1.0')
156
- end
157
- end
158
-
159
- describe '.product' do
160
- before do
161
- allow(WssAgent::Configure).to receive(:current_path)
162
- .and_return(File.join(File.expand_path('../..', __FILE__), 'fixtures/wss_agent.yml'))
163
- end
164
-
165
- it 'should be "Test product"' do
166
- expect(WssAgent::Configure['product']).to eq('Test product')
167
- end
168
- end
169
-
170
- describe '.product_version' do
171
- before do
172
- allow(WssAgent::Configure).to receive(:current_path)
173
- .and_return(File.join(File.expand_path('../..', __FILE__), 'fixtures/wss_agent.yml'))
174
- end
175
-
176
- it 'should be "1.0.1"' do
177
- expect(WssAgent::Configure['product_version']).to eq('1.0.1')
178
- end
179
- end
180
- end
1
+ require 'spec_helper'
2
+
3
+ describe WssAgent::Configure do
4
+ let(:default_config) do
5
+ {
6
+ 'url' => 'https://saas.whitesourcesoftware.com/agent',
7
+ 'token' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
8
+ 'check_policies' => false,
9
+ 'force_check_all_dependencies' => false,
10
+ 'agent' => 'bundler-plugin',
11
+ 'agent_version' => '1.0',
12
+ 'product' => '',
13
+ 'product_version' => '',
14
+ 'coordinates' => { 'artifact_id' => '', 'version' => '' }
15
+ }
16
+ end
17
+
18
+ describe '.default_path' do
19
+ it 'should return path of default config' do
20
+ expect(WssAgent::Configure.default_path).to match(/\/lib\/config\/default\.yml\Z/)
21
+ end
22
+ end
23
+
24
+ describe '.exist_default_config?' do
25
+ it 'should be true' do
26
+ expect(WssAgent::Configure.exist_default_config?).to be true
27
+ end
28
+ end
29
+
30
+ describe '.default' do
31
+ it 'should default config' do
32
+ expect(WssAgent::Configure.default).to eq(default_config)
33
+ end
34
+ end
35
+
36
+ describe '.current_path' do
37
+ it 'should return locally config path' do
38
+ expect(WssAgent::Configure.current_path).to match(/wss_agent\.yml\Z/)
39
+ end
40
+ end
41
+
42
+ describe '.check_policies' do
43
+ context 'default' do
44
+ it 'should be "false"' do
45
+ expect(WssAgent::Configure['check_policies']).to be false
46
+ end
47
+ end
48
+ end
49
+
50
+ describe '.current' do
51
+ context 'when locally config is found' do
52
+ before do
53
+ allow(WssAgent::Configure).to receive(:current_path)
54
+ .and_return(File.join(File.expand_path('../..', __FILE__), 'fixtures/wss_agent.yml'))
55
+ end
56
+ it 'should return locally config' do
57
+ expect(WssAgent::Configure.current).to eq('url' => 'https://saas.whitesourcesoftware.com',
58
+ 'token' => '11111111-1111-1111-1111-111111111112',
59
+ 'check_policies' => false,
60
+ 'force_check_all_dependencies' => false,
61
+ 'agent' => 'bundler-plugin',
62
+ 'agent_version' => '1.0',
63
+ 'coordinates' => { 'artifact_id' => '', 'version' => '' },
64
+ 'product' => 'Test product',
65
+ 'product_version' => '1.0.1')
66
+ end
67
+ end
68
+ end
69
+
70
+ describe '.force_update' do
71
+ it 'set value to false' do
72
+ expect(WssAgent::Configure['force_update']).to be_falsey
73
+ end
74
+ it 'set value to true' do
75
+ allow(
76
+ WssAgent::Configure
77
+ ).to receive_messages(current: { 'force_update' => true })
78
+ expect(WssAgent::Configure['force_update']).to be_truthy
79
+ end
80
+
81
+ context 'default value' do
82
+ it 'should be false' do
83
+ expect(WssAgent::Configure['force_update']).to be_falsey
84
+ end
85
+ end
86
+ end
87
+
88
+ describe '.url' do
89
+ context 'when url is empty' do
90
+ before do
91
+ allow(WssAgent::Configure).to receive_messages(current: {})
92
+ end
93
+ it 'should cause an exception' do
94
+ expect { WssAgent::Configure.url }.to raise_error(WssAgent::ApiUrlNotFound)
95
+ end
96
+ end
97
+ context 'when url is exist' do
98
+ it 'should return url' do
99
+ expect(WssAgent::Configure.url).to eq('https://saas.whitesourcesoftware.com')
100
+ end
101
+ end
102
+ end
103
+
104
+ describe '.token' do
105
+ context 'when token is not found' do
106
+ it 'should cause an exception' do
107
+ expect { WssAgent::Configure.token }.to raise_error(WssAgent::TokenNotFound)
108
+ end
109
+ end
110
+ context 'when token is found' do
111
+ let(:config) do
112
+ {
113
+ 'url' => 'https://saas.whitesourcesoftware.com',
114
+ 'token' => '11111111-1111-1111-1111-111111111111'
115
+ }
116
+ end
117
+ before do
118
+ allow(WssAgent::Configure).to receive_messages(current: config)
119
+ end
120
+
121
+ it 'should return token' do
122
+ expect(WssAgent::Configure.token).to eq('11111111-1111-1111-1111-111111111111')
123
+ end
124
+ end
125
+ end
126
+
127
+ describe '.user_key' do
128
+ context 'when user_key is not exist' do
129
+ it 'should return empty string' do
130
+ expect(WssAgent::Configure.user_key).to eq('')
131
+ end
132
+ it { expect(WssAgent::Configure.user_key?).to be_falsey }
133
+ end
134
+ context 'when user_key is exist' do
135
+ before do
136
+ allow(WssAgent::Configure).to receive_messages(user_key: '11111111')
137
+ end
138
+
139
+ it 'should return user_key' do
140
+ expect(WssAgent::Configure.user_key).to eq('11111111')
141
+ end
142
+
143
+ it { expect(WssAgent::Configure.user_key?).to be_truthy }
144
+ end
145
+ end
146
+
147
+ describe '.agent' do
148
+ it 'should be "bundler-plugin"' do
149
+ expect(WssAgent::Configure['agent']).to eq('bundler-plugin')
150
+ end
151
+ end
152
+
153
+ describe '.agent_version' do
154
+ it 'should be "1.0"' do
155
+ expect(WssAgent::Configure['agent_version']).to eq('1.0')
156
+ end
157
+ end
158
+
159
+ describe '.product' do
160
+ before do
161
+ allow(WssAgent::Configure).to receive(:current_path)
162
+ .and_return(File.join(File.expand_path('../..', __FILE__), 'fixtures/wss_agent.yml'))
163
+ end
164
+
165
+ it 'should be "Test product"' do
166
+ expect(WssAgent::Configure['product']).to eq('Test product')
167
+ end
168
+ end
169
+
170
+ describe '.product_version' do
171
+ before do
172
+ allow(WssAgent::Configure).to receive(:current_path)
173
+ .and_return(File.join(File.expand_path('../..', __FILE__), 'fixtures/wss_agent.yml'))
174
+ end
175
+
176
+ it 'should be "1.0.1"' do
177
+ expect(WssAgent::Configure['product_version']).to eq('1.0.1')
178
+ end
179
+ end
180
+ end
@@ -1,162 +1,162 @@
1
- require 'spec_helper'
2
-
3
- describe WssAgent::Specifications, vcr: true do
4
- before do
5
- allow(WssAgent::Configure).to receive_messages(token: 'xxxxxx')
6
- end
7
-
8
- let(:gem_list) {
9
- [
10
- {
11
- 'groupId' => 'bacon',
12
- 'artifactId' => 'bacon-1.2.0.gem',
13
- 'version' => '1.2.0',
14
- 'sha1' => 'xxxxxxxxxxxxxxxxxxxxxxx',
15
- 'optional' => '',
16
- 'children' => '',
17
- 'exclusions' => ''
18
- }
19
- ]
20
- }
21
- let(:default_config) { WssAgent::Configure.default }
22
-
23
- describe '.check_policies' do
24
- let(:success_response) {
25
- WssAgent::ResponsePolicies.new(
26
- Faraday::Response.new(
27
- body:
28
- "{\"envelopeVersion\":\"2.1.0\",\"status\":1,\"message\":\"ok\",\"data\":\"{\\\"organization\\\":\\\"TestParallel588\\\",\\\"existingProjects\\\":{\\\"TestProjectUbuntu\\\":{\\\"children\\\":[]}},\\\"newProjects\\\":{},\\\"projectNewResources\\\":{\\\"TestProjectUbuntu\\\":[]}}\"}",
29
- status: 200))
30
- }
31
-
32
- it 'should check policies' do
33
- Timecop.freeze(Time.now) do
34
- allow_any_instance_of(WssAgent::Client).to receive(:check_policies)
35
- .and_return(success_response)
36
-
37
- allow(WssAgent::Specifications).to receive(:list).and_return(gem_list)
38
- expect(capture(:stdout) {WssAgent::Specifications.check_policies}).to eq("All dependencies conform with open source policies\n")
39
- end
40
- end
41
- end
42
-
43
- describe '.update' do
44
- let(:wss_client) { WssAgent::Client.new }
45
- let(:success_response) {
46
- WssAgent::ResponseInventory.new(
47
- Faraday::Response.new(
48
- body:
49
- "{\"envelopeVersion\":\"2.1.0\",\"status\":1,\"message\":\"ok\",\"data\":\"{\\\"organization\\\":\\\"Tom Test\\\",\\\"updatedProjects\\\":[],\\\"createdProjects\\\":[]}\"}",
50
- status: 200))
51
- }
52
- let(:policy_success_response) {
53
- WssAgent::ResponsePolicies.new(
54
- Faraday::Response.new(
55
- body:
56
- "{\"envelopeVersion\":\"2.1.0\",\"status\":1,\"message\":\"ok\",\"data\":\"{\\\"organization\\\":\\\"TestParallel588\\\",\\\"existingProjects\\\":{\\\"TestProjectUbuntu\\\":{\\\"children\\\":[]}},\\\"newProjects\\\":{},\\\"projectNewResources\\\":{\\\"TestProjectUbuntu\\\":[]}}\"}",
57
- status: 200))
58
- }
59
-
60
- it 'should update list gems on server' do
61
- Timecop.freeze(Time.now) do
62
- allow_any_instance_of(WssAgent::Client).to receive(:update)
63
- .and_return(success_response)
64
- allow(WssAgent::Specifications).to receive(:list).and_return(gem_list)
65
- res = WssAgent::Specifications.update
66
- expect(res.success?).to be true
67
- end
68
- end
69
-
70
- context 'when check_policies is true' do
71
- before do
72
- allow(WssAgent::Client).to receive(:new).and_return(wss_client)
73
- allow(WssAgent::Configure).to receive(:current).and_return(default_config.merge('check_policies' => true))
74
- end
75
- context 'and check policies return a violation' do
76
- it 'should not update inventory' do
77
- allow(policy_success_response).to receive(:policy_violations?).and_return(true)
78
- expect(wss_client).to receive(:check_policies).and_return(policy_success_response)
79
- expect(wss_client).to_not receive(:update)
80
- res = WssAgent::Specifications.update
81
- expect(res.success?).to be false
82
- end
83
- context 'and force_update set true' do
84
- before do
85
- allow(WssAgent::Client).to receive(:new).and_return(wss_client)
86
- allow(WssAgent::Configure).to receive(:current).and_return(
87
- default_config.merge(
88
- 'check_policies' => true,
89
- 'force_update' => true
90
- )
91
- )
92
- end
93
- it 'should be update if update_force is true' do
94
- allow(WssAgent::Specifications).to receive(:list).and_return(gem_list)
95
- allow(policy_success_response).to receive(:policy_violations?).and_return(true)
96
- expect(wss_client).to receive(:check_policies).and_return(policy_success_response)
97
- expect(wss_client).to receive(:update).and_return(success_response)
98
- res = WssAgent::Specifications.update
99
- expect(res.success?).to be_truthy
100
- end
101
- end
102
- end
103
-
104
- context 'and check policies returns without a violation' do
105
- it 'should update inventory' do
106
- allow(WssAgent::Specifications).to receive(:list).and_return(gem_list)
107
- allow(policy_success_response).to receive(:policy_violations?).and_return(false)
108
- expect(wss_client).to receive(:check_policies).and_return(policy_success_response)
109
- expect(wss_client).to receive(:update).and_return(success_response)
110
-
111
- res = WssAgent::Specifications.update
112
- expect(res.success?).to be true
113
- end
114
- end
115
- end
116
-
117
- context 'when check_policies is false' do
118
- before {
119
- allow(WssAgent::Client).to receive(:new).and_return(wss_client)
120
- allow(WssAgent::Configure).to receive(:current)
121
- .and_return(default_config.merge({'check_policies' => false}))
122
- }
123
- it 'should update inventory' do
124
- allow(WssAgent::Specifications).to receive(:list).and_return(gem_list)
125
-
126
- expect(wss_client).to_not receive(:check_policies)
127
- expect(wss_client).to receive(:update).and_return(success_response)
128
-
129
- res = WssAgent::Specifications.update
130
- expect(res.success?).to be true
131
- end
132
- end
133
- end
134
-
135
- describe '.list' do
136
- it 'should build list gems'do
137
- allow_any_instance_of(WssAgent::GemSha1).to receive(:sha1).and_return("85b19b68a33f1dc0e147ff08bad66f7cfc52de36")
138
-
139
- load_specs = double(
140
- resolve: Bundler::SpecSet.new([Gem::Specification.new('bacon', '1.2.0')]),
141
- requested_dependencies: [Bundler::Dependency.new("bacon", "1.2.0")]
142
- )
143
- allow(Bundler::Definition).to receive(:build).and_return(load_specs)
144
- expect(WssAgent::Specifications.list).to eq([{"groupId"=>"bacon",
145
- "artifactId"=>"bacon-1.2.0.gem",
146
- "version"=>"1.2.0",
147
- "sha1"=>"85b19b68a33f1dc0e147ff08bad66f7cfc52de36",
148
- "optional"=>false,
149
- "children"=>[], "exclusions"=>[]}])
150
- end
151
- end
152
-
153
- describe '.specs' do
154
- it 'load bundle spec' do
155
- specs_double = double()
156
- expect(specs_double).to receive(:resolve).and_return(Bundler::SpecSet.new([]))
157
- expect(specs_double).to receive(:requested_dependencies).and_return([])
158
- expect(Bundler::Definition).to receive(:build).and_return(specs_double)
159
- WssAgent::Specifications.specs
160
- end
161
- end
162
- end
1
+ require 'spec_helper'
2
+
3
+ describe WssAgent::Specifications, vcr: true do
4
+ before do
5
+ allow(WssAgent::Configure).to receive_messages(token: 'xxxxxx')
6
+ end
7
+
8
+ let(:gem_list) {
9
+ [
10
+ {
11
+ 'groupId' => 'bacon',
12
+ 'artifactId' => 'bacon-1.2.0.gem',
13
+ 'version' => '1.2.0',
14
+ 'sha1' => 'xxxxxxxxxxxxxxxxxxxxxxx',
15
+ 'optional' => '',
16
+ 'children' => '',
17
+ 'exclusions' => ''
18
+ }
19
+ ]
20
+ }
21
+ let(:default_config) { WssAgent::Configure.default }
22
+
23
+ describe '.check_policies' do
24
+ let(:success_response) {
25
+ WssAgent::ResponsePolicies.new(
26
+ Faraday::Response.new(
27
+ body:
28
+ "{\"envelopeVersion\":\"2.1.0\",\"status\":1,\"message\":\"ok\",\"data\":\"{\\\"organization\\\":\\\"TestParallel588\\\",\\\"existingProjects\\\":{\\\"TestProjectUbuntu\\\":{\\\"children\\\":[]}},\\\"newProjects\\\":{},\\\"projectNewResources\\\":{\\\"TestProjectUbuntu\\\":[]}}\"}",
29
+ status: 200))
30
+ }
31
+
32
+ it 'should check policies' do
33
+ Timecop.freeze(Time.now) do
34
+ allow_any_instance_of(WssAgent::Client).to receive(:check_policies)
35
+ .and_return(success_response)
36
+
37
+ allow(WssAgent::Specifications).to receive(:list).and_return(gem_list)
38
+ expect(capture(:stdout) {WssAgent::Specifications.check_policies}).to eq("All dependencies conform with open source policies\n")
39
+ end
40
+ end
41
+ end
42
+
43
+ describe '.update' do
44
+ let(:wss_client) { WssAgent::Client.new }
45
+ let(:success_response) {
46
+ WssAgent::ResponseInventory.new(
47
+ Faraday::Response.new(
48
+ body:
49
+ "{\"envelopeVersion\":\"2.1.0\",\"status\":1,\"message\":\"ok\",\"data\":\"{\\\"organization\\\":\\\"Tom Test\\\",\\\"updatedProjects\\\":[],\\\"createdProjects\\\":[]}\"}",
50
+ status: 200))
51
+ }
52
+ let(:policy_success_response) {
53
+ WssAgent::ResponsePolicies.new(
54
+ Faraday::Response.new(
55
+ body:
56
+ "{\"envelopeVersion\":\"2.1.0\",\"status\":1,\"message\":\"ok\",\"data\":\"{\\\"organization\\\":\\\"TestParallel588\\\",\\\"existingProjects\\\":{\\\"TestProjectUbuntu\\\":{\\\"children\\\":[]}},\\\"newProjects\\\":{},\\\"projectNewResources\\\":{\\\"TestProjectUbuntu\\\":[]}}\"}",
57
+ status: 200))
58
+ }
59
+
60
+ it 'should update list gems on server' do
61
+ Timecop.freeze(Time.now) do
62
+ allow_any_instance_of(WssAgent::Client).to receive(:update)
63
+ .and_return(success_response)
64
+ allow(WssAgent::Specifications).to receive(:list).and_return(gem_list)
65
+ res = WssAgent::Specifications.update
66
+ expect(res.success?).to be true
67
+ end
68
+ end
69
+
70
+ context 'when check_policies is true' do
71
+ before do
72
+ allow(WssAgent::Client).to receive(:new).and_return(wss_client)
73
+ allow(WssAgent::Configure).to receive(:current).and_return(default_config.merge('check_policies' => true))
74
+ end
75
+ context 'and check policies return a violation' do
76
+ it 'should not update inventory' do
77
+ allow(policy_success_response).to receive(:policy_violations?).and_return(true)
78
+ expect(wss_client).to receive(:check_policies).and_return(policy_success_response)
79
+ expect(wss_client).to_not receive(:update)
80
+ res = WssAgent::Specifications.update
81
+ expect(res.success?).to be false
82
+ end
83
+ context 'and force_update set true' do
84
+ before do
85
+ allow(WssAgent::Client).to receive(:new).and_return(wss_client)
86
+ allow(WssAgent::Configure).to receive(:current).and_return(
87
+ default_config.merge(
88
+ 'check_policies' => true,
89
+ 'force_update' => true
90
+ )
91
+ )
92
+ end
93
+ it 'should be update if update_force is true' do
94
+ allow(WssAgent::Specifications).to receive(:list).and_return(gem_list)
95
+ allow(policy_success_response).to receive(:policy_violations?).and_return(true)
96
+ expect(wss_client).to receive(:check_policies).and_return(policy_success_response)
97
+ expect(wss_client).to receive(:update).and_return(success_response)
98
+ res = WssAgent::Specifications.update
99
+ expect(res.success?).to be_truthy
100
+ end
101
+ end
102
+ end
103
+
104
+ context 'and check policies returns without a violation' do
105
+ it 'should update inventory' do
106
+ allow(WssAgent::Specifications).to receive(:list).and_return(gem_list)
107
+ allow(policy_success_response).to receive(:policy_violations?).and_return(false)
108
+ expect(wss_client).to receive(:check_policies).and_return(policy_success_response)
109
+ expect(wss_client).to receive(:update).and_return(success_response)
110
+
111
+ res = WssAgent::Specifications.update
112
+ expect(res.success?).to be true
113
+ end
114
+ end
115
+ end
116
+
117
+ context 'when check_policies is false' do
118
+ before {
119
+ allow(WssAgent::Client).to receive(:new).and_return(wss_client)
120
+ allow(WssAgent::Configure).to receive(:current)
121
+ .and_return(default_config.merge({'check_policies' => false}))
122
+ }
123
+ it 'should update inventory' do
124
+ allow(WssAgent::Specifications).to receive(:list).and_return(gem_list)
125
+
126
+ expect(wss_client).to_not receive(:check_policies)
127
+ expect(wss_client).to receive(:update).and_return(success_response)
128
+
129
+ res = WssAgent::Specifications.update
130
+ expect(res.success?).to be true
131
+ end
132
+ end
133
+ end
134
+
135
+ describe '.list' do
136
+ it 'should build list gems'do
137
+ allow_any_instance_of(WssAgent::GemSha1).to receive(:sha1).and_return("85b19b68a33f1dc0e147ff08bad66f7cfc52de36")
138
+
139
+ load_specs = double(
140
+ resolve: Bundler::SpecSet.new([Gem::Specification.new('bacon', '1.2.0')]),
141
+ requested_dependencies: [Bundler::Dependency.new("bacon", "1.2.0")]
142
+ )
143
+ allow(Bundler::Definition).to receive(:build).and_return(load_specs)
144
+ expect(WssAgent::Specifications.list).to eq([{"groupId"=>"bacon",
145
+ "artifactId"=>"bacon-1.2.0.gem",
146
+ "version"=>"1.2.0",
147
+ "sha1"=>"85b19b68a33f1dc0e147ff08bad66f7cfc52de36",
148
+ "optional"=>false,
149
+ "children"=>[], "exclusions"=>[]}])
150
+ end
151
+ end
152
+
153
+ describe '.specs' do
154
+ it 'load bundle spec' do
155
+ specs_double = double()
156
+ expect(specs_double).to receive(:resolve).and_return(Bundler::SpecSet.new([]))
157
+ expect(specs_double).to receive(:requested_dependencies).and_return([])
158
+ expect(Bundler::Definition).to receive(:build).and_return(specs_double)
159
+ WssAgent::Specifications.specs
160
+ end
161
+ end
162
+ end