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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +2 -2
- data/lib/newman_scenario/cli.rb +2 -0
- data/lib/newman_scenario/scenario.rb +28 -24
- data/lib/newman_scenario/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 974c64e5aba070897acfef6b3156b1e72373760c2daabeee9abc5972c30f60d6
|
4
|
+
data.tar.gz: 5b4c7294a9dd87669272f4c4632e4c165872d7840b5b50ec9ca59437a0db13cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 607faa6bb4ab1a8cc839028146f0c61b80463433c89a9b4ec83931d2eca5693ec1e2f6d00437a1145c774f39c8fc6c90ffe270cf95416c56929f1021681b3875
|
7
|
+
data.tar.gz: 453b185f054f60e2c6a3fb429a2627ffd8294098d33b6dd417030710dcfae0d08905dbe26264eedab66c9e1548a6dfadb2d84e5d82013e420c089cc43b97ea07
|
data/Gemfile.lock
CHANGED
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
|
|
data/lib/newman_scenario/cli.rb
CHANGED
@@ -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
|
-
|
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.
|
33
|
-
|
34
|
-
|
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
|
-
|
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 =
|
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
|