Skip to content
This repository was archived by the owner on Mar 29, 2022. It is now read-only.

Commit b5c04e0

Browse files
committed
Add tests for GithubErrorHandler
1 parent fba79a5 commit b5c04e0

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

app/services/github_error_handler.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ def process_github_error(error, user)
2929
end
3030

3131
def process_user_missing_error(_error, user)
32+
user.deactivate
33+
3234
return unless (user_stat = UserStat.where(user_id: user.id).first)
3335

3436
user_stat.destroy
35-
36-
user.deactivate
3737
end
3838
end
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)