Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions app/assets/javascripts/change_current_provider.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
$(document).ready ->

$('.display-modal').leanModal
top: 200
overlay: 0.6
Expand Down Expand Up @@ -53,11 +52,12 @@ $(document).ready ->
url: "/set_provider?provider_id=#{provider}"
method: 'post'
dataType: 'json'

success: (data, status, xhr) ->
# Click the link that the user needs
$(link)[0].click()

# change current provider from banner link
# Change current provider
$('#change-current-provider-banner-link').on 'click', (element) ->
provider = $(element.target).data('provider')
actionLink = $(element.target).data('actionLink')
Expand All @@ -66,5 +66,22 @@ $(document).ready ->
url: "/set_provider?provider_id=#{provider}"
method: 'post'
dataType: 'json'

success: (data, status, xhr) ->
$("##{actionLink}")[0].click()

$('#refresh-available-providers-link').on 'click', (element) ->
$.ajax
url: '/refresh_providers'
method: 'get'
dataType: 'json'

success: (response) ->
$('#select_provider').empty()
$('#select_provider').append($('<option>').text('Select Provider'))

$.each response.items, (index, value) ->
$('#select_provider').append($('<option>').val(value).text(value))

$('span.refresh-providers.spinner').remove()
$('a.refresh-providers.spinner').show()
27 changes: 27 additions & 0 deletions app/concerns/provider_context_redirector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# :nodoc:
module ProviderContextRedirector
extend ActiveSupport::Concern

routes = Rails.application.routes.url_helpers

# Add any redirects we want to customize here, e.g., if the "index" action
# for a given path is "/provider_orders", we will redirect to the '/orders'
# path.
ROUTE_EXCEPTIONS = {
'manage_cmr' => routes.manage_cmr_path,
'pages' => routes.manage_metadata_path,
'provider_orders' => routes.orders_path
}.freeze

def get_redirect_route(original_route)
# Get the controller name from the original path
controller_name = Rails.application.routes.recognize_path(original_route)[:controller]

# Return the user to the "index" action of the original controller they were on,
# unless it's in the exception list.
ROUTE_EXCEPTIONS.include?(controller_name) ? ROUTE_EXCEPTIONS[controller_name] : url_for(action: 'index', controller: controller_name)
rescue ActionController::UrlGenerationError
# If we missed any route exceptions fallback to manage metadata
Rails.application.routes.url_helpers.manage_metadata_path
end
end
9 changes: 7 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# :nodoc:
class UsersController < ApplicationController
include ProviderContextRedirector

skip_before_filter :is_logged_in, except: [:set_provider, :refresh_providers]
skip_before_filter :setup_query

Expand Down Expand Up @@ -26,7 +29,7 @@ def set_provider
set_provider_context_token

respond_to do |format|
format.html { redirect_to manage_metadata_path }
format.html { redirect_to get_redirect_route(request.referer) }
format.json { render json: nil, status: :ok }
end
end
Expand All @@ -35,6 +38,8 @@ def refresh_providers
current_user.update(provider_id: nil)
current_user.set_available_providers(token)

redirect_to manage_metadata_path
respond_to do |format|
format.json { render json: { items: current_user.available_providers } }
end
end
end
2 changes: 1 addition & 1 deletion app/views/shared/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<% end %>

<p>
<%= link_to 'Refresh your available providers', refresh_user_providers_path, class: 'refresh-providers spinner' %>
<%= link_to 'Refresh your available providers', 'javascript:void(0)', id: 'refresh-available-providers-link', class: 'refresh-providers spinner' %>
</p>
</div>
<% end %>
Expand Down
50 changes: 47 additions & 3 deletions spec/features/provider_context/provider_context_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# MMT-272
# MMT-272, MMT-717

require 'rails_helper'

describe 'Provider context', js: true do
context 'when the user has multiple possible provider contexts' do
describe 'Provider context', reset_provider: true, js: true do
let(:order_guid) { 'FF330AD3-1A89-871C-AC94-B689A5C95723' }

context 'when the user has multiple providers' do
before do
login(providers: nil)
end
Expand Down Expand Up @@ -86,6 +88,48 @@
expect(page).to have_content('MMT_2')
end
end

# This is the exception case (see redirector.rb)
context 'if user is on the provider order details page' do
before do
# The order guid belongs to NSIDC_ECS
User.first.update(provider_id: 'NSIDC_ECS')

VCR.use_cassette('echo_soap/order_processing_service/provider_orders/terminal_order', record: :none) do
visit provider_order_path(order_guid)
end

within '#user-info' do
click_on 'Change Provider'
end

select 'MMT_1', from: 'select_provider'
wait_for_ajax
end

it 'redirects to the orders index page when switching provider context' do
expect(page).to have_current_path(orders_path, only_path: true)
end
end

context 'if user is on the permissions creation page' do
before do
visit permissions_path

click_on 'Create a Permission'

within '#user-info' do
click_on 'Change Provider'
end

select 'MMT_1', from: 'select_provider'
wait_for_ajax
end

it 'redirects to the permissions index page when switching provider context' do
expect(page).to have_current_path(permissions_path, only_path: true)
end
end
end
end

Expand Down