Skip to content

Commit

Permalink
Fixing tests (nanego#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
B-Rass authored Sep 12, 2024
1 parent b201340 commit e76969e
Show file tree
Hide file tree
Showing 24 changed files with 163 additions and 125 deletions.
12 changes: 6 additions & 6 deletions app/controllers/external_app_records_controller.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
class ExternalAppRecordsController < ApplicationController
# frozen_string_literal: true

class ExternalAppRecordsController < ApplicationController
def index
@external_app_records = ExternalAppRecord.includes(:server => :frame).order('servers.name')
@external_app_records = ExternalAppRecord.includes(server: :frame).order("servers.name")
@filter = ProcessorFilter.new(@external_app_records, params)
@external_app_records = @filter.results
@servers_count = Server.no_pdus.count
end

def sync_all_servers_with_glpi
if ExternalAppRequest.where(status: ['pending', 'in_progress'], external_app_name: 'glpi').exists?
render json: { error: 'Another request is already in progress' }
if ExternalAppRequest.exists?(status: ["pending", "in_progress"], external_app_name: "glpi")
render json: { error: "Another request is already in progress" }
else
request = ExternalAppRequest.create!(status: :pending, user: current_user, external_app_name: 'glpi')
request = ExternalAppRequest.create!(status: :pending, user: current_user, external_app_name: "glpi")
SyncWithGlpiJob.perform_later

render json: { request_id: request.id, status: request.status, progress: request.progress }
end
end

end
4 changes: 2 additions & 2 deletions app/controllers/external_app_requests_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class ExternalAppRequestsController < ApplicationController
# frozen_string_literal: true

class ExternalAppRequestsController < ApplicationController
def show
@external_app_request = ExternalAppRequest.find(params[:id])
render json: { status: @external_app_request.status,
Expand All @@ -12,5 +13,4 @@ def destroy

redirect_to external_app_records_path
end

end
12 changes: 5 additions & 7 deletions app/jobs/sync_with_glpi_job.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
# frozen_string_literal: true

class SyncWithGlpiJob < ApplicationJob
queue_as :default

def perform(*args)

def perform
request = ExternalAppRequest.find_by(status: 'pending', external_app_name: 'glpi')
request.update(status: :in_progress)

begin

client = GlpiClient.new
servers = Server.no_pdus
servers_count = servers.count

Rails.logger.info "Synchronizing #{servers_count} servers with GLPI records:"
puts "Synchronizing #{servers_count} servers with GLPI records:"
Rails.logger.debug { "Synchronizing #{servers_count} servers with GLPI records:" }

servers.each_with_index do |server, index|
ExternalAppRecord.sync_server_with_glpi(server, client)

puts "Processed #{index + 1} servers so far" if (index + 1) % 25 == 0
Rails.logger.debug { "Processed #{index + 1} servers so far" } if ((index + 1) % 25).zero?
request.update(progress: (index + 1) * 100 / servers_count)
end

request.update(status: :completed, progress: 100)

rescue StandardError => e
request.update(status: :failed)
Rails.logger.error "ExternalAppRequest failed: #{e.message}"
end

end
end
16 changes: 8 additions & 8 deletions app/models/external_app_record.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class ExternalAppRecord < ApplicationRecord
# frozen_string_literal: true

class ExternalAppRecord < ApplicationRecord
belongs_to :server

before_create :set_external_app_name
Expand All @@ -23,18 +24,17 @@ def self.sync_server_with_glpi(server, client)

computer = client.computer(serial: server.numero, params: params)
record = ExternalAppRecord.find_or_create_by(server: server)
if computer.present?
updated = record.update({ external_name: computer.name, external_id: computer.id, external_serial: computer.serial })
else
updated = record.update({ external_name: '', external_id: '', external_serial: '' })
end
puts "Server ##{server.id} #{server.name} NOT updated" unless updated
updated = if computer.present?
record.update({ external_name: computer.name, external_id: computer.id, external_serial: computer.serial })
else
record.update({ external_name: '', external_id: '', external_serial: '' })
end
Rails.logger.debug { "Server ##{server.id} #{server.name} NOT updated" } unless updated
end

private

def set_external_app_name
self.app_name = 'glpi' # Only one external app supported for now
end

end
3 changes: 2 additions & 1 deletion app/models/external_app_request.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class ExternalAppRequest < ApplicationRecord
belongs_to :user

Expand All @@ -22,5 +24,4 @@ def set_defaults
self.status ||= :pending
self.progress ||= 0
end

end
2 changes: 1 addition & 1 deletion app/models/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def method_missing(symbol, *args)

return attributes[symbol] if attribute_names.include?(symbol)

if method_name.end_with?("=") && attribute_names.include?(method_name.chop.to_sym)
if method_name.end_with?("=") && attribute_names.include?(method_name.chop.to_sym) # rubocop:disable Style/IfUnlessModifier
return attributes[method_name.chop.to_sym] = args.first
end

Expand Down
1 change: 0 additions & 1 deletion app/services/glpi_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ def get_processor_designation_from_glpi(id:)
end

def get_computer_id_from_glpi(serial:)

serial = "AZERTY" unless Rails.env.production?

resp = @connection.get("Computer?searchText[serial]=#{serial}") do |request|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
<% if islets.size > 1 %>
<% islets.each do |islet| %>
<li class="nav-item" role="menuitem">
<%= link_to islet_path(islet, view: params[:view], bg: params[:bg]), class: "dropdown-item ps-4" do %>
<%= link_to room_path(room, islet: islet.name, view: params[:view], bg: params[:bg]),
class: "dropdown-item ps-4" do %>
<span class="text-primary"></span>
<span><%= "#{Islet.model_name.human} #{islet.name}" %></span>
<% end %>
Expand Down
2 changes: 2 additions & 0 deletions db/migrate/20240822135649_create_external_app_requests.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class CreateExternalAppRequests < ActiveRecord::Migration[7.1]
def change
create_table :external_app_requests do |t|
Expand Down
9 changes: 3 additions & 6 deletions lib/tasks/sync_glpi_data.rake
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
# frozen_string_literal: true

namespace :sync_glpi_data do

desc "Sync all servers with GLPI records"
task :sync_all_servers => :environment do

client = GlpiClient.new

servers = Server.no_pdus

puts "Synchronizing #{servers.count} servers with GLPI records:"

servers.each_with_index do |server, index|
puts "Processed #{index + 1} servers so far" if (index + 1) % 25 == 0
puts "Processed #{index + 1} servers so far" if ((index + 1) % 25).zero?
ExternalAppRecord.sync_server_with_glpi(server, client)
end

end

desc "Clear all ExternalAppRecord"
task :clear_all_external_records => :environment do
ExternalAppRecord.destroy_all
ExternalAppRecord.destroy_all
end

end
26 changes: 0 additions & 26 deletions spec/controllers/external_app_records_controller_spec.rb

This file was deleted.

21 changes: 0 additions & 21 deletions spec/controllers/external_app_requests_controller_spec.rb

This file was deleted.

31 changes: 31 additions & 0 deletions spec/jobs/sync_with_glpi_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe SyncWithGlpiJob do
let(:request) { ExternalAppRequest.create(user: User.first, external_app_name: "glpi") }

before { request }

it "queues the job" do
expect do
described_class.perform_later
end.to have_enqueued_job(described_class).on_queue("default")
end

it "executes perform" do
expect do
perform_enqueued_jobs { described_class.perform_later }
end.to change { request.reload.progress }.from(0).to(100)
end

describe "updates the request status and progress correctly" do
before do
perform_enqueued_jobs { described_class.perform_later }
request.reload
end

it { expect(request.status).to eq("completed") }
it { expect(request.progress).to eq(100) }
end
end
30 changes: 0 additions & 30 deletions spec/jobs/sync_with_glpi_jop_spec.rb

This file was deleted.

9 changes: 5 additions & 4 deletions spec/models/external_app_request_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
require 'rails_helper'
# frozen_string_literal: true

RSpec.describe ExternalAppRequest, type: :model do
require "rails_helper"

RSpec.describe ExternalAppRequest do
fixtures :users

let(:request) { ExternalAppRequest.new(user: User.first, external_app_name: 'glpi') }
let(:request) { described_class.new(user: User.first, external_app_name: "glpi") }

it "is valid with valid attributes" do
expect(request).to be_valid
end

it "is not valid without a user" do
request.user = nil
expect(request).to_not be_valid
expect(request).not_to be_valid
end

it "sets default status to pending" do
Expand Down
52 changes: 52 additions & 0 deletions spec/requests/external_app_records_request_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe "ExternalAppRecords" do
let(:ext_app_rec) { external_app_records(:one) }

before do
sign_in users(:one)

ext_app_rec
end

describe "GET #index" do
subject(:response) do
get external_app_records_path

@response # rubocop:disable RSpec/InstanceVariable
end

it { expect(response).to have_http_status(:success) }
it { expect(response).to render_template(:index) }
it { expect(response.body).to include(ext_app_rec.server.numero) }
end

describe "PUT #sync_all_servers_with_glpi" do
subject(:response) do
put sync_with_glpi_external_app_records_path

# NOTE: used to simplify usage and custom test done in final spec file.
@response # rubocop:disable RSpec/InstanceVariable
end

it "creates a new request and enqueues the job" do
expect do
response
end.to change(ExternalAppRequest, :count).by(1)
end

it "enqueues the job" do
expect do
response
end.to have_enqueued_job(SyncWithGlpiJob)
end

it do
expect(response.parsed_body).to include("request_id" => ExternalAppRequest.last.id,
"status" => "pending",
"progress" => 0)
end
end
end
Loading

0 comments on commit e76969e

Please sign in to comment.