forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add QR code login for mobile to the profile tray
Closes USERS-433 Refs USERS-438 Refs UXS-30 flag=mobile_qr_login Adds an entry to the left nav "Profile" tray that allows a user to pull up a modal that displays QR code to scan on their phone to log into the mobile app. Test Plan: - Ensure your version of canvas has an up to date instructure_misc_plugin - create a developer key - add https://sso.canvaslms.com/canvas/login as its only redirect URI - In a rails console - a = Account.default - a.settings[:ios_mobile_sso_developer_key_id] = <dev key global id> - a.save! - a.account_domains.create!(name: 'sso.canvaslms.com') - In canvas, ensure the QR for Mobile Login release flag is on - Open the profile tray and click "QR for Mobile Login" - Ensure the modal dialog shows the QR code - Ensure the modal dialog can then be dismissed Change-Id: I92b3c4869530207b9274e9e207828be8de111672 Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/230318 Reviewed-by: Spencer Olson <[email protected]> Reviewed-by: Cody Cutrer <[email protected]> QA-Review: Pat Renner <[email protected]> Tested-by: Service Cloud Jenkins <[email protected]> Product-Review: Kevin Dougherty <[email protected]>
- Loading branch information
Showing
8 changed files
with
271 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright (C) 2020 - present Instructure, Inc. | ||
* | ||
* This file is part of Canvas. | ||
* | ||
* Canvas is free software: you can redistribute it and/or modify it under | ||
* the terms of the GNU Affero General Public License as published by the Free | ||
* Software Foundation, version 3 of the License. | ||
* | ||
* Canvas is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
* A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | ||
* details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License along | ||
* with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import I18n from 'i18n!QRLoginModal' | ||
import React, {useCallback, useState} from 'react' | ||
import ReactDOM from 'react-dom' | ||
import useFetchApi from 'jsx/shared/effects/useFetchApi' | ||
import {showFlashAlert} from 'jsx/shared/FlashAlert' | ||
|
||
import Modal from '../../shared/components/InstuiModal' | ||
import {Img} from '@instructure/ui-img' | ||
import {Button} from '@instructure/ui-buttons' | ||
import {View} from '@instructure/ui-view' | ||
import {Spinner} from '@instructure/ui-spinner' | ||
import {func} from 'prop-types' | ||
|
||
let modalContainer | ||
|
||
// exported for tests only | ||
export function killQRLoginModal() { | ||
if (modalContainer) ReactDOM.unmountComponentAtNode(modalContainer) | ||
modalContainer.remove() | ||
modalContainer = undefined | ||
} | ||
|
||
// exported for tests only | ||
export function QRLoginModal({onDismiss}) { | ||
const [image, setImage] = useState(null) | ||
|
||
function fetchError(e) { | ||
showFlashAlert({ | ||
message: I18n.t('An error occurred while retrieving your QR Code'), | ||
err: e | ||
}) | ||
killQRLoginModal() | ||
} | ||
|
||
useFetchApi({ | ||
path: 'canvas/login.png', | ||
fetchOpts: {method: 'POST'}, | ||
success: useCallback(r => setImage(r), []), | ||
error: useCallback(fetchError, []) | ||
}) | ||
|
||
function renderQRCode() { | ||
const body = image ? ( | ||
<Img data-testid="qr-code-image" src={`data:image/png;base64, ${image.png}`} /> | ||
) : ( | ||
<Spinner | ||
data-testid="qr-code-spinner" | ||
renderTitle={I18n.t('Waiting for your QR Code to load')} | ||
/> | ||
) | ||
return ( | ||
<View display="block" textAlign="center" padding="small 0 0"> | ||
{body} | ||
</View> | ||
) | ||
} | ||
|
||
return ( | ||
<Modal onDismiss={onDismiss} open label={I18n.t('QR for Mobile Login')} size="small"> | ||
<Modal.Body> | ||
<View display="block"> | ||
{I18n.t( | ||
"Scan this QR code from any Canvas mobile app to access your Canvas account when you're on the go." | ||
)} | ||
</View> | ||
{renderQRCode()} | ||
</Modal.Body> | ||
<Modal.Footer> | ||
<Button data-testid="qr-close-button" variant="primary" onClick={onDismiss}> | ||
{I18n.t('Done')} | ||
</Button> | ||
</Modal.Footer> | ||
</Modal> | ||
) | ||
} | ||
|
||
QRLoginModal.propTypes = { | ||
onDismiss: func.isRequired | ||
} | ||
|
||
export function showQRLoginModal(props = {}) { | ||
if (modalContainer) return // Modal is already up | ||
const {QRModal, ...modalProps} = props | ||
modalContainer = document.createElement('div') | ||
modalContainer.setAttribute('id', 'qr_login_modal_container') | ||
document.body.appendChild(modalContainer) | ||
|
||
const Component = QRModal || QRLoginModal | ||
ReactDOM.render(<Component onDismiss={killQRLoginModal} {...modalProps} />, modalContainer) | ||
} |
92 changes: 92 additions & 0 deletions
92
app/jsx/navigation_header/trays/__tests__/QRLoginModal.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* Copyright (C) 2020 - present Instructure, Inc. | ||
* | ||
* This file is part of Canvas. | ||
* | ||
* Canvas is free software: you can redistribute it and/or modify it under | ||
* the terms of the GNU Affero General Public License as published by the Free | ||
* Software Foundation, version 3 of the License. | ||
* | ||
* Canvas is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
* A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | ||
* details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License along | ||
* with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import React from 'react' | ||
import fetchMock from 'fetch-mock' | ||
import {showQRLoginModal, QRLoginModal, killQRLoginModal} from '../QRLoginModal' | ||
import {render, fireEvent} from '@testing-library/react' | ||
import {getByText as domGetByText} from '@testing-library/dom' | ||
|
||
const loginImageJson = { | ||
png: 'R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==' | ||
} | ||
|
||
const QRModalStub = () => <span>hi there</span> | ||
|
||
describe('Navigation Header > Trays', () => { | ||
describe('showQRLoginModal function', () => { | ||
it('renders the modal component inside the div', () => { | ||
showQRLoginModal({QRModal: QRModalStub}) | ||
const container = document.querySelector('div#qr_login_modal_container') | ||
expect(domGetByText(container, 'hi there')).toBeInTheDocument() | ||
}) | ||
|
||
it('removes the div when the modal is closed', () => { | ||
showQRLoginModal({QRModal: QRModalStub}) | ||
killQRLoginModal() | ||
const container = document.querySelector('div#qr_login_modal_container') | ||
expect(container).toBeNull() | ||
}) | ||
|
||
it('does not render multiple divs if called more than once', () => { | ||
showQRLoginModal({QRModal: QRModalStub}) | ||
showQRLoginModal({QRModal: QRModalStub}) | ||
showQRLoginModal({QRModal: QRModalStub}) | ||
const containers = document.querySelectorAll('div#qr_login_modal_container') | ||
expect(containers.length).toBe(1) | ||
}) | ||
}) | ||
|
||
describe('QRLoginModal component', () => { | ||
const handleDismiss = jest.fn() | ||
|
||
beforeEach(handleDismiss.mockClear) | ||
|
||
afterEach(fetchMock.restore) | ||
|
||
it('renders the dialog', () => { | ||
const {getByText} = render(<QRLoginModal onDismiss={handleDismiss} />) | ||
expect(getByText(/Scan this QR code/)).toBeInTheDocument() | ||
}) | ||
|
||
it('renders a spinner before the API call has completed', () => { | ||
const {getByTestId} = render(<QRLoginModal onDismiss={handleDismiss} />) | ||
expect(getByTestId('qr-code-spinner')).toBeInTheDocument() | ||
}) | ||
|
||
it('renders the image returned by the API call', async () => { | ||
fetchMock.post('/canvas/login.png', loginImageJson) | ||
const {findByTestId} = render(<QRLoginModal onDismiss={handleDismiss} />) | ||
const image = await findByTestId('qr-code-image') | ||
expect(image.src).toBe(`data:image/png;base64, ${loginImageJson.png}`) | ||
}) | ||
|
||
it('kills the modal off when "Done" button is clicked', () => { | ||
const {getByTestId} = render(<QRLoginModal onDismiss={handleDismiss} />) | ||
const closeButton = getByTestId('qr-close-button') | ||
fireEvent.click(closeButton) | ||
expect(handleDismiss).toHaveBeenCalled() | ||
}) | ||
|
||
it('kills the modal off when the modal dismiss X is clicked', () => { | ||
const {getByTestId} = render(<QRLoginModal onDismiss={handleDismiss} />) | ||
const closeButton = getByTestId('instui-modal-close').querySelector('button') | ||
fireEvent.click(closeButton) | ||
expect(handleDismiss).toHaveBeenCalled() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mobile_qr_login: | ||
state: hidden | ||
applies_to: RootAccount | ||
display_name: "QR for Mobile Login" | ||
description: "Include a link to show a QR code to easy mobile login on the profile page." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters