diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-07-19 15:09:10 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-07-19 15:09:10 +0000 |
commit | 9c8e8b5ffc6e11d827fa42f2dce5f90c4dc19493 (patch) | |
tree | fef494515627439a22a06addc7ff5f57d418778a /spec/models/hooks | |
parent | 690c904b5e340f14c04f93ff688b647b46f7d1a2 (diff) | |
download | gitlab-ce-9c8e8b5ffc6e11d827fa42f2dce5f90c4dc19493.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/models/hooks')
-rw-r--r-- | spec/models/hooks/project_hook_spec.rb | 71 | ||||
-rw-r--r-- | spec/models/hooks/web_hook_spec.rb | 15 |
2 files changed, 82 insertions, 4 deletions
diff --git a/spec/models/hooks/project_hook_spec.rb b/spec/models/hooks/project_hook_spec.rb index 4253686b843..923a6f92424 100644 --- a/spec/models/hooks/project_hook_spec.rb +++ b/spec/models/hooks/project_hook_spec.rb @@ -15,6 +15,19 @@ RSpec.describe ProjectHook do subject { build(:project_hook, project: create(:project)) } end + describe '.for_projects' do + it 'finds related project hooks' do + hook_a = create(:project_hook) + hook_b = create(:project_hook) + hook_c = create(:project_hook) + + expect(described_class.for_projects([hook_a.project, hook_b.project])) + .to contain_exactly(hook_a, hook_b) + expect(described_class.for_projects(hook_c.project)) + .to contain_exactly(hook_c) + end + end + describe '.push_hooks' do it 'returns hooks for push events only' do hook = create(:project_hook, push_events: true) @@ -50,4 +63,62 @@ RSpec.describe ProjectHook do ) end end + + describe '#update_last_failure', :clean_gitlab_redis_shared_state do + let_it_be(:hook) { create(:project_hook) } + + it 'is a method of this class' do + expect { hook.update_last_failure }.not_to raise_error + end + + context 'when the hook is executable' do + it 'does not update the state' do + expect(Gitlab::Redis::SharedState).not_to receive(:with) + + hook.update_last_failure + end + end + + context 'when the hook is failed' do + before do + allow(hook).to receive(:executable?).and_return(false) + end + + def last_failure + Gitlab::Redis::SharedState.with do |redis| + redis.get("web_hooks:last_failure:project-#{hook.project.id}") + end + end + + context 'there is no prior value', :freeze_time do + it 'updates the state' do + expect { hook.update_last_failure }.to change { last_failure }.to(Time.current) + end + end + + context 'there is a prior value, from before now' do + it 'updates the state' do + the_future = 1.minute.from_now + + hook.update_last_failure + + travel_to(the_future) do + expect { hook.update_last_failure }.to change { last_failure }.to(the_future.iso8601) + end + end + end + + context 'there is a prior value, from after now' do + it 'does not update the state' do + the_past = 1.minute.ago + + hook.update_last_failure + + travel_to(the_past) do + expect { hook.update_last_failure }.not_to change { last_failure } + end + end + end + end + end end diff --git a/spec/models/hooks/web_hook_spec.rb b/spec/models/hooks/web_hook_spec.rb index fb3968777bf..9faa5e1567c 100644 --- a/spec/models/hooks/web_hook_spec.rb +++ b/spec/models/hooks/web_hook_spec.rb @@ -187,8 +187,8 @@ RSpec.describe WebHook do end end - describe '.executable' do - let(:not_executable) do + describe '.executable/.disabled' do + let!(:not_executable) do [ [0, Time.current], [0, 1.minute.from_now], @@ -202,7 +202,7 @@ RSpec.describe WebHook do end end - let(:executables) do + let!(:executables) do [ [0, nil], [0, 1.day.ago], @@ -217,6 +217,7 @@ RSpec.describe WebHook do it 'finds the correct set of project hooks' do expect(described_class.where(project_id: project.id).executable).to match_array executables + expect(described_class.where(project_id: project.id).disabled).to match_array not_executable end context 'when the feature flag is not enabled' do @@ -224,7 +225,7 @@ RSpec.describe WebHook do stub_feature_flags(web_hooks_disable_failed: false) end - it 'is the same as all' do + specify 'enabled is the same as all' do expect(described_class.where(project_id: project.id).executable).to match_array(executables + not_executable) end end @@ -635,4 +636,10 @@ RSpec.describe WebHook do end end end + + describe '#update_last_failure' do + it 'is a method of this class' do + expect { described_class.new.update_last_failure }.not_to raise_error + end + end end |