Skip to content

Commit

Permalink
MMT-3936: Made env change on constant
Browse files Browse the repository at this point in the history
  • Loading branch information
mandyparson committed Dec 3, 2024
1 parent f7cbed3 commit a8f3321
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 35 deletions.
6 changes: 2 additions & 4 deletions serverless/src/samlCallback/__tests__/handler.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions serverless/src/samlRefreshToken/__tests__/handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ vi.spyOn(getConfig, 'getApplicationConfig').mockImplementation(() => ({
apiHost: 'https://mmt.localtest.earthdata.nasa.gov/dev',
graphQlHost: 'http://localhost:3013/dev/api',
cmrHost: 'http://localhost:4000',
version: 'sit',
env: 'development'
version: 'sit'
}))

vi.mock('jsonwebtoken', async () => ({
Expand Down
7 changes: 0 additions & 7 deletions serverless/src/utils/__tests__/createCookie.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import createCookie from '../createCookie'

vi.mock('../../../../../../sharedUtils/getConfig', async () => ({
...await vi.importActual('../../../../../../sharedUtils/getConfig'),
getApplicationConfig: vi.fn(() => ({
env: 'development'
}))
}))

describe('createCookie', () => {
const OLD_ENV = process.env

Expand Down
6 changes: 2 additions & 4 deletions serverless/src/utils/createCookie.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { getApplicationConfig } from '../../../sharedUtils/getConfig'
import MMT_COOKIE from '../../../static/src/js/constants/mmtCookie'

/**
* Returns the cookie string with the provided JWT
* @param {String} jwt JWT to use for the cookie value
*/
const createCookie = (jwt) => {
const { env } = getApplicationConfig()

const {
COOKIE_DOMAIN,
IS_OFFLINE,
JWT_VALID_TIME
} = process.env

let cookie = `_mmt_jwt_${env}=${jwt}; SameSite=Strict; Path=/; Domain=${COOKIE_DOMAIN}; Max-Age=${JWT_VALID_TIME};`
let cookie = `${MMT_COOKIE}=${jwt}; SameSite=Strict; Path=/; Domain=${COOKIE_DOMAIN}; Max-Age=${JWT_VALID_TIME};`
if (!IS_OFFLINE) {
cookie += ' Secure;'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ describe('TemplateForm', () => {
},
nativeId: 'MMT_mock-uuid',
providerId: 'MMT_2',
ummVersion: '1.18.2'
ummVersion: `${ummCVersion}`
}
},
error: new Error('An error occurred')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ import deleteTemplate from '@/js/utils/deleteTemplate'
import NotificationsContext from '@/js/context/NotificationsContext'
import MMT_COOKIE from '@/js/constants/mmtCookie'

import { getApplicationConfig } from '../../../../../../sharedUtils/getConfig'

import TemplateList from '../TemplateList'

const { env } = getApplicationConfig()

vi.mock('@/js/utils/deleteTemplate')
vi.mock('@/js/utils/errorLogger')
vi.mock('@/js/utils/getTemplates')
Expand All @@ -25,7 +21,7 @@ vi.mock('react-cookie', async () => ({
...await vi.importActual('react-cookie'),
useCookies: vi.fn().mockImplementation(() => ([
{
[`${MMT_COOKIE}_${env}`]: 'mock-jwt'
[MMT_COOKIE]: 'mock-jwt'
},
vi.fn(),
vi.fn()
Expand Down
6 changes: 5 additions & 1 deletion static/src/js/constants/mmtCookie.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { getApplicationConfig } from '../../../../sharedUtils/getConfig'

/**
* This is the name of the cookie that MMT uses.
*/
const MMT_COOKIE = '_mmt_jwt'
const { env } = getApplicationConfig()

const MMT_COOKIE = `_mmt_jwt_${env}`

export default MMT_COOKIE
6 changes: 1 addition & 5 deletions static/src/js/hooks/useMMTCookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@ import { useCookies } from 'react-cookie'

import MMT_COOKIE from '../constants/mmtCookie'

import { getApplicationConfig } from '../../../../sharedUtils/getConfig'

/**
* Returns the cookie value for the MMT auth cookie
*/
const useMMTCookie = () => {
const { env } = getApplicationConfig()

const [
cookies,
setCookie,
removeCookie
] = useCookies([MMT_COOKIE])

const { [`${MMT_COOKIE}_${env}`]: mmtJwt } = cookies
const { [MMT_COOKIE]: mmtJwt } = cookies

return {
mmtJwt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ vi.mock('../../../../../../sharedUtils/getConfig', async () => ({
getApplicationConfig: vi.fn(() => ({
apiHost: 'http://test.com/dev',
cookieDomain: 'example.com',
tokenValidTime: '900',
env: 'development'
tokenValidTime: '900'
}))
}))

Expand Down Expand Up @@ -124,7 +123,7 @@ describe('AuthContextProvider component', () => {
test('shows the name', async () => {
useCookies.mockImplementation(() => ([
{
[`${MMT_COOKIE}_development`]: 'mock-jwt'
[MMT_COOKIE]: 'mock-jwt'
},
vi.fn(),
vi.fn()
Expand Down Expand Up @@ -153,7 +152,7 @@ describe('AuthContextProvider component', () => {
const setCookie = vi.fn()
useCookies.mockImplementation(() => ([
{
[`${MMT_COOKIE}_development`]: 'mock-jwt'
[MMT_COOKIE]: 'mock-jwt'
},
setCookie,
vi.fn()
Expand All @@ -178,7 +177,7 @@ describe('AuthContextProvider component', () => {
const setCookie = vi.fn()
useCookies.mockImplementation(() => ([
{
[`${MMT_COOKIE}_development`]: 'mock-jwt'
[MMT_COOKIE]: 'mock-jwt'
},
setCookie,
vi.fn()
Expand Down Expand Up @@ -211,7 +210,7 @@ describe('AuthContextProvider component', () => {
jwt.decode.mockImplementation(() => { throw new Error('Error decoding jwt') })
useCookies.mockImplementation(() => ([
{
[`${MMT_COOKIE}_development`]: 'mock-jwt'
[MMT_COOKIE]: 'mock-jwt'
},
vi.fn(),
vi.fn()
Expand Down

0 comments on commit a8f3321

Please sign in to comment.