newman_scenario 0.1.1 → 0.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 559c5e124ccffa5e4f98839bc34a87351203e13f977dbc07820fe7f7cdf9720b
4
- data.tar.gz: a0935c087ed1e143699f7932c04965845b90c90cecd66cedfe21a58056e9861f
3
+ metadata.gz: 974c64e5aba070897acfef6b3156b1e72373760c2daabeee9abc5972c30f60d6
4
+ data.tar.gz: 5b4c7294a9dd87669272f4c4632e4c165872d7840b5b50ec9ca59437a0db13cd
5
5
  SHA512:
6
- metadata.gz: 5da5a15b5291c5a38224b6e352181f876a2451c202af0e396dbb3500884e63e329a4512c9110bf1438e573955356fee7d353a1685f55d8d6cf0e83bd0033ceff
7
- data.tar.gz: 849b32f48312ae202757ffb1b17197a9936fd0d4671b5196ad507b0fe8a3e891108f3efe94e872f919cd54c43748985eb23ff036fad2d5ecb70aa3bfb36d179a
6
+ metadata.gz: 607faa6bb4ab1a8cc839028146f0c61b80463433c89a9b4ec83931d2eca5693ec1e2f6d00437a1145c774f39c8fc6c90ffe270cf95416c56929f1021681b3875
7
+ data.tar.gz: 453b185f054f60e2c6a3fb429a2627ffd8294098d33b6dd417030710dcfae0d08905dbe26264eedab66c9e1548a6dfadb2d84e5d82013e420c089cc43b97ea07
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- newman_scenario (0.1.1)
4
+ newman_scenario (0.1.2)
5
5
  dotenv (= 2.7.5)
6
6
  httparty (= 0.16.2)
7
7
  thor (= 1.0.1)
data/README.md CHANGED
@@ -113,10 +113,10 @@ NewmanScenario::Scenario.new.run(scenario_name: 'Signup', environment_name: 'sta
113
113
  - [x] `NewmanScenario::Scenario.run`
114
114
  - [ ] Specs :(
115
115
  - [x] `newman_scenario` cli
116
- - [x] Configure using `NewmanScenario::Scenario.configure`
116
+ - [x] Configure using `NewmanScenario::Scenario.configure` or `newman_scenario configure`
117
+ - [x] Fetch available collections and environments from Postman
117
118
  - [ ] Support for custom scenario variable
118
119
  - [ ] Support for local environment (no synchronised with Postman)
119
- - [ ] Fetch available collections and environments from Postman
120
120
 
121
121
  ## Development
122
122
 
@@ -2,6 +2,8 @@ require 'thor'
2
2
 
3
3
  module NewmanScenario
4
4
  class CLI < Thor
5
+ default_task :run_scenario
6
+
5
7
  desc "run_scenario environment scenario", "Run scenario using environment"
6
8
  long_desc <<~EOF
7
9
 
@@ -25,23 +25,30 @@ module NewmanScenario
25
25
  attr_accessor :default_last_scenario_file_path
26
26
 
27
27
  def configure(default_api_key: nil, default_collection_id: nil, default_environment_ids: nil, default_custom_scenarios_file_path: nil, default_last_scenario_file_path: nil)
28
- self.default_api_key = default_api_key || prompt.ask('Postman API Key:')
29
- self.default_collection_id = default_collection_id || prompt.ask('Postman Collection Id:')
28
+ self.default_api_key = default_api_key || prompt.ask('Postman API Key (https://YOURPOSTMAN.postman.co/settings/me/api-keys):', value: ENV['POSTMAN_API_KEY'].to_s)
29
+ collections = fetch_postman('/collections', api_key: self.default_api_key).parsed_response&.fetch('collections', nil) || []
30
+ collections = collections.map { |collection| collection.slice('name', 'id').values }.to_h
31
+ self.default_collection_id = default_collection_id || prompt.select('Postman Collection', collections, default: 1)
30
32
  self.default_environment_ids = default_environment_ids
31
33
  unless self.default_environment_ids
32
- self.default_environment_ids = {}
33
- loop do
34
- break unless prompt.yes?('Add environment?')
35
-
36
- name = prompt.ask('Environment Name:')
37
- id = prompt.ask('Environment Id:')
38
- self.default_environment_ids[name] = id
39
- end
34
+ environments = fetch_postman('/environments', api_key: self.default_api_key).parsed_response&.fetch('environments', nil) || []
35
+ environments = environments.map { |environment| environment.slice('name', 'id').values }.to_h
36
+ environment_ids = prompt.multi_select('Postman Collection', environments)
37
+ self.default_environment_ids = environments.select { |_, id| environment_ids.include?(id) }
40
38
  end
41
39
  self.default_custom_scenarios_file_path = default_custom_scenarios_file_path || prompt.ask('Custom scenarios file path:', value: DEFAULT_CUSTOM_SCENARIOS_FILE_PATH)
42
40
  self.default_last_scenario_file_path = default_last_scenario_file_path || prompt.ask('Last scenario file path:', value: DEFAULT_LAST_SCENARIO_FILE_PATH)
43
41
  if (env_path = prompt.ask('Save to: [enter to not save]', value: '.env'))
44
- File.open(env_path, 'a') do |file|
42
+ envs = {
43
+ POSTMAN_API_KEY: self.default_api_key,
44
+ NEWMAN_SCENARIO_COLLECTION_ID: self.default_collection_id,
45
+ NEWMAN_SCENARIO_ENVIRONMENTS: self.default_environment_ids.to_json,
46
+ NEWMAN_SCENARIO_CUSTOM_COLLECTION_FILE_PATH: self.default_custom_scenarios_file_path,
47
+ NEWMAN_SCENARIO_LAST_SCENARIO_FILE_PATH: self.default_last_scenario_file_path,
48
+ }
49
+ existing_lines = File.readlines(env_path).reject { |line| envs.keys.include?(line.split(':').first.to_sym) }
50
+ File.open(env_path, 'w+') do |file|
51
+ existing_lines.each { |line| file.puts line }
45
52
  file.puts "POSTMAN_API_KEY: #{self.default_api_key}"
46
53
  file.puts "NEWMAN_SCENARIO_COLLECTION_ID: #{self.default_collection_id}"
47
54
  file.puts "NEWMAN_SCENARIO_ENVIRONMENTS: #{self.default_environment_ids.to_json}"
@@ -51,8 +58,15 @@ module NewmanScenario
51
58
  end
52
59
  end
53
60
 
61
+ def fetch_postman(url_path, expected_response_codes: [200], api_key: nil)
62
+ response = HTTParty.get("https://api.getpostman.com#{url_path}", headers: { 'X-Api-Key' => api_key})
63
+ raise Error, "Invalid response code: #{response.code}" unless expected_response_codes.include?(response.code)
64
+
65
+ response
66
+ end
67
+
54
68
  private
55
-
69
+
56
70
  def prompt
57
71
  @prompt ||= TTY::Prompt.new
58
72
  end
@@ -91,7 +105,6 @@ module NewmanScenario
91
105
  def run(environment_name: nil, scenario_name: nil, bail: true, no_prompt: false)
92
106
  return if `which newman`.empty? && !prompt_to_install_newman
93
107
 
94
- prompt_to_set_api_key unless api_key
95
108
  environment = environment_ids[environment_name.to_sym] if environment_name
96
109
  environment ||= prompt.select("Environment", environment_ids, default: 1)
97
110
  load_postman_environment(environment, no_prompt: no_prompt)
@@ -176,13 +189,6 @@ module NewmanScenario
176
189
  cmd('brew install newman')
177
190
  end
178
191
 
179
- def prompt_to_set_api_key
180
- prompt.warn 'Missing Postman API Key'
181
- prompt.warn 'Get one from: https://YOURPOSTMAN.postman.co/settings/me/api-keys'
182
- self.api_key = prompt.ask('Postman API Key')
183
- cmd("echo \"POSTMAN_API_KEY: #{self.api_key}\" >> .env")
184
- end
185
-
186
192
  def load_postman_environment(environment, no_prompt: false)
187
193
  reload =
188
194
  if no_prompt
@@ -204,10 +210,8 @@ module NewmanScenario
204
210
  end
205
211
  end
206
212
 
207
- def fetch_postman_to_file(url_path, file_path)
208
- response = HTTParty.get("https://api.getpostman.com/#{url_path}", headers: { 'X-Api-Key' => api_key})
209
- raise Error, "Invalid response code: #{response.code}" unless response.code == 200
210
-
213
+ def fetch_postman_to_file(url_path, file_path, expected_response_codes: [200])
214
+ response = self.class.fetch_postman(url_path, expected_response_codes: expected_response_codes, api_key: api_key)
211
215
  File.open(file_path, 'w+') do |file|
212
216
  file.puts response.body
213
217
  end
@@ -1,3 +1,3 @@
1
1
  module NewmanScenario
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: newman_scenario
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hugues Bernet-Rollande