Skip to content

Commit

Permalink
Add unit tests for authenticated content setServerToken (#432)
Browse files Browse the repository at this point in the history
* Fix for InvalidTokenError when auth cookie is null.

* Fix import and change future date to 1000 years

* Remove check for old cookie
  • Loading branch information
dylankelly authored Jul 30, 2019
1 parent 3579948 commit 23cec48
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ function serverSetToken (cookies, store) {
let isAuth = false
if (cookies) {
const parsed = cookieparser.parse(cookies)
// Check if authenticated content cookie is set
if (parsed[authCookieName]) {
if (!isTokenExpired(parsed[authCookieName])) {
serverToken = parsed[authCookieName]
Expand Down
4 changes: 4 additions & 0 deletions packages/ripple-nuxt-tide/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,9 @@
"peerDependencies": {
"@nuxtjs/axios": "^5.3.1",
"nuxt": "^2.4.3"
},
"devDependencies": {
"mockdate": "^2.0.3",
"vuex-mock-store": "^0.0.7"
}
}
41 changes: 41 additions & 0 deletions packages/ripple-nuxt-tide/test/unit/authenticate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { serverSetToken } from './../../modules/authenticated-content/lib/authenticate'
import { Store } from 'vuex-mock-store'

describe('setServerToken', () => {
const MockDate = require('mockdate')
const validToken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE1NjMxNzA5NTcsImV4cCI6MTU2MzE3MTU1NywiZHJ1cGFsIjp7InVpZCI6IjY0OTIifX0.vEJHPrci2KBBowyRvtZot5vHCE8xFcy77bWU814BLEmrk0Zs3bvGVAgJ_SlW57uwlCbyYeSMwe8-uKqHzsrrYBugW55qvua-UfdgkJfwxeH32PXZlStVgoHsPu_BHQMpqwOh9pcH-2Lueiz-BxbT1DrYduC4R6zHBTcO-VLiW0swDPVtZHgPFWruGYgyHN8T2YWsn0ujBkLNOxD6-7FAEeY5W0X-yEHLXESjA15YDcfRebSIsvtKrZrLu21PTW9E0ql0zkLFSCip46js-f2BOc8V7jnSQM5XNwKHwrU3gtIKDhKptBQmdDLQ8KCkFhKkoyepEMoBsK_y5TWhZNOPlw'

afterEach(() => {
MockDate.reset()
})

it('should set isAuth to true when valid JWT is passed in cookie', () => {
MockDate.set('2019-07-15T06:19:17+00:00')

const store = new Store({
state: {
tideAuthenticatedContent: {
isAuthenticated: false
}
}
})

serverSetToken(`authenticatedContent=${validToken};`, store)
expect(store.dispatch).toHaveBeenCalledWith('tideAuthenticatedContent/setAuthenticated', true)
})

it('should isAuth to false when an expired JWT is passed in cookie', () => {
MockDate.set('3022-07-15T06:19:17+00:00')

const store = new Store({
state: {
tideAuthenticatedContent: {
isAuthenticated: false
}
}
})

serverSetToken(`authenticatedContent=${validToken};`, store)
expect(store.dispatch).toHaveBeenCalledWith('tideAuthenticatedContent/setAuthenticated', false)
})
})
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10469,6 +10469,11 @@ [email protected], "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0, mkd
dependencies:
minimist "0.0.8"

mockdate@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/mockdate/-/mockdate-2.0.3.tgz#f4095e5eaea94c5f8b4e0e708f5d79fdff846a66"
integrity sha512-/wRyr3grWk3tyk188qjZpeiiAfkAoDPEGqyerretomeeaH0D+pN9MCcedhAwrkxX3a216gp8CwPeQMHfLvrFpA==

modify-values@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"
Expand Down Expand Up @@ -15430,6 +15435,13 @@ vue@^2.2.4, vue@^2.5.17, vue@^2.6.10:
version "2.6.10"
resolved "https://registry.yarnpkg.com/vue/-/vue-2.6.10.tgz#a72b1a42a4d82a721ea438d1b6bf55e66195c637"

vuex-mock-store@^0.0.7:
version "0.0.7"
resolved "https://registry.yarnpkg.com/vuex-mock-store/-/vuex-mock-store-0.0.7.tgz#b18c529bc48b23dc596fdf9e0739676ae16dd7d1"
integrity sha512-Hd8q/lab+U/b0o0YvGvUWbxtrFbRTxwTMp+VL8qPQ1l2wmhJOMBwb086yil5dn1i2qLiF8Y5UtgKqCMcLzShKQ==
dependencies:
lodash.clonedeep "^4.5.0"

vuex-persistedstate@^2.5.4:
version "2.5.4"
resolved "https://registry.yarnpkg.com/vuex-persistedstate/-/vuex-persistedstate-2.5.4.tgz#a19710ad7f9a08cea4e65fc585924d9fdac7384a"
Expand Down

0 comments on commit 23cec48

Please sign in to comment.