Skip to content

Commit f1d9ca4

Browse files
committed
test: add unit tests for helper authentication parameters in HelperAuthenticationTest
1 parent 42cd448 commit f1d9ca4

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../test_helper"
4+
5+
class HelperAuthenticationTest < Minitest::Test
6+
def test_should_return_correct_authentication_parameters_with_provided_token_and_expire
7+
private_key = "private_key_test"
8+
client = Imagekit::Client.new(private_key: private_key)
9+
10+
token = "your_token"
11+
expire = 1_582_269_249
12+
13+
params = client.helper.get_authentication_parameters(token: token, expire: expire)
14+
15+
# Expected exact match with Node.js output
16+
expected_signature = "e71bcd6031016b060d349d212e23e85c791decdd"
17+
18+
assert_equal(token, params[:token])
19+
assert_equal(expire, params[:expire])
20+
assert_equal(expected_signature, params[:signature])
21+
end
22+
23+
def test_should_return_authentication_parameters_with_required_properties_when_no_params_provided
24+
private_key = "private_key_test"
25+
client = Imagekit::Client.new(private_key: private_key)
26+
27+
params = client.helper.get_authentication_parameters
28+
29+
# Check that all required properties exist
30+
assert(params.key?(:token), "Expected token parameter")
31+
assert(params.key?(:expire), "Expected expire parameter")
32+
assert(params.key?(:signature), "Expected signature parameter")
33+
34+
# Token should be a 32-character hex string
35+
token = params[:token]
36+
assert_instance_of(String, token)
37+
assert_match(/^[0-9a-f]{32}$/, token, "Expected token to be 32-character hex format")
38+
39+
# Expire should be a number greater than current time
40+
expire = params[:expire]
41+
assert_instance_of(Integer, expire)
42+
current_time = Time.now.to_i
43+
assert(expire > current_time, "Expected expire #{expire} to be greater than current time #{current_time}")
44+
45+
# Signature should be a hex string (40 characters for HMAC-SHA1)
46+
signature = params[:signature]
47+
assert_instance_of(String, signature)
48+
assert_match(/^[a-f0-9]{40}$/, signature, "Expected signature to be 40 character hex string")
49+
end
50+
51+
def test_should_handle_edge_case_with_expire_time_0
52+
private_key = "private_key_test"
53+
client = Imagekit::Client.new(private_key: private_key)
54+
55+
token = "test-token"
56+
expire = 0
57+
58+
params = client.helper.get_authentication_parameters(token: token, expire: expire)
59+
60+
assert_equal(token, params[:token])
61+
62+
# When expire is 0 (falsy), it should use default expire time (30 minutes from now)
63+
expire_result = params[:expire]
64+
assert_instance_of(Integer, expire_result)
65+
expected_expire = Time.now.to_i + (60 * 30)
66+
# Allow a 10 second tolerance for test execution time
67+
assert(
68+
expire_result >= expected_expire - 10 && expire_result <= expected_expire + 10,
69+
"Expected expire to be close to #{expected_expire} (30 minutes from now), got #{expire_result}"
70+
)
71+
72+
# Signature should be a hex string (40 characters for HMAC-SHA1)
73+
signature = params[:signature]
74+
assert_instance_of(String, signature)
75+
assert_match(/^[a-f0-9]{40}$/, signature, "Expected signature to be 40 character hex string")
76+
end
77+
78+
def test_should_handle_empty_string_token
79+
private_key = "private_key_test"
80+
client = Imagekit::Client.new(private_key: private_key)
81+
82+
token = "" # Empty string is falsy
83+
expire = 1_582_269_249
84+
85+
params = client.helper.get_authentication_parameters(token: token, expire: expire)
86+
87+
# Since empty string is falsy, it should generate a token
88+
token_result = params[:token]
89+
assert_instance_of(String, token_result)
90+
refute_empty(token_result, "Expected token to be generated when empty string is provided")
91+
assert_match(/^[0-9a-f]{32}$/, token_result, "Expected generated token to be 32-character hex format")
92+
93+
assert_equal(expire, params[:expire])
94+
95+
# Signature should be a hex string (40 characters for HMAC-SHA1)
96+
signature = params[:signature]
97+
assert_instance_of(String, signature)
98+
assert_match(/^[a-f0-9]{40}$/, signature, "Expected signature to be 40 character hex string")
99+
end
100+
101+
def test_should_raise_error_when_private_key_is_not_provided
102+
# Test with empty private key
103+
assert_raises(ArgumentError, "Expected error when private key is empty") do
104+
client = Imagekit::Client.new(private_key: "")
105+
client.helper.get_authentication_parameters(token: "test", expire: 123)
106+
end
107+
end
108+
109+
def test_should_raise_error_when_private_key_is_nil
110+
# Test with nil private key
111+
assert_raises(ArgumentError, "Expected error when private key is nil") do
112+
client = Imagekit::Client.new(private_key: nil)
113+
client.helper.get_authentication_parameters(token: "test", expire: 123)
114+
end
115+
end
116+
end

0 commit comments

Comments
 (0)