Skip to content

Commit

Permalink
✅(backend) update jwt tokens within tests
Browse files Browse the repository at this point in the history
With upgrade of PyJWT 2.6.0 jwt token header has changed, so we have to update
our tests comparing jwt tokens.
  • Loading branch information
jbpenrath committed Dec 7, 2022
1 parent d8c406d commit 8f34ee6
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 30 deletions.
64 changes: 64 additions & 0 deletions src/frontend/magnify/vite.dev.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { resolve } from 'path';
import typescript from '@rollup/plugin-typescript';
import react from '@vitejs/plugin-react';
import postcss from 'rollup-plugin-postcss';
import { defineConfig } from 'vite';

// https://vitejs.dev/config/
export default defineConfig({
build: {
emptyOutDir: false,
lib: {
entry: resolve(__dirname, './src/index.ts'),
fileName: () => 'index.js',
formats: ['es'],
name: 'index',
},
minify: false,
rollupOptions: {
external: [
'@jitsi/react-sdk',
'axios',
'formik',
'grommet',
'grommet-icons',
'polished',
'react',
'react-intl',
'react-query',
'react-router-dom',
'styled-components',
'validator',
'yup',
],
plugins: [
postcss({
modules: true,
}),
],
treeshake: false,
},
sourcemap: true,
watch: {
exclude: [resolve(__dirname, './src/tests'), resolve(__dirname, './src/**/*.spec.tsx?')],
include: [resolve(__dirname, './src/**')],
},
},
plugins: [
react(),
typescript({
declaration: true,
declarationDir: resolve(__dirname, './dist'),
exclude: [
resolve(__dirname, './dist'),
resolve(__dirname, './node_modules/**'),
// Ignore all test stuff
resolve(__dirname, './src/tests'),
resolve(__dirname, './src/**/*.spec.tsx?'),
],
noEmitOnError: false,
rootDir: resolve(__dirname, './src'),
target: 'ESNext',
}),
],
});
57 changes: 27 additions & 30 deletions tests/apps/core/test_core_utils_generate_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import datetime
import random
from unittest import mock
import time_machine

from django.conf import settings
from django.contrib.auth.models import AnonymousUser
Expand Down Expand Up @@ -31,29 +32,28 @@
class TokenUtilsTestCase(TestCase):
"""Test suite for utils related to the JWT token."""


@time_machine.travel(datetime.datetime(2030, 6, 15, tzinfo=datetime.timezone.utc))
def test_utils_generate_token_anonymous(self):
"""Generate a JWT token for a guest."""
now = datetime.datetime(2030, 6, 15, tzinfo=datetime.timezone.utc)

# Test token with a fix admin status
with mock.patch.object(timezone, "now", return_value=now):
token = generate_token(AnonymousUser(), "my-room", is_admin=True)
token = generate_token(AnonymousUser(), "my-room", is_admin=True)

self.assertEqual(
token,
(
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE5MDc3MTI2MDAsImlhdCI6MTkwNzcxMjA"
"wMCwibW9kZXJhdG9yIjp0cnVlLCJhdWQiOiJqaXRzaSIsImlzcyI6ImFwcF9pZCIsInN1YiI6Im1lZXQ"
"uaml0c2kiLCJyb29tIjoibXktcm9vbSIsImNvbnRleHQiOnsidXNlciI6eyJhdmF0YXIiOiJhdmF0YXI"
"uanBnIiwibmFtZSI6Imd1ZXN0IiwiZW1haWwiOiIifX19.yzGbUJywsVqL0WAbzslIoUovLuAKeLBqXs"
"N3h0sPaMo"
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE5MDc3MTI2MDAsImlhdCI6M"
"TkwNzcxMjAwMCwibW9kZXJhdG9yIjp0cnVlLCJhdWQiOiJqaXRzaSIsImlzcyI6ImFwcF9"
"pZCIsInN1YiI6Im1lZXQuaml0c2kiLCJyb29tIjoibXktcm9vbSIsImNvbnRleHQiOnsid"
"XNlciI6eyJhdmF0YXIiOiJhdmF0YXIuanBnIiwibmFtZSI6Imd1ZXN0IiwiZW1haWwiOiI"
"ifX19.mCcDVhhWyWfOEGTpiNdemQlPURTCFGh-AWOrLFY2Tds"
),
)

# Test payload with a random admin status
is_admin = random.choice([True, False]) # nosec
with mock.patch.object(timezone, "now", return_value=now):
token = generate_token(AnonymousUser(), "my-room", is_admin=is_admin)
token = generate_token(AnonymousUser(), "my-room", is_admin=is_admin)

payload = jwt.decode(
token,
Expand All @@ -77,30 +77,28 @@ def test_utils_generate_token_anonymous(self):
},
)

@time_machine.travel(datetime.datetime(2030, 6, 15, tzinfo=datetime.timezone.utc))
def test_utils_generate_token_authenticated(self):
"""Generate a token for a quidam user."""
user = UserFactory(username="mickael", email="mickael@example.com")
now = datetime.datetime(2030, 6, 15, tzinfo=datetime.timezone.utc)

# Test token with a fix admin status
with mock.patch.object(timezone, "now", return_value=now):
token = generate_token(user, "my-room", is_admin=True)
token = generate_token(user, "my-room", is_admin=True)

self.assertEqual(
token,
(
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE5MDc3MTI2MDAsImlhdCI6MTkwNzcxMjA"
"wMCwibW9kZXJhdG9yIjp0cnVlLCJhdWQiOiJqaXRzaSIsImlzcyI6ImFwcF9pZCIsInN1YiI6Im1lZXQ"
"uaml0c2kiLCJyb29tIjoibXktcm9vbSIsImNvbnRleHQiOnsidXNlciI6eyJhdmF0YXIiOiJhdmF0YXI"
"uanBnIiwibmFtZSI6Im1pY2thZWwiLCJlbWFpbCI6Im1pY2thZWxAZXhhbXBsZS5jb20ifX19.sU6tEi"
"0SKOAheTcS4BCXMhYz1ntiTvKi1uAAiuPNP3k"
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE5MDc3MTI2MDAsImlhdCI6M"
"TkwNzcxMjAwMCwibW9kZXJhdG9yIjp0cnVlLCJhdWQiOiJqaXRzaSIsImlzcyI6ImFwcF9"
"pZCIsInN1YiI6Im1lZXQuaml0c2kiLCJyb29tIjoibXktcm9vbSIsImNvbnRleHQiOnsid"
"XNlciI6eyJhdmF0YXIiOiJhdmF0YXIuanBnIiwibmFtZSI6Im1pY2thZWwiLCJlbWFpbCI"
"6Im1pY2thZWxAZXhhbXBsZS5jb20ifX19.0pKgUY7362CaQZYXYgAysot431giCertbe4B"
"IOlJ3_8"
),
)

# Test payload with a random admin status
is_admin = random.choice([True, False]) # nosec
with mock.patch.object(timezone, "now", return_value=now):
token = generate_token(user, "my-room", is_admin=is_admin)
token = generate_token(user, "my-room", is_admin=is_admin)

payload = jwt.decode(
token,
Expand Down Expand Up @@ -128,25 +126,24 @@ def test_utils_generate_token_authenticated(self):
},
)

@time_machine.travel(datetime.datetime(2030, 6, 15, tzinfo=datetime.timezone.utc))
def test_utils_generate_token_staff(self):
"""Generate a token for a staff user."""
user = UserFactory(
username="mickael", email="mickael@example.com", is_staff=True
)
now = datetime.datetime(2030, 6, 15, tzinfo=datetime.timezone.utc)
is_admin = random.choice([True, False]) # nosec

with mock.patch.object(timezone, "now", return_value=now):
token = generate_token(user, "my-room", is_admin=is_admin)
token = generate_token(user, "my-room", is_admin=is_admin)

self.assertEqual(
token,
(
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE5MDc3MTI2MDAsImlhdCI6MTkwNzcxMjA"
"wMCwibW9kZXJhdG9yIjp0cnVlLCJhdWQiOiJqaXRzaSIsImlzcyI6ImFwcF9pZCIsInN1YiI6Im1lZXQ"
"uaml0c2kiLCJyb29tIjoibXktcm9vbSIsImNvbnRleHQiOnsidXNlciI6eyJhdmF0YXIiOiJhdmF0YXI"
"uanBnIiwibmFtZSI6Im1pY2thZWwiLCJlbWFpbCI6Im1pY2thZWxAZXhhbXBsZS5jb20ifX19.sU6tEi"
"0SKOAheTcS4BCXMhYz1ntiTvKi1uAAiuPNP3k"
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE5MDc3MTI2MDAsImlhdCI6M"
"TkwNzcxMjAwMCwibW9kZXJhdG9yIjp0cnVlLCJhdWQiOiJqaXRzaSIsImlzcyI6ImFwcF9"
"pZCIsInN1YiI6Im1lZXQuaml0c2kiLCJyb29tIjoibXktcm9vbSIsImNvbnRleHQiOnsid"
"XNlciI6eyJhdmF0YXIiOiJhdmF0YXIuanBnIiwibmFtZSI6Im1pY2thZWwiLCJlbWFpbCI"
"6Im1pY2thZWxAZXhhbXBsZS5jb20ifX19.0pKgUY7362CaQZYXYgAysot431giCertbe4B"
"IOlJ3_8"
),
)

Expand Down
Empty file.

0 comments on commit 8f34ee6

Please sign in to comment.