-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify_auth.rb
More file actions
72 lines (56 loc) · 2.13 KB
/
Copy pathverify_auth.rb
File metadata and controls
72 lines (56 loc) · 2.13 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
require 'minitest/autorun'
require 'mocha/minitest'
require 'json'
require_relative 'backend-server/lib/auth_service'
# Mock the DB constant
class MockDB
def exec_params(query, params)
# Return a dummy result object
end
end
DB = MockDB.new
class TestAuthService < Minitest::Test
def setup
# Reset mocks
end
def test_verify_login_success
user_id = 1
attempt_data = [
{ 'pair' => 'ab', 'dwell' => 100, 'flight' => 50 },
{ 'pair' => 'bc', 'dwell' => 110, 'flight' => 60 }
]
# Mock DB response for profile fetch
mock_result = mock()
mock_result.stubs(:ntuples).returns(2)
mock_result.stubs(:each).yields({
'key_pair' => 'ab', 'avg_dwell_time' => '100', 'avg_flight_time' => '50', 'sample_count' => '10'
}).yields({
'key_pair' => 'bc', 'avg_dwell_time' => '110', 'avg_flight_time' => '60', 'sample_count' => '10'
})
DB.expects(:exec_params).with(regexp_matches(/SELECT key_pair/), [user_id]).returns(mock_result)
# Mock MathEngine to return a perfect match
MathEngine.expects(:get_score).returns(5.0) # < 10.0 is perfect match
# Mock DB update
DB.expects(:exec_params).with(regexp_matches(/UPDATE biometric_profiles/), anything).twice
result = AuthService.verify_login(user_id, attempt_data)
assert_equal "SUCCESS", result[:status]
assert_equal 5.0, result[:score]
end
def test_verify_login_imposter
user_id = 1
attempt_data = [
{ 'pair' => 'ab', 'dwell' => 200, 'flight' => 150 } # Very different
]
mock_result = mock()
mock_result.stubs(:ntuples).returns(1)
mock_result.stubs(:each).yields({
'key_pair' => 'ab', 'avg_dwell_time' => '100', 'avg_flight_time' => '50', 'sample_count' => '10'
})
DB.expects(:exec_params).with(regexp_matches(/SELECT key_pair/), [user_id]).returns(mock_result)
MathEngine.expects(:get_score).returns(50.0) # > 20.0 is imposter
# Should NOT update profile
DB.expects(:exec_params).with(regexp_matches(/UPDATE biometric_profiles/), anything).never
result = AuthService.verify_login(user_id, attempt_data)
assert_equal "DENIED", result[:status]
end
end