1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/test/unit/app/controllers/provider-approval-test.js
Mark Stacey 8dfb0e8154
Add hostname and extensionId to site metadata (#7218)
If the extension ID is set, an alternate title and subtitle are used
for the Connect Request screen. The title is always `External
Extension`, and the subtitle is `Extension ID: [id]` instead of the
origin (which would just be `[extension-scheme]://[id]` anyway).

The hostname for the site is used as a fallback in case it has no
title.

The artificial hostname set for internal connections has been renamed
from 'MetaMask' to 'metamask' because URL objects automatically
normalize hostnames to be all lower-case, and it was more convenient to
use a URL object so that the parameter would be the same type as used
for an untrusted connection.
2019-10-29 13:14:41 -03:00

331 lines
10 KiB
JavaScript

const assert = require('assert')
const sinon = require('sinon')
const ProviderApprovalController = require('../../../../app/scripts/controllers/provider-approval')
const mockLockedKeyringController = {
memStore: {
getState: () => ({
isUnlocked: false,
}),
},
}
const mockUnlockedKeyringController = {
memStore: {
getState: () => ({
isUnlocked: true,
}),
},
}
describe('ProviderApprovalController', () => {
describe('#_handleProviderRequest', () => {
it('should add a pending provider request when unlocked', () => {
const controller = new ProviderApprovalController({
keyringController: mockUnlockedKeyringController,
})
const metadata = {
hostname: 'https://example.com',
origin: 'example.com',
siteTitle: 'Example',
siteImage: 'https://example.com/logo.svg',
}
controller._handleProviderRequest(metadata)
assert.deepEqual(controller._getMergedState(), {
approvedOrigins: {},
providerRequests: [metadata],
})
})
it('should add a pending provider request when locked', () => {
const controller = new ProviderApprovalController({
keyringController: mockLockedKeyringController,
})
const metadata = {
hostname: 'https://example.com',
origin: 'example.com',
siteTitle: 'Example',
siteImage: 'https://example.com/logo.svg',
}
controller._handleProviderRequest(metadata)
assert.deepEqual(controller._getMergedState(), {
approvedOrigins: {},
providerRequests: [metadata],
})
})
it('should add a 2nd pending provider request when unlocked', () => {
const controller = new ProviderApprovalController({
keyringController: mockUnlockedKeyringController,
})
const metadata = [{
hostname: 'https://example1.com',
origin: 'example1.com',
siteTitle: 'Example 1',
siteImage: 'https://example1.com/logo.svg',
}, {
hostname: 'https://example2.com',
origin: 'example2.com',
siteTitle: 'Example 2',
siteImage: 'https://example2.com/logo.svg',
}]
controller._handleProviderRequest(metadata[0])
controller._handleProviderRequest(metadata[1])
assert.deepEqual(controller._getMergedState(), {
approvedOrigins: {},
providerRequests: metadata,
})
})
it('should add a 2nd pending provider request when locked', () => {
const controller = new ProviderApprovalController({
keyringController: mockLockedKeyringController,
})
const metadata = [{
hostname: 'https://example1.com',
origin: 'example1.com',
siteTitle: 'Example 1',
siteImage: 'https://example1.com/logo.svg',
}, {
hostname: 'https://example2.com',
origin: 'example2.com',
siteTitle: 'Example 2',
siteImage: 'https://example2.com/logo.svg',
}]
controller._handleProviderRequest(metadata[0])
controller._handleProviderRequest(metadata[1])
assert.deepEqual(controller._getMergedState(), {
approvedOrigins: {},
providerRequests: metadata,
})
})
it('should call openPopup when unlocked and when given', () => {
const openPopup = sinon.spy()
const controller = new ProviderApprovalController({
openPopup,
keyringController: mockUnlockedKeyringController,
})
const metadata = {
hostname: 'https://example.com',
origin: 'example.com',
siteTitle: 'Example',
siteImage: 'https://example.com/logo.svg',
}
controller._handleProviderRequest(metadata)
assert.ok(openPopup.calledOnce)
})
it('should call openPopup when locked and when given', () => {
const openPopup = sinon.spy()
const controller = new ProviderApprovalController({
openPopup,
keyringController: mockLockedKeyringController,
})
const metadata = {
hostname: 'https://example.com',
origin: 'example.com',
siteTitle: 'Example',
siteImage: 'https://example.com/logo.svg',
}
controller._handleProviderRequest(metadata)
assert.ok(openPopup.calledOnce)
})
it('should NOT call openPopup when unlocked and when the domain has already been approved', () => {
const openPopup = sinon.spy()
const controller = new ProviderApprovalController({
openPopup,
keyringController: mockUnlockedKeyringController,
})
controller.store.updateState({
approvedOrigins: {
'example.com': {
siteTitle: 'Example',
siteImage: 'https://example.com/logo.svg',
},
},
})
const metadata = {
hostname: 'https://example.com',
origin: 'example.com',
siteTitle: 'Example',
siteImage: 'https://example.com/logo.svg',
}
controller._handleProviderRequest(metadata)
assert.ok(openPopup.notCalled)
})
})
describe('#approveProviderRequestByOrigin', () => {
it('should mark the origin as approved and remove the provider request', () => {
const controller = new ProviderApprovalController({
keyringController: mockUnlockedKeyringController,
})
const metadata = {
hostname: 'https://example.com',
origin: 'example.com',
siteTitle: 'Example',
siteImage: 'https://example.com/logo.svg',
}
controller._handleProviderRequest(metadata)
controller.approveProviderRequestByOrigin('example.com')
assert.deepEqual(controller._getMergedState(), {
providerRequests: [],
approvedOrigins: {
'example.com': {
hostname: 'https://example.com',
siteTitle: 'Example',
siteImage: 'https://example.com/logo.svg',
},
},
})
})
it('should mark the origin as approved and multiple requests for the same domain', () => {
const controller = new ProviderApprovalController({
keyringController: mockUnlockedKeyringController,
})
const metadata = {
hostname: 'https://example.com',
origin: 'example.com',
siteTitle: 'Example',
siteImage: 'https://example.com/logo.svg',
}
controller._handleProviderRequest(metadata)
controller._handleProviderRequest(metadata)
controller.approveProviderRequestByOrigin('example.com')
assert.deepEqual(controller._getMergedState(), {
providerRequests: [],
approvedOrigins: {
'example.com': {
hostname: 'https://example.com',
siteTitle: 'Example',
siteImage: 'https://example.com/logo.svg',
},
},
})
})
it('should mark the origin as approved without a provider request', () => {
const controller = new ProviderApprovalController({
keyringController: mockUnlockedKeyringController,
})
controller.approveProviderRequestByOrigin('example.com')
assert.deepEqual(controller._getMergedState(), {
providerRequests: [],
approvedOrigins: {
'example.com': {
hostname: null,
siteTitle: null,
siteImage: null,
},
},
})
})
})
describe('#rejectProviderRequestByOrigin', () => {
it('should remove the origin from approved', () => {
const controller = new ProviderApprovalController({
keyringController: mockUnlockedKeyringController,
})
const metadata = {
hostname: 'https://example.com',
origin: 'example.com',
siteTitle: 'Example',
siteImage: 'https://example.com/logo.svg',
}
controller._handleProviderRequest(metadata)
controller.approveProviderRequestByOrigin('example.com')
controller.rejectProviderRequestByOrigin('example.com')
assert.deepEqual(controller._getMergedState(), {
providerRequests: [],
approvedOrigins: {},
})
})
it('should reject the origin even without a pending request', () => {
const controller = new ProviderApprovalController({
keyringController: mockUnlockedKeyringController,
})
controller.rejectProviderRequestByOrigin('example.com')
assert.deepEqual(controller._getMergedState(), {
providerRequests: [],
approvedOrigins: {},
})
})
})
describe('#clearApprovedOrigins', () => {
it('should clear the approved origins', () => {
const controller = new ProviderApprovalController({
keyringController: mockUnlockedKeyringController,
})
const metadata = {
hostname: 'https://example.com',
origin: 'example.com',
siteTitle: 'Example',
siteImage: 'https://example.com/logo.svg',
}
controller._handleProviderRequest(metadata)
controller.approveProviderRequestByOrigin('example.com')
controller.clearApprovedOrigins()
assert.deepEqual(controller._getMergedState(), {
providerRequests: [],
approvedOrigins: {},
})
})
})
describe('#shouldExposeAccounts', () => {
it('should return true for an approved origin', () => {
const controller = new ProviderApprovalController({
keyringController: mockUnlockedKeyringController,
})
const metadata = {
hostname: 'https://example.com',
origin: 'example.com',
siteTitle: 'Example',
siteImage: 'https://example.com/logo.svg',
}
controller._handleProviderRequest(metadata)
controller.approveProviderRequestByOrigin('example.com')
assert.ok(controller.shouldExposeAccounts('example.com'))
})
it('should return false for an origin not yet approved', () => {
const controller = new ProviderApprovalController({
keyringController: mockUnlockedKeyringController,
})
const metadata = {
hostname: 'https://example.com',
origin: 'example.com',
siteTitle: 'Example',
siteImage: 'https://example.com/logo.svg',
}
controller._handleProviderRequest(metadata)
controller.approveProviderRequestByOrigin('example.com')
assert.ok(!controller.shouldExposeAccounts('bad.website'))
})
})
})