Add spec for CheckAppVersion service

This commit is contained in:
Eugene Burmakin 2024-04-25 22:40:51 +02:00
parent e00f614b9a
commit 98d33da3d1
6 changed files with 59 additions and 4 deletions

View file

@ -36,6 +36,7 @@ group :test do
gem 'shoulda-matchers' gem 'shoulda-matchers'
gem 'simplecov' gem 'simplecov'
gem 'super_diff' gem 'super_diff'
gem 'webmock'
end end
group :development do group :development do

View file

@ -91,6 +91,9 @@ GEM
concurrent-ruby (1.2.3) concurrent-ruby (1.2.3)
connection_pool (2.4.1) connection_pool (2.4.1)
content_disposition (1.0.0) content_disposition (1.0.0)
crack (1.0.0)
bigdecimal
rexml
crass (1.0.6) crass (1.0.6)
date (3.3.4) date (3.3.4)
debug (1.9.2) debug (1.9.2)
@ -127,6 +130,7 @@ GEM
geocoder (1.8.2) geocoder (1.8.2)
globalid (1.2.1) globalid (1.2.1)
activesupport (>= 6.1) activesupport (>= 6.1)
hashdiff (1.1.0)
i18n (1.14.4) i18n (1.14.4)
concurrent-ruby (~> 1.0) concurrent-ruby (~> 1.0)
importmap-rails (2.0.1) importmap-rails (2.0.1)
@ -346,6 +350,10 @@ GEM
unicode-display_width (2.5.0) unicode-display_width (2.5.0)
warden (1.2.9) warden (1.2.9)
rack (>= 2.0.9) rack (>= 2.0.9)
webmock (3.23.0)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
webrick (1.8.1) webrick (1.8.1)
websocket-driver (0.7.6) websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0) websocket-extensions (>= 0.1.0)
@ -391,6 +399,7 @@ DEPENDENCIES
tailwindcss-rails tailwindcss-rails
turbo-rails turbo-rails
tzinfo-data tzinfo-data
webmock
RUBY VERSION RUBY VERSION
ruby 3.2.3p157 ruby 3.2.3p157

View file

@ -9,7 +9,7 @@ class CheckAppVersion
def call def call
begin begin
latest_version = JSON.parse(Net::HTTP.get(URI.parse(@repo_url)))[0]['name'] latest_version = JSON.parse(Net::HTTP.get(URI.parse(@repo_url)))[0]['name']
rescue rescue StandardError
return false return false
end end

View file

@ -4,10 +4,8 @@ RSpec.describe ImportJob, type: :job do
describe '#perform' do describe '#perform' do
subject(:perform) { described_class.new.perform(user.id, import.id) } subject(:perform) { described_class.new.perform(user.id, import.id) }
let(:file_path) { 'spec/fixtures/files/owntracks/export.json' }
let(:file) { fixture_file_upload(file_path) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:import) { create(:import, user: user, file: file, name: File.basename(file.path)) } let(:import) { create(:import, user:, name: 'owntracks_export.json') }
it 'creates points' do it 'creates points' do
expect { perform }.to change { Point.count }.by(8) expect { perform }.to change { Point.count }.by(8)

View file

@ -0,0 +1,43 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe CheckAppVersion do
describe '#call' do
subject(:check_app_version) { described_class.new.call }
let(:app_version) { File.read('.app_version').strip }
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
end
context 'when latest version is newer' do
before { allow(File).to receive(:read).with('.app_version').and_return('0.9.0') }
it { is_expected.to be true }
end
context 'when latest version is the same' do
before { allow(File).to receive(:read).with('.app_version').and_return('1.0.0') }
it { is_expected.to be false }
end
context 'when latest version is older' do
before { allow(File).to receive(:read).with('.app_version').and_return('1.1.0') }
it { is_expected.to be true }
end
context 'when request fails' do
before do
allow(Net::HTTP).to receive(:get).and_raise(StandardError)
allow(File).to receive(:read).with('.app_version').and_return(app_version)
end
it { is_expected.to be false }
end
end
end

View file

@ -1,4 +1,8 @@
# frozen_string_literal: true
require 'simplecov' require 'simplecov'
require 'webmock/rspec'
SimpleCov.start SimpleCov.start
# This file was generated by the `rails generate rspec:install` command. Conventionally, all # This file was generated by the `rails generate rspec:install` command. Conventionally, all