Commit efc6600b authored by Robert Hunt's avatar Robert Hunt

Created new unit tests for views and moved shared feature logic

- Deleted logic tests and added new shows-up tests to shared features
- Created new unit test to test expiry_date partial's logic
- Add PAT view unit tests
- Add SSH key view unit tests
parent b9da26c8
......@@ -2,46 +2,6 @@
require 'spec_helper'
RSpec.shared_examples 'credentials inventory expiry date' do
it 'shows the expiry date' do
visit credentials_path
expect(first_row.text).to include(expiry_date)
end
end
RSpec.shared_examples 'credentials inventory expiry date before' do
before do
travel_to(view_at_date)
end
after do
travel_back
end
it 'shows the expiry without any warnings' do
visit credentials_path
expect(first_row).not_to have_selector('[data-testid="expiry-date-icon"]')
end
end
RSpec.shared_examples 'credentials inventory expiry date close or past' do
before do
travel_to(view_at_date)
end
after do
travel_back
end
it 'adds a warning to the expiry date' do
visit credentials_path
expect(first_row.find('[data-testid="expiry-date-icon"]').find('svg').native.inner_html).to match(/<use xlink:href=".+?icons-.+?##{expected_icon}">/)
end
end
RSpec.shared_examples_for 'credentials inventory personal access tokens' do |group_managed_account: false|
let_it_be(:user) { group_managed_account ? managed_user : create(:user, name: 'David') }
......@@ -58,7 +18,7 @@ RSpec.shared_examples_for 'credentials inventory personal access tokens' do |gro
visit credentials_path
end
it 'shows the details with no revoked date', :aggregate_failures do
it 'shows the details', :aggregate_failures do
expect(first_row.text).to include('David')
expect(first_row.text).to include('api')
expect(first_row.text).to include('2019-12-10')
......@@ -78,27 +38,16 @@ RSpec.shared_examples_for 'credentials inventory personal access tokens' do |gro
expires_at: expiry_date)
end
context 'and is not expired' do
let(:view_at_date) { 20.days.ago }
it_behaves_like 'credentials inventory expiry date'
it_behaves_like 'credentials inventory expiry date before'
before do
visit credentials_path
end
context 'and is near expiry' do
let(:expected_icon) { 'warning' }
let(:view_at_date) { 1.day.ago }
it_behaves_like 'credentials inventory expiry date'
it_behaves_like 'credentials inventory expiry date close or past'
it 'shows the details with an expiry date' do
expect(first_row.text).to include(expiry_date)
end
context 'and is expired' do
let(:expected_icon) { 'error' }
let(:view_at_date) { 2.days.since }
it_behaves_like 'credentials inventory expiry date'
it_behaves_like 'credentials inventory expiry date close or past'
it 'has an expiry icon' do
expect(first_row).to have_selector('[data-testid="expiry-date-icon"]')
end
end
......@@ -117,9 +66,6 @@ RSpec.shared_examples_for 'credentials inventory personal access tokens' do |gro
end
it 'shows the details with a revoked date', :aggregate_failures do
expect(first_row.text).to include('David')
expect(first_row.text).to include('api')
expect(first_row.text).to include('2019-12-10')
expect(first_row.text).to include('2020-06-22')
end
end
......@@ -141,7 +87,7 @@ RSpec.shared_examples_for 'credentials inventory SSH keys' do |group_managed_acc
visit credentials_path
end
it 'shows the details with no expiry', :aggregate_failures do
it 'shows the details', :aggregate_failures do
expect(first_row.text).to include('David')
expect(first_row.text).to include('2019-12-09')
expect(first_row.text).to include('2019-12-10')
......@@ -160,27 +106,16 @@ RSpec.shared_examples_for 'credentials inventory SSH keys' do |group_managed_acc
expires_at: expiry_date)
end
context 'and is not expired' do
let(:view_at_date) { 20.days.ago }
it_behaves_like 'credentials inventory expiry date'
it_behaves_like 'credentials inventory expiry date before'
before do
visit credentials_path
end
context 'and is near expiry' do
let(:expected_icon) { 'warning' }
let(:view_at_date) { 1.day.ago }
it_behaves_like 'credentials inventory expiry date'
it_behaves_like 'credentials inventory expiry date close or past'
it 'shows the details with an expiry date' do
expect(first_row.text).to include(expiry_date)
end
context 'and is expired' do
let(:expected_icon) { 'error' }
let(:view_at_date) { 2.days.since }
it_behaves_like 'credentials inventory expiry date'
it_behaves_like 'credentials inventory expiry date close or past'
it 'has an expiry icon' do
expect(first_row).to have_selector('[data-testid="expiry-date-icon"]')
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe('shared/credentials_inventory/_expiry_date.html.haml') do
let_it_be(:user) { create(:user) }
context 'when a non-expirable credential is used' do
let_it_be(:credential) { create(:deploy_key, user: user)}
before do
render 'shared/credentials_inventory/expiry_date', credential: credential
end
it 'shows "Never" if expires? method does not exist' do
expect(rendered).to have_text('Never')
end
end
context 'when an expirable credential is used' do
let_it_be(:credential) { create(:personal_access_token, user: user, expires_at: nil)}
before do
render 'shared/credentials_inventory/expiry_date', credential: credential
end
it 'shows "Never" when not expirable' do
expect(rendered).to have_text('Never')
end
context 'and is not expired' do
let_it_be(:expiry_date) { 20.days.since.to_date.to_s }
let_it_be(:credential) { create(:personal_key, user: user, expires_at: expiry_date)}
before do
render 'shared/credentials_inventory/expiry_date', credential: credential
end
it 'shows the correct date' do
expect(rendered).to have_text(expiry_date)
end
it 'does not have an expiry icon' do
expect(rendered).not_to have_selector('[data-testid="expiry-date-icon"]')
end
end
context 'and is near expiry' do
let_it_be(:expiry_date) { 1.day.since.to_date.to_s }
let_it_be(:credential) { create(:personal_access_token, user: user, expires_at: expiry_date)}
before do
render 'shared/credentials_inventory/expiry_date', credential: credential
end
it 'shows the correct date' do
expect(rendered).to have_text(expiry_date)
end
it 'has an icon' do
expect(rendered).to match(/<use xlink:href=".+?icons-.+?#warning">/)
end
end
context 'and has expired' do
let_it_be(:expiry_date) { 2.days.ago.to_date.to_s }
let_it_be(:credential) { create(:personal_access_token, user: user, expires_at: expiry_date)}
before do
render 'shared/credentials_inventory/expiry_date', credential: credential
end
it 'shows the correct date' do
expect(rendered).to have_text(expiry_date)
end
it 'has an icon' do
expect(rendered).to match(/<use xlink:href=".+?icons-.+?#error">/)
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe('shared/credentials_inventory/personal_access_tokens/_personal_access_token.html.haml') do
let(:user) { create(:user) }
let(:expiry_date) { 20.days.since }
let(:personal_access_token) { create(:personal_access_token, user: user, expires_at: expiry_date)}
before do
allow(view).to receive(:user_detail_path).and_return('abcd')
render 'shared/credentials_inventory/personal_access_tokens/personal_access_token', personal_access_token: personal_access_token
end
it 'shows the users name' do
expect(rendered).to have_text(user.name)
end
it 'shows the created on date' do
expect(rendered).to have_text(personal_access_token.created_at.to_date.to_s)
end
it 'shows the expiry date' do
expect(rendered).to have_text(personal_access_token.expires_at.to_date.to_s)
end
context 'revoked date' do
let(:updated_at_date) { 10.days.ago }
context 'when set' do
let(:personal_access_token) { create(:personal_access_token, user: user, updated_at: updated_at_date, revoked: true)}
before do
render 'shared/credentials_inventory/personal_access_tokens/personal_access_token', personal_access_token: personal_access_token
end
it 'shows the last accessed on date' do
expect(rendered).to have_text(personal_access_token.updated_at.to_date.to_s)
end
end
context 'when not set' do
let(:personal_access_token) { create(:personal_access_token, user: user, updated_at: updated_at_date)}
before do
render 'shared/credentials_inventory/personal_access_tokens/personal_access_token', personal_access_token: personal_access_token
end
it 'shows "Never" for the last accessed on date' do
expect(rendered).not_to have_text(updated_at_date.to_date.to_s)
end
end
end
context 'scopes' do
context 'when set' do
let(:scopes) { %w(api read_user read_api) }
let(:personal_access_token) { create(:personal_access_token, user: user, scopes: scopes)}
before do
render 'shared/credentials_inventory/personal_access_tokens/personal_access_token', personal_access_token: personal_access_token
end
it 'shows the scopes' do
expect(rendered).to have_text(personal_access_token.scopes.join(', '))
end
end
context 'when not set' do
let(:personal_access_token) { create(:personal_access_token, user: user)}
before do
# Turns out on creation of a PersonalAccessToken we set some default scopes and you can't pass `nil`
# This is forcing the scope to be `nil` even though it looks impossible to do, we have the logic in the view
personal_access_token.scopes = nil
render 'shared/credentials_inventory/personal_access_tokens/personal_access_token', personal_access_token: personal_access_token
end
it 'shows "No Scopes"' do
expect(rendered).to have_text('Scope No Scopes')
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe('shared/credentials_inventory/ssh_keys/_ssh_key.html.haml') do
let(:user) { create(:user) }
let(:expiry_date) { 20.days.since }
let(:ssh_key) { create(:personal_key, user: user, expires_at: expiry_date)}
before do
allow(view).to receive(:user_detail_path).and_return('abcd')
render 'shared/credentials_inventory/ssh_keys/ssh_key', ssh_key: ssh_key
end
it 'shows the users name' do
expect(rendered).to have_text(user.name)
end
it 'shows the created on date' do
expect(rendered).to have_text(ssh_key.created_at.to_date.to_s)
end
it 'shows the expiry date' do
expect(rendered).to have_text(ssh_key.expires_at.to_date.to_s)
end
context 'last accessed date' do
context 'when set' do
let(:last_used_date) { 10.days.ago }
let(:ssh_key) { create(:personal_key, user: user, last_used_at: last_used_date)}
before do
render 'shared/credentials_inventory/ssh_keys/ssh_key', ssh_key: ssh_key
end
it 'shows the last accessed on date' do
expect(rendered).to have_text(ssh_key.last_used_at.to_date.to_s)
end
end
context 'when not set' do
let(:ssh_key) { create(:personal_key, user: user)}
before do
render 'shared/credentials_inventory/ssh_keys/ssh_key', ssh_key: ssh_key
end
it 'shows "Never" for the last accessed on date' do
expect(rendered).to have_text('Last Accessed On Never')
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment