From 27fd03e91ea97830409c7881e8d2f80c397dbe88 Mon Sep 17 00:00:00 2001 From: Javier Torres Date: Thu, 9 Aug 2018 17:29:42 +0200 Subject: [PATCH] Test for oauth rakes --- Makefile | 1 + spec/lib/tasks/oauth_rake_spec.rb | 85 +++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 spec/lib/tasks/oauth_rake_spec.rb diff --git a/Makefile b/Makefile index ab0bf5b25a..41547c2559 100644 --- a/Makefile +++ b/Makefile @@ -326,6 +326,7 @@ SPEC_HELPER_MIN_SPECS = \ spec/lib/carto/visualization_invalidation_service_spec.rb \ spec/lib/tasks/layers_rake_spec.rb \ spec/lib/tasks/fix_unique_legends_spec.rb \ + spec/lib/tasks/oauth_rake_spec.rb \ spec/models/carto/username_proposer_spec.rb \ spec/services/carto/overquota_users_service_spec.rb \ spec/services/visualization/common_data_service_spec.rb \ diff --git a/spec/lib/tasks/oauth_rake_spec.rb b/spec/lib/tasks/oauth_rake_spec.rb new file mode 100644 index 0000000000..31f491dbe6 --- /dev/null +++ b/spec/lib/tasks/oauth_rake_spec.rb @@ -0,0 +1,85 @@ +require 'spec_helper_min' +require 'rake' + +describe 'oauth.rake' do + before(:all) do + Rake.application.rake_require('tasks/oauth') + Rake::Task.define_task(:environment) + + @sequel_developer = FactoryGirl.create(:valid_user) + @developer = Carto::User.find(@sequel_developer.id) + @user = FactoryGirl.create(:valid_user) + @oauth_app = FactoryGirl.create(:oauth_app, user: @developer) + end + + before(:each) do + @oauth_app_user = @oauth_app.oauth_app_users.create!(user_id: @user.id) + end + + after(:each) do + @oauth_app_user.reload.destroy! + end + + after(:all) do + @oauth_app.destroy! + @user.destroy + @sequel_developer.destroy + end + + describe '#destroy_expired_access_tokens' do + before(:each) do + Rake::Task['cartodb:oauth:destroy_expired_access_tokens'].reenable + end + + it 'does not delete just created access tokens' do + access_token = @oauth_app_user.oauth_access_tokens.create! + Rake::Task['cartodb:oauth:destroy_expired_access_tokens'].invoke + expect(Carto::OauthAccessToken.exists?(access_token.id)).to(be_true) + end + + it 'deletes old access tokens' do + access_token = @oauth_app_user.oauth_access_tokens.create! + Delorean.jump(2.hours) + Rake::Task['cartodb:oauth:destroy_expired_access_tokens'].invoke + expect(Carto::OauthAccessToken.exists?(access_token.id)).to(be_false) + end + end + + describe '#destroy_expired_refresh_tokens' do + before(:each) do + Rake::Task['cartodb:oauth:destroy_expired_refresh_tokens'].reenable + end + + it 'does not delete just created access tokens' do + refresh_token = @oauth_app_user.oauth_refresh_tokens.create!(scopes: ['offline']) + Rake::Task['cartodb:oauth:destroy_expired_refresh_tokens'].invoke + expect(Carto::OauthRefreshToken.exists?(refresh_token.id)).to(be_true) + end + + it 'deletes old access tokens' do + refresh_token = @oauth_app_user.oauth_refresh_tokens.create!(scopes: ['offline']) + Delorean.jump(1.year) + Rake::Task['cartodb:oauth:destroy_expired_refresh_tokens'].invoke + expect(Carto::OauthRefreshToken.exists?(refresh_token.id)).to(be_false) + end + end + + describe '#destroy_expired_authorization_codes' do + before(:each) do + Rake::Task['cartodb:oauth:destroy_expired_authorization_codes'].reenable + end + + it 'does not delete just created access tokens' do + authorization_code = @oauth_app_user.oauth_authorization_codes.create! + Rake::Task['cartodb:oauth:destroy_expired_authorization_codes'].invoke + expect(Carto::OauthAuthorizationCode.exists?(authorization_code.id)).to(be_true) + end + + it 'deletes old access tokens' do + authorization_code = @oauth_app_user.oauth_authorization_codes.create! + Delorean.jump(2.minutes) + Rake::Task['cartodb:oauth:destroy_expired_authorization_codes'].invoke + expect(Carto::OauthAuthorizationCode.exists?(authorization_code.id)).to(be_false) + end + end +end