Skip to content

Commit

Permalink
Merge pull request #3 from Dainii/improve-tests
Browse files Browse the repository at this point in the history
Improve test suite
  • Loading branch information
Dainii authored Mar 5, 2024
2 parents cd20f77 + c24cab9 commit 11a8f7c
Show file tree
Hide file tree
Showing 22 changed files with 724 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ Rails/FilePath:
RSpec/ImplicitSubject:
EnforcedStyle: single_statement_only

RSpec/NestedGroups:
Max: 4

RSpec/ContextWording:
Prefixes:
- when
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/ptr_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class PtrController < AuthenticatedController
# Get /dns/1
# Get /ptr/1
def show
@ip_addres = params[:id]
@ptr = Ptr.resolve(params[:id])
Expand Down
2 changes: 2 additions & 0 deletions app/jobs/base_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ class BaseJob < ApplicationJob
def name
'Undefined'
end

def perform; end
end
32 changes: 31 additions & 1 deletion spec/controllers/domains_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

RSpec.describe DomainsController do
context 'with an unauthenticated user' do
describe 'GET /' do
describe 'GET /domains' do
before do
get :index
end
Expand All @@ -21,4 +21,34 @@
it { is_expected.to use_before_action(:set_domain) }
end
end

context 'with an authenticated user' do
let(:account) { Account.create!(email: 'user@example.com', password: 'secret123', status: 2) }

before do
sign_in(account)
end

def sign_in(account)
@controller.rodauth.account_from_login(account.email) # rubocop:disable RSpec/InstanceVariable
@controller.rodauth.login_session('secret123') # rubocop:disable RSpec/InstanceVariable
end

describe 'GET /' do
before do
get :index
end

it { is_expected.to respond_with(200) }
end

describe 'GET /domains/:id' do
before do
get :show, params: { id: Domain.create!(name: 'controller.domain').id }
end

it { is_expected.to respond_with(200) }
it { is_expected.to use_before_action(:set_domain) }
end
end
end
68 changes: 68 additions & 0 deletions spec/controllers/feedback_uploads_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe FeedbackUploadsController do
context 'with an unauthenticated user' do
describe 'GET /feedback_uploads/new' do
before do
get :new
end

it { is_expected.to respond_with(302) }
end

describe 'POST /feedback_uploads' do
context 'with valid params' do
before do
post :create, params: {
archives: file_fixture_upload(
Rails.root.join('spec', 'examples', 'reports', 'report_google.com.xml'),
'text/xml'
)
}
end

it { is_expected.to respond_with(302) }
end
end
end

context 'with an authenticated user' do
let(:account) { Account.create!(email: 'user@example.com', password: 'secret123', status: 2) }

before do
sign_in(account)
end

def sign_in(account)
@controller.rodauth.account_from_login(account.email) # rubocop:disable RSpec/InstanceVariable
@controller.rodauth.login_session('secret123') # rubocop:disable RSpec/InstanceVariable
end

describe 'GET /feedback_uploads/new' do
before do
get :new
end

it { is_expected.to respond_with(200) }
end

describe 'POST /feedback_uploads' do
context 'with valid params' do
before do
post :create, params: {
feedback_upload: {
archives: file_fixture_upload(
Rails.root.join('spec', 'examples', 'reports', 'report_google.com.xml'),
'text/xml'
)
}
}
end

it { is_expected.to respond_with(302) }
end
end
end
end
43 changes: 43 additions & 0 deletions spec/controllers/feedbacks_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe FeedbacksController do
context 'with an unauthenticated user' do
let(:domain) { Domain.create!(name: 'controller.domain') }
let(:feedback) { Feedback.create!(domain:) }

describe 'GET /domains/:domain_id/feedbacks/:id' do
before do
get :show, params: { domain_id: domain.id, id: feedback.id }
end

it { is_expected.to respond_with(302) }
it { is_expected.to use_before_action(:set_feedback) }
end
end

context 'with an authenticated user' do
let(:account) { Account.create!(email: 'user@example.com', password: 'secret123', status: 2) }
let(:domain) { Domain.create!(name: 'controller.domain') }
let(:feedback) { Feedback.create!(domain:) }

before do
sign_in(account)
end

def sign_in(account)
@controller.rodauth.account_from_login(account.email) # rubocop:disable RSpec/InstanceVariable
@controller.rodauth.login_session('secret123') # rubocop:disable RSpec/InstanceVariable
end

describe 'GET /domains/:domain_id/feedbacks/:id' do
before do
get :show, params: { domain_id: domain.id, id: feedback.id }
end

it { is_expected.to respond_with(200) }
it { is_expected.to use_before_action(:set_feedback) }
end
end
end
68 changes: 66 additions & 2 deletions spec/controllers/home_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,77 @@
require 'rails_helper'

RSpec.describe HomeController do
describe 'GET /' do
context 'with an unauthenticated user' do
context 'with an unauthenticated user' do
describe 'GET /' do
before do
get :index
end

it { is_expected.to respond_with(302) }
end
end

context 'with an authenticated user' do
let(:account) { Account.create!(email: 'user@example.com', password: 'secret123', status: 2) }

before do
sign_in(account)
end

def sign_in(account)
@controller.rodauth.account_from_login(account.email) # rubocop:disable RSpec/InstanceVariable
@controller.rodauth.login_session('secret123') # rubocop:disable RSpec/InstanceVariable
end

describe 'GET /' do
before do
get :index
end

it { is_expected.to respond_with(200) }
end
end

# TODO
# The below tests are design to test the `switch_locale` method defined in `ApplicationController`
# It should probably be elsewhere
context 'with render_views as authenticated user' do
let(:account) { Account.create!(email: 'user@example.com', password: 'secret123', status: 2) }

before do
sign_in(account)
end

def sign_in(account)
@controller.rodauth.account_from_login(account.email) # rubocop:disable RSpec/InstanceVariable
@controller.rodauth.login_session('secret123') # rubocop:disable RSpec/InstanceVariable
end

describe 'GET / with en-US locale' do
before do
request.headers['HTTP_ACCEPT_LANGUAGE'] = 'en-US,en;q=0.5'
get :index
end

it { is_expected.to use_around_action(:switch_locale) }
end

describe 'GET / with fr-FR locale' do
before do
request.headers['HTTP_ACCEPT_LANGUAGE'] = 'fr-FR,fr;q=0.5'
get :index
end

it { is_expected.to use_around_action(:switch_locale) }
end

describe 'GET / with unknown locale' do
before do
request.headers['HTTP_ACCEPT_LANGUAGE'] = 'as-XD,as;q=0.5'
get :index
end

it { is_expected.to use_around_action(:switch_locale) }
end
end
end
73 changes: 73 additions & 0 deletions spec/controllers/jobs_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe JobsController do
context 'with an unauthenticated user' do
let(:job) do
sq_job = SolidQueue::Job.create(queue_name: 'default', class_name: 'BaseJob')

Job.create!(
job_id: '123456789',
name: 'test job',
solid_queue_job: sq_job
)
end

describe 'GET /jobs' do
before do
get :index
end

it { is_expected.to respond_with(302) }
end

describe 'GET /jobs/:id' do
before do
get :show, params: { id: job.id }
end

it { is_expected.to respond_with(302) }
it { is_expected.to use_before_action(:set_job) }
end
end

context 'with an authenticated user' do
let(:job) do
sq_job = SolidQueue::Job.create(queue_name: 'default', class_name: 'BaseJob')

Job.create!(
job_id: '123456789',
name: 'test job',
solid_queue_job: sq_job
)
end
let(:account) { Account.create!(email: 'user@example.com', password: 'secret123', status: 2) }

before do
sign_in(account)
end

def sign_in(account)
@controller.rodauth.account_from_login(account.email) # rubocop:disable RSpec/InstanceVariable
@controller.rodauth.login_session('secret123') # rubocop:disable RSpec/InstanceVariable
end

describe 'GET /' do
before do
get :index
end

it { is_expected.to respond_with(200) }
end

describe 'GET /jobs/:id' do
before do
get :show, params: { id: job.id }
end

it { is_expected.to respond_with(200) }
it { is_expected.to use_before_action(:set_job) }
end
end
end
Loading

0 comments on commit 11a8f7c

Please sign in to comment.