samanage 2.1.04 → 2.1.05

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b2eae5b0a2fd2224a58170972a1743f1b4f97874
4
- data.tar.gz: c77a999e1d466c5e98fd6f312b182a416f0d4df3
3
+ metadata.gz: 8c12e9f23a7417d96f83d1c76e7e5780cba44460
4
+ data.tar.gz: 1df4bc708e4688b5b84a6d33b358eb0f3d6eba0c
5
5
  SHA512:
6
- metadata.gz: 859bb3474497a51470e7c8b54654e47cec0b2eb7e77e3760233faf8f823b195c1b6576508829b688e7044e737b93d130463b717842e4e740af32bb5f36806cad
7
- data.tar.gz: f059e28ff6ddf2f33a5a06c3a9a2aeb07794790990b5b53255740b819b7af4a050f03f49b976cf01d6a4c8cb0bc29971eaba0964451ed242dcd9726856486b62
6
+ metadata.gz: 96ab8a94116c16ae22b3a35e466f0826a03b43793937e9c34a6664811b41442b311c4bfd957e8abcc3fac37c703837e084a7ac6d08d5851e73963f0cefcbf7d4
7
+ data.tar.gz: f64fc20a63cfa50828f909d9156f0fbb5bfca20834bcbf7f524cd2737539f66d649a0747fbca5a9211b4ccf8d08a5d33b5b19f9ad61dffeb711629570fed4117
data/Gemfile CHANGED
@@ -3,7 +3,12 @@ source "https://rubygems.org"
3
3
  gem 'rspec'
4
4
 
5
5
  gem 'httparty', '0.15.7'
6
-
6
+ gem 'ffi', '1.9.24'
7
7
  group :development do
8
8
  gem 'guard-rspec', require: false
9
+ end
10
+
11
+ group :test do
12
+ gem 'guard-rspec', require: false
13
+ gem 'faker'
9
14
  end
data/Gemfile.lock CHANGED
@@ -2,7 +2,10 @@ GEM
2
2
  remote: https://rubygems.org/
3
3
  specs:
4
4
  coderay (1.1.2)
5
+ concurrent-ruby (1.0.5)
5
6
  diff-lcs (1.3)
7
+ faker (1.9.1)
8
+ i18n (>= 0.7)
6
9
  ffi (1.9.23)
7
10
  formatador (0.2.5)
8
11
  guard (2.14.2)
@@ -21,6 +24,8 @@ GEM
21
24
  rspec (>= 2.99.0, < 4.0)
22
25
  httparty (0.15.7)
23
26
  multi_xml (>= 0.5.2)
27
+ i18n (1.1.0)
28
+ concurrent-ruby (~> 1.0)
24
29
  listen (3.1.5)
25
30
  rb-fsevent (~> 0.9, >= 0.9.4)
26
31
  rb-inotify (~> 0.9, >= 0.9.7)
@@ -59,9 +64,11 @@ PLATFORMS
59
64
  ruby
60
65
 
61
66
  DEPENDENCIES
67
+ faker
68
+ ffi (= 1.9.24)
62
69
  guard-rspec
63
70
  httparty (= 0.15.7)
64
71
  rspec
65
72
 
66
73
  BUNDLED WITH
67
- 1.16.0
74
+ 1.16.1
data/changelog.md CHANGED
@@ -1,3 +1,6 @@
1
+ # 2.1.05
2
+ - Adding new error checks
3
+ - Using Faker for tests
1
4
  # 2.1.04
2
5
  - params bug fix
3
6
  # 2.1.03
data/lib/samanage/api.rb CHANGED
@@ -3,6 +3,7 @@ require 'open-uri'
3
3
  module Samanage
4
4
  class Api
5
5
  include HTTParty
6
+ attr_accessor :datacenter, :content_type, :base_url, :token, :custom_forms, :authorized, :admins, :max_retries
6
7
  MAX_RETRIES = 3
7
8
  PATHS = {
8
9
  category: 'categories.json',
@@ -20,11 +21,9 @@ module Samanage
20
21
  solution: 'solutions.json',
21
22
  user: 'users.json',
22
23
  }
23
- attr_accessor :datacenter, :content_type, :base_url, :token, :custom_forms, :authorized, :admins, :max_retries
24
-
25
24
  # Development mode forces authorization & pre-populates admins and custom forms / fields
26
25
  # datacenter should equal 'eu' or blank
27
- def initialize(token: , datacenter: nil, development_mode: false, max_retries: MAX_RETRIES)
26
+ def initialize(token: , datacenter: nil, development_mode: false, max_retries: MAX_RETRIES, content_type: 'json')
28
27
  self.token = token
29
28
  if !datacenter.nil? && datacenter.to_s.downcase != 'eu'
30
29
  datacenter = nil
@@ -47,15 +46,15 @@ module Samanage
47
46
  self.authorized
48
47
  end
49
48
 
50
- # Check token against api.json
49
+ # Check "oken against api.json"
51
50
  def authorize
52
- self.execute(path: 'api.json')
51
+ self.execute(path: "api.#{self.content_type}")
53
52
  self.authorized = true
54
53
  end
55
54
 
56
55
  # Calling execute without a method defaults to GET
57
56
  def execute(http_method: 'get', path: nil, payload: nil, verbose: nil, headers: {})
58
- if payload.class == String
57
+ if payload.class == String && self.content_type == 'json'
59
58
  begin
60
59
  payload = JSON.parse(payload)
61
60
  rescue => e
@@ -92,8 +91,8 @@ module Samanage
92
91
  else
93
92
  raise Samanage::Error.new(response: {response: 'Unknown HTTP method'})
94
93
  end
95
- rescue Errno::ECONNREFUSED, Net::OpenTimeout, Errno::ETIMEDOUT, OpenSSL::SSL::SSLError => e
96
- puts "[Warning]#{e.class}: #{e} - Retry: #{retries}/#{self.max_retries}"
94
+ rescue Errno::ECONNREFUSED, Net::OpenTimeout, Errno::ETIMEDOUT, OpenSSL::SSL::SSLError, Errno::ENETDOWN, Errno::ECONNRESET, Errno::ENOENT, EOFError => e
95
+ puts "[Warning] #{e.class}: #{e} - Retry: #{retries}/#{self.max_retries}"
97
96
  sleep 3
98
97
  retries += 1
99
98
  retry if retries < self.max_retries
@@ -150,7 +149,7 @@ module Samanage
150
149
 
151
150
  # Return all admins in the account
152
151
  def list_admins
153
- admin_role_id = self.execute(path: 'roles.json')[:data].select{|role| role['name'] == 'Administrator'}.first['id']
152
+ admin_role_id = self.execute(path: "roles.json")[:data].select{|role| role['name'] == 'Administrator'}.first['id']
154
153
  self.admins.push(
155
154
  self.execute(path: "users.json?role=#{admin_role_id}")[:data].map{|u| u['email']}
156
155
  ).flatten
@@ -3,7 +3,6 @@ module Samanage
3
3
  # Get custom forms path
4
4
  def get_custom_forms(path: PATHS[:custom_forms], options: {})
5
5
  params = self.set_params(options: options)
6
- path = 'custom_forms.json?' + params
7
6
  self.execute(path: path)
8
7
  end
9
8
 
@@ -41,8 +41,10 @@ module Samanage
41
41
 
42
42
  # Email is unique so compare first for exact match only. Returns nil or the id
43
43
  def find_user_id_by_email(email: )
44
- api_call = self.check_user(value: email)
45
- api_call[:data].select{|u| u['email'].to_s.downcase == email.to_s.downcase}.first.to_h['id']
44
+ api_call = self.get_users(options: {email: email})
45
+ api_call[:data]
46
+ .select{|u| u['email'].to_s.downcase == email.to_s.downcase}
47
+ .first.to_h['id']
46
48
  end
47
49
 
48
50
  # Returns nil if no matching group_id
@@ -11,8 +11,8 @@ module Samanage
11
11
  self.status_code = nil
12
12
  self.response = response
13
13
  end
14
-
15
14
  puts "[Error] #{self.status_code}: #{self.response}" if options[:verbose]
15
+ return
16
16
  end
17
17
  end
18
18
 
@@ -1,3 +1,3 @@
1
1
  module Samanage
2
- VERSION = '2.1.04'
2
+ VERSION = '2.1.05'
3
3
  end
@@ -3,12 +3,41 @@ require 'samanage'
3
3
  describe Samanage::Api do
4
4
  context 'Attachments' do
5
5
  describe 'API Functions' do
6
- before(:all) do
7
- TOKEN ||= ENV['SAMANAGE_TEST_API_TOKEN']
8
- @samanage = Samanage::Api.new(token: TOKEN)
9
- @users = @samanage.get_users[:data]
10
- @incidents = @samanage.incidents
11
- end
6
+ before(:all) do
7
+ TOKEN ||= ENV['SAMANAGE_TEST_API_TOKEN']
8
+ @samanage = Samanage::Api.new(token: TOKEN)
9
+ @users = @samanage.get_users[:data]
10
+ @incidents = @samanage.incidents
12
11
  end
12
+ # it 'uploads an attachment' do
13
+ # filename = 'sample_file.txt'
14
+ # attachment = File.open(filename)
15
+ # incident_id = @incidents.sample.dig('id')
16
+ # attach = @samanage.upload_attachment(
17
+ # attachment: attachment,
18
+ # attachable_id: incident_id,
19
+ # attachable_type: 'Incident'
20
+ # )
21
+ # expect(attach[:code]).to eq(200).or(201)
22
+ # expect(filename).to eq(attach[:data]['filename'])
23
+ # expect(attachment.size).to eq(attach[:data]['size'])
24
+ # end
25
+ # it 'downloads an attachment' do
26
+ # incident_id = @incidents.sample.dig('id')
27
+ # filename = 'sample_file.txt'
28
+ # attachment = File.open(filename)
29
+ # attach = @samanage.upload_attachment(
30
+ # attachment: attachment,
31
+ # attachable_id: incident_id,
32
+ # attachable_type: 'Incident'
33
+ # )
34
+ # attachable_incident_id = incident_id # Need filter for has attachment?
35
+ # incident = @samanage.find_incident(id: attachable_incident_id, options: {layout: 'long'})
36
+ # attachment = @samanage.download_attachment(attachment: incident[:data]['attachments'].first)
37
+ # expect(attachment.class).to be(File)
38
+ # expect(attach[:code]).to eq(200)
39
+ # expect(attachment.path.split('/').last).to eq(filename)
40
+ # end
41
+ end
13
42
  end
14
43
  end
@@ -21,7 +21,7 @@ describe Samanage::Api do
21
21
  expect(@contracts.size).to eq(contract_count)
22
22
  end
23
23
  it 'create_contract(payload: json): creates a contract' do
24
- random_name = (0...50).map { ('a'..'z').to_a[rand(26)] }.join
24
+ random_name = Faker::Artist.name
25
25
  json = {
26
26
  contract: {
27
27
  type: 'SoftwareLicense',
@@ -36,7 +36,7 @@ describe Samanage::Api do
36
36
  expect(contract_create[:data]['name']).to eq(random_name)
37
37
  end
38
38
  it 'create_contract: fails if no status' do
39
- contract_name = "samanage-ruby-#{(rand*10**10).ceil}"
39
+ contract_name = Faker::Ancient.god
40
40
  json = {
41
41
  :contract => {
42
42
  model: 'test',
@@ -60,7 +60,7 @@ describe Samanage::Api do
60
60
  end
61
61
  it 'update_contract: update_contract by id' do
62
62
  sample_id = @contracts.sample['id']
63
- new_name = (0...50).map {('a'..'z').to_a[rand(26)] }.join
63
+ new_name = Faker::Company.name
64
64
  json = {
65
65
  contract: {
66
66
  manufacturer_name: new_name
@@ -74,10 +74,10 @@ describe Samanage::Api do
74
74
  sample_id = @contracts.sample['id']
75
75
  item = {
76
76
  item: {
77
- name: "name",
77
+ name: Faker::Company.catch_phrase ,
78
78
  version:" 123",
79
79
  qty: "2",
80
- tag: "tag",
80
+ tag: Faker::Company.profession,
81
81
  }
82
82
  }
83
83
  add_item = @samanage.add_item_to_contract(id: sample_id, payload: item)
@@ -9,7 +9,7 @@ describe Samanage::Api do
9
9
  @custom_forms = @controller.collect_custom_forms
10
10
  end
11
11
  it 'collects all custom forms' do
12
- expect(@custom_forms).to be_a(Array)
12
+ expect(@custom_forms).to be_an(Array)
13
13
  end
14
14
  it 'Organizes custom forms by module' do
15
15
  api_call = @controller.organize_forms
@@ -1,4 +1,5 @@
1
1
  require 'samanage'
2
+ require 'faker'
2
3
  describe Samanage::Api do
3
4
  context 'department' do
4
5
  before(:all) do
@@ -19,8 +20,8 @@ describe Samanage::Api do
19
20
  expect(@departments.size).to eq(department_count)
20
21
  end
21
22
  it 'creates a department' do
22
- department_name = "department ##{(rand*10**4).ceil}"
23
- department_description = "Location #{(rand*10**4).ceil}"
23
+ department_name = Faker::Book.title
24
+ department_description = Faker::Book.author
24
25
  payload = {
25
26
  department: {
26
27
  name: department_name,
@@ -26,8 +26,8 @@ describe Samanage::Api do
26
26
  expect(group[:data]).to have_key('name')
27
27
  end
28
28
  it 'creates a group' do
29
- group_name = "Group Name #{(rand*10**4).ceil}"
30
- group_description = "Description #{(rand*10**4).ceil}"
29
+ group_name = Faker::BossaNova.artist
30
+ group_description = Faker::BossaNova.song
31
31
  payload = {
32
32
  group: {
33
33
  name: group_name,
@@ -1,4 +1,5 @@
1
1
  require 'samanage'
2
+ require 'faker'
2
3
  describe Samanage::Api do
3
4
  context 'Hardware' do
4
5
  describe 'API Functions' do
@@ -21,8 +22,8 @@ describe Samanage::Api do
21
22
  expect(@hardwares.size).to eq(hardware_count)
22
23
  end
23
24
  it 'create_hardware(payload: payload): creates a hardware' do
24
- hardware_name = "samanage-ruby-#{(rand*10**10).ceil}"
25
- serial_number = (0...50).map { ('a'..'z').to_a[rand(26)] }.join
25
+ hardware_name = Faker::Science.scientist
26
+ serial_number = Faker::Device.serial
26
27
  payload = {
27
28
  :hardware => {
28
29
  :name => hardware_name,
@@ -70,10 +71,12 @@ describe Samanage::Api do
70
71
  end
71
72
  it 'update_hardware: update_hardware by id' do
72
73
  sample_id = @hardwares.sample['id']
73
- new_name = (0...50).map { ('a'..'z').to_a[rand(26)] }.join
74
+ new_name = [Faker::Dune.planet,Faker::ProgrammingLanguage.name].join('-')
74
75
  payload = {
75
76
  :hardware => {
76
- :name => new_name
77
+ name: new_name,
78
+ tag: Faker::Dune.saying,
79
+ asset_id: Faker::Space.moon
77
80
  }
78
81
  }
79
82
  hardware_update = @samanage.update_hardware(payload: payload, id: sample_id)
@@ -1,4 +1,5 @@
1
1
  require 'samanage'
2
+ require 'faker'
2
3
  describe Samanage::Api do
3
4
  context 'Incidents' do
4
5
  describe 'API Functions' do
@@ -24,12 +25,12 @@ describe Samanage::Api do
24
25
  end
25
26
  it 'create_incident(payload: json): creates a incident' do
26
27
  users_email = @samanage.collect_users.sample['email']
27
- incident_name = "Samanage Ruby Incident"
28
+ incident_name = [Faker::StarWars.specie,Faker::StarWars.planet,Faker::StarWars.droid].shuffle.join(' ')
28
29
  json = {
29
30
  :incident => {
30
31
  :requester => {:email => users_email},
31
32
  :name => incident_name,
32
- :description => "Description"
33
+ :description => Faker::StarWars.quote
33
34
  }
34
35
  }
35
36
  incident_create = @samanage.create_incident(payload: json)
@@ -43,7 +44,7 @@ describe Samanage::Api do
43
44
  json = {
44
45
  :incident => {
45
46
  :requester => {:email => users_email},
46
- :description => "Description"
47
+ :description => Faker::StarWars.quote
47
48
  }
48
49
  }
49
50
  expect{@samanage.create_incident(payload: json)}.to raise_error(Samanage::InvalidRequest)
@@ -73,7 +74,8 @@ describe Samanage::Api do
73
74
  it 'update_incident: update_incident by id' do
74
75
  sample_incident = @incidents.reject{|i| ['Closed','Resolved'].include? i['state']}.sample
75
76
  sample_id = sample_incident['id']
76
- description = (0...500).map { ('a'..'z').to_a[rand(26)] }.join
77
+ name = Faker::Movie.quote
78
+ description = [Faker::String.random,Faker::Seinfeld.quote,Faker::Lorem.paragraph].sample
77
79
  incident_json = {
78
80
  :incident => {
79
81
  :description => description
@@ -22,7 +22,7 @@ describe Samanage::Api do
22
22
  it 'creates a site' do
23
23
  site_name = "Site ##{(rand*10**4).ceil}"
24
24
  site_location = "Location #{(rand*10**4).ceil}"
25
- site_description = "Descrption #{(rand*10**4).ceil}"
25
+ site_description = Faker::Movie.quote
26
26
  payload = {
27
27
  site: {
28
28
  name: site_name,
@@ -20,8 +20,8 @@ describe Samanage::Api do
20
20
  expect(solutions.size).to eq(solution_count)
21
21
  end
22
22
  it 'creates a solution' do
23
- solution_name = "solution ##{(rand*10**4).ceil}"
24
- solution_description = "Description #{(rand*10**4).ceil}"
23
+ solution_name = Faker::Music.album
24
+ solution_description = Faker::Music.band
25
25
  payload = {
26
26
  solution: {
27
27
  name: solution_name,
@@ -36,7 +36,7 @@ describe Samanage::Api do
36
36
  end
37
37
  it 'updates a valid solution' do
38
38
  sample_solution_id = @solutions.sample['id']
39
- new_description = solution_description = "Description #{(rand*10**4).ceil}"
39
+ new_description = Faker::Movie.quote
40
40
  payload = {solution: {description: new_description}}
41
41
  solution_update = @samanage.update_solution(id: sample_solution_id, payload: payload)
42
42
 
@@ -1,4 +1,5 @@
1
1
  require 'samanage'
2
+ require 'faker'
2
3
 
3
4
  describe Samanage::Api do
4
5
  context 'Users' do
@@ -9,8 +10,10 @@ describe Samanage::Api do
9
10
  end
10
11
  describe 'API Functions' do
11
12
  it 'ensures custom field is updated' do
12
- val ="Random #{(rand*10**4).ceil}"
13
- field_name = @samanage.custom_forms['user'][0]['custom_form_fields'].sample.dig('custom_field','name')
13
+ val = Faker::Seinfeld.quote
14
+ field_name = @samanage.custom_forms['user'][0]['custom_form_fields']
15
+ .select{|f| f.dig('custom_field','field_type') == 1} # field_type == 1 is text
16
+ .sample.dig('custom_field','name')
14
17
  payload = {
15
18
  user: {
16
19
  custom_fields_values:{
@@ -38,8 +41,8 @@ describe Samanage::Api do
38
41
  expect(@users.size).to eq(user_count)
39
42
  end
40
43
  it 'create_user(payload: json): creates a user' do
41
- user_name = "autotest-#{(rand*42**100).ceil}"
42
- email = user_name + "@samanage.com"
44
+ user_name = [Faker::Simpsons.character,Faker::StarWars.character,Faker::Name.name].sample
45
+ email = Faker::Internet.safe_email(user_name)
43
46
  json = {
44
47
  :user => {
45
48
  :name => user_name,
@@ -53,7 +56,7 @@ describe Samanage::Api do
53
56
  expect(user_create[:code]).to eq(200).or(201)
54
57
  end
55
58
  it 'create_user: fails if no email' do
56
- user_name = "samanage-ruby-#{(rand*10**(rand(10))).ceil}"
59
+ user_name = Faker::Superhero.name
57
60
  json = {
58
61
  :user => {
59
62
  :name => user_name,
@@ -120,10 +123,11 @@ describe Samanage::Api do
120
123
 
121
124
  it 'update_user: update_user by id' do
122
125
  sample_id = @users.sample['id']
123
- new_name = (0...25).map { ('a'..'z').to_a[rand(26)] }.join
126
+ new_name = [Faker::Simpsons.character,Faker::StarWars.character,Faker::Name.name].sample
124
127
  json = {
125
128
  :user => {
126
- :name => new_name
129
+ name: new_name,
130
+ title: Faker::Job.title
127
131
  }
128
132
  }
129
133
  user_update = @samanage.update_user(payload: json, id: sample_id)
@@ -9,7 +9,7 @@ describe Samanage::Api do
9
9
  @incidents = @samanage.incidents
10
10
  end
11
11
  it 'sends an activation email' do
12
- valid_email = @users.sample['email']
12
+ valid_email = @users.select{|u| !u['disabled'] && !u['last_login']}.sample['email']
13
13
  send_email = @samanage.send_activation_email(email: valid_email)
14
14
  expect(send_email[:code]).to eq(200)
15
15
  end
data/spec/spec_helper.rb CHANGED
@@ -6,4 +6,5 @@ $LOAD_PATH.unshift(spec_dir)
6
6
  $LOAD_PATH.unshift(lib_dir)
7
7
  $LOAD_PATH.uniq!
8
8
 
9
- require 'samanage'
9
+ require 'samanage'
10
+ require 'faker'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: samanage
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.04
4
+ version: 2.1.05
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Walls
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-11 00:00:00.000000000 Z
11
+ date: 2018-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty