Skip to content

Commit e58951c

Browse files
committed
Fix tests
1 parent 8ba8a79 commit e58951c

2 files changed

Lines changed: 11 additions & 29 deletions

File tree

test/integration/by_access_token_integration_test.rb

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,8 @@ def test_index_with_access_token
9191
get admin_api_accounts_path(format: :xml), params: { access_token: legacy_value }
9292

9393
assert_response :success
94-
# Token should be migrated to hashed value
95-
assert_equal AccessToken::HASHED_TOKEN_LENGTH, token.reload.read_attribute(:value).length
96-
end
97-
98-
test 'authentication with legacy unmigrated token migrates the token' do
99-
token = FactoryBot.create(:access_token, owner: @user, scopes: 'account_management')
100-
legacy_value = 'legacy_plaintext_token_for_integration'
101-
token.update_columns(value: legacy_value)
102-
103-
get admin_api_accounts_path(format: :xml), params: { access_token: legacy_value }
104-
105-
assert_equal AccessToken::HASHED_TOKEN_LENGTH, token.reload.read_attribute(:value).length
94+
# No migration: DB value remains unchanged
95+
assert_equal legacy_value, token.reload.read_attribute(:value)
10696
end
10797

10898
test 'authentication with leaked database hash fails' do
@@ -116,8 +106,8 @@ def test_index_with_access_token
116106
# Get the actual hash stored in the database
117107
leaked_hash = token.reload.read_attribute(:value)
118108

119-
# Verify we have a 96-char hash
120-
assert_equal AccessToken::HASHED_TOKEN_LENGTH, leaked_hash.length
109+
# Verify the stored value has our prefix
110+
assert leaked_hash.start_with?(AccessToken::DIGEST_PREFIX)
121111

122112
# An attacker trying to use the leaked hash directly should be blocked
123113
get admin_api_accounts_path(format: :xml), params: { access_token: leaked_hash }

test/models/access_token_test.rb

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def test_find_from_value_finds_new_token_by_digest
110110
found = AccessToken.find_from_value(@token.plaintext_value)
111111

112112
assert_equal @token.id, found&.id
113-
assert_equal AccessToken::HASHED_TOKEN_LENGTH, @token.reload.read_attribute(:value).length
113+
assert @token.reload.read_attribute(:value).start_with?(AccessToken::DIGEST_PREFIX)
114114
end
115115

116116
def test_find_from_value_finds_legacy_token
@@ -120,26 +120,18 @@ def test_find_from_value_finds_legacy_token
120120
found = AccessToken.find_from_value(legacy_value)
121121

122122
assert_equal @token.id, found&.id
123-
end
124-
125-
def test_find_from_value_migrates_legacy_token_to_hash
126-
legacy_value = 'legacy_plaintext_token_value_64chars'
127-
@token.update_columns(value: legacy_value)
128-
129-
AccessToken.find_from_value(legacy_value)
130-
131-
assert_equal AccessToken::HASHED_TOKEN_LENGTH, @token.reload.read_attribute(:value).length
132-
assert_equal AccessToken.compute_digest(legacy_value), @token.read_attribute(:value)
123+
# No migration: DB value remains unchanged
124+
assert_equal legacy_value, @token.reload.read_attribute(:value)
133125
end
134126

135127
def test_find_from_value_rejects_leaked_hash_as_token
136-
leaked_hash = @token.reload.read_attribute(:value)
128+
stored_hash = @token.reload.read_attribute(:value)
137129

138-
# Verify the hash is 96 chars (our security boundary)
139-
assert_equal AccessToken::HASHED_TOKEN_LENGTH, leaked_hash.length
130+
# Verify the DB value has our prefix
131+
assert stored_hash.start_with?(AccessToken::DIGEST_PREFIX)
140132

141133
# An attacker with access to the DB hash should NOT be able to authenticate
142-
found = AccessToken.find_from_value(leaked_hash)
134+
found = AccessToken.find_from_value(stored_hash)
143135

144136
assert_nil found, "Security vulnerability: leaked hash was accepted as a valid token"
145137
end

0 commit comments

Comments
 (0)