forked from saasbook/flextensions
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add token encryption/decryption test
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# spec/models/lms_credential_spec.rb | ||
require 'rails_helper' | ||
|
||
class MockCanvas | ||
# Simulate authentication using a token and refresh token | ||
def self.authenticate(token, refresh_token) | ||
token == 'sensitive_token' && refresh_token == 'sensitive_refresh_token' | ||
end | ||
|
||
# Simulate retrieving a service, returning 'service_object' if credentials are valid | ||
def self.mock_get_service(token, refresh_token) | ||
authenticate(token, refresh_token) ? 'service_object' : nil | ||
end | ||
end | ||
|
||
|
||
RSpec.describe LmsCredential, type: :model do | ||
describe 'Token Encryption' do | ||
let(:user) { User.create!(email: '[email protected]') } | ||
let!(:credential) do | ||
LmsCredential.create!( | ||
user: user, | ||
lms_name: 'ExampleLMS', | ||
username: 'testuser', | ||
password: 'testpassword', | ||
token: 'sensitive_token', | ||
refresh_token: 'sensitive_refresh_token' | ||
) | ||
end | ||
|
||
it 'encrypts the token and refresh_token' do | ||
raw_token = ActiveRecord::Base.connection.execute( | ||
"SELECT token FROM lms_credentials WHERE id = #{credential.id}" | ||
).first['token'] | ||
raw_refresh_token = ActiveRecord::Base.connection.execute( | ||
"SELECT refresh_token FROM lms_credentials WHERE id = #{credential.id}" | ||
).first['refresh_token'] | ||
|
||
expect(raw_token).not_to eq 'sensitive_token' | ||
expect(raw_refresh_token).not_to eq 'sensitive_refresh_token' | ||
expect(credential.token).to eq 'sensitive_token' | ||
expect(credential.refresh_token).to eq 'sensitive_refresh_token' | ||
end | ||
|
||
it 'decrypts the token and refresh_token for use' do | ||
expect(credential.token).to eq('sensitive_token') | ||
expect(credential.refresh_token).to eq('sensitive_refresh_token') | ||
|
||
# Simulate a call to get a service object | ||
expect(MockCanvas.mock_get_service(credential.token, credential.refresh_token)).to eq('service_object') | ||
end | ||
end | ||
end |