|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe GithubErrorHandler do |
| 6 | + let(:user) { FactoryBot.create(:user) } |
| 7 | + describe '.process_github_error' do |
| 8 | + context 'The error is UserNotFoundOnGithubError' do |
| 9 | + let(:error) { GithubPullRequestService::UserNotFoundOnGithubError.new } |
| 10 | + |
| 11 | + it 'calls process_user_missing_error' do |
| 12 | + expect(GithubErrorHandler) |
| 13 | + .to receive(:process_user_missing_error).with(error, user) |
| 14 | + |
| 15 | + GithubErrorHandler.process_github_error(error, user) |
| 16 | + end |
| 17 | + |
| 18 | + it 'correctly updates the last_error column on the user' do |
| 19 | + GithubErrorHandler.process_github_error(error, user) |
| 20 | + |
| 21 | + expect(user.last_error).to eq(error.class.to_s) |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + context 'The error is Octokit::AccountSuspended' do |
| 26 | + let(:error) { Octokit::AccountSuspended.new } |
| 27 | + |
| 28 | + it 'calls process_user_missing_error' do |
| 29 | + expect(GithubErrorHandler) |
| 30 | + .to receive(:process_user_missing_error).with(error, user) |
| 31 | + |
| 32 | + GithubErrorHandler.process_github_error(error, user) |
| 33 | + end |
| 34 | + |
| 35 | + it 'correctly updates the last_error column on the user' do |
| 36 | + GithubErrorHandler.process_github_error(error, user) |
| 37 | + |
| 38 | + expect(user.last_error).to eq(error.class.to_s) |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + context 'The error is an unhandled error' do |
| 43 | + let(:error) { StandardError.new } |
| 44 | + |
| 45 | + it 'raises the error' do |
| 46 | + expect { GithubErrorHandler.process_github_error(error, user) } |
| 47 | + .to raise_error(StandardError) |
| 48 | + end |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + describe '.process_user_missing_error' do |
| 53 | + let(:error) { GithubPullRequestService::UserNotFoundOnGithubError.new } |
| 54 | + it 'destroys the UserStat' do |
| 55 | + UserStat.create(user_id: user.id, data: user) |
| 56 | + |
| 57 | + GithubErrorHandler.process_user_missing_error(error, user) |
| 58 | + |
| 59 | + expect(UserStat.count).to eq(0) |
| 60 | + end |
| 61 | + |
| 62 | + it 'deactivates the user' do |
| 63 | + # to pass user#deactivate validations |
| 64 | + allow(user) |
| 65 | + .to receive(:last_error).and_return('Octokit::AccountSuspended') |
| 66 | + |
| 67 | + GithubErrorHandler.process_user_missing_error(error, user) |
| 68 | + |
| 69 | + expect(user.state).to eq('inactive') |
| 70 | + end |
| 71 | + end |
| 72 | +end |
0 commit comments