-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.rb
More file actions
133 lines (94 loc) · 3.31 KB
/
client_test.rb
File metadata and controls
133 lines (94 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
require "test_helper"
class ClientTest < Minitest::Test
LIZARD_ENV_KEYS = %w[LIZARD_API_KEY LIZARD_URL].freeze
def setup
@original_env = LIZARD_ENV_KEYS.to_h { |k| [k, ENV[k]] }
end
def teardown
Lizard.api_key = nil
Lizard.url = nil
LIZARD_ENV_KEYS.each { |k| ENV[k] = @original_env[k] }
end
def test_initialize_accepts_parameters
client = Lizard::Client.new(api_key: "test_key", url: "https://test.example.com")
assert_instance_of Lizard::Client, client
end
def test_initialize_with_lizard_module_fallback
Lizard.api_key = "module_key"
Lizard.url = "https://module.example.com"
client = Lizard::Client.new
assert_instance_of Lizard::Client, client
end
def test_send_test_run_returns_early_when_not_configured
ENV.delete("LIZARD_API_KEY")
ENV.delete("LIZARD_URL")
client = Lizard::Client.new(api_key: nil, url: nil)
result = client.send_test_run({test: "data"})
assert_nil result
end
def test_send_test_run_success
response = mock
response.stubs(:code).returns("200")
http = mock
http.stubs(:use_ssl=)
http.stubs(:request).returns(response)
Net::HTTP.stubs(:new).returns(http)
client = Lizard::Client.new(api_key: "test_key", url: "https://test.example.com")
out, _err = capture_io do
client.send_test_run({test: "data"})
end
assert_includes out, "📊 Sent test results to Lizard: 200"
end
def test_send_test_run_handles_exceptions
Net::HTTP.stubs(:new).raises(StandardError.new("Connection failed"))
client = Lizard::Client.new(api_key: "test_key", url: "https://test.example.com")
out, _err = capture_io do
client.send_test_run({test: "data"})
end
assert_includes out, "❌ Failed to send to Lizard: Connection failed"
end
def test_send_test_run_handles_4xx_errors
response = mock
response.stubs(:code).returns("400")
response.stubs(:body).returns("Bad Request")
http = mock
http.stubs(:use_ssl=)
http.stubs(:request).returns(response)
Net::HTTP.stubs(:new).returns(http)
client = Lizard::Client.new(api_key: "test_key", url: "https://test.example.com")
out, _err = capture_io do
client.send_test_run({test: "data"})
end
assert_includes out, "❌ Lizard API error (400): Bad Request"
end
def test_send_test_run_sets_ssl_for_https_url
response = mock
response.stubs(:code).returns("200")
http = mock
http.expects(:use_ssl=).with(true)
http.stubs(:request).returns(response)
Net::HTTP.stubs(:new).returns(http)
client = Lizard::Client.new(api_key: "test_key", url: "https://test.example.com")
capture_io do
client.send_test_run({test: "data"})
end
end
def test_send_test_run_sets_ssl_false_for_http_url
response = mock
response.stubs(:code).returns("200")
http = mock
http.expects(:use_ssl=).with(false)
http.stubs(:request).returns(response)
Net::HTTP.stubs(:new).returns(http)
client = Lizard::Client.new(api_key: "test_key", url: "http://test.example.com")
capture_io do
client.send_test_run({test: "data"})
end
end
def test_initialize_with_environment_variables
ENV["LIZARD_API_KEY"] = "env_key"
ENV["LIZARD_URL"] = "https://env.example.com"
client = Lizard::Client.new
assert_instance_of Lizard::Client, client
end
end