-
Notifications
You must be signed in to change notification settings - Fork 10
msw: continue handler migration #1186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
deer-wmde
wants to merge
15
commits into
main
Choose a base branch
from
de/msw-continue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+236
−233
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3007e3b
add verify email handlers
deer-wmde f6cc557
add complaint and contact message handlers
deer-wmde 6c7d012
migrate remaining old handlers
deer-wmde 2adcc96
formatting
deer-wmde 85d9922
fix obj access
deer-wmde 408ab20
fix wiki details response
deer-wmde afb68be
strcit type checks
deer-wmde 51a1dad
fix create wiki response
deer-wmde 33664f4
add handler for current policy
deer-wmde 1c5cf60
disable mock bypass
deer-wmde f33c853
fix removeWiki
deer-wmde a65b6a9
fix user
deer-wmde 8d52c7d
fix entityimport handler
deer-wmde 82be549
formatting
deer-wmde 5565314
remove obsolete note
deer-wmde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,10 +1,9 @@ | ||
| import { http } from 'msw' | ||
|
|
||
| // implementations for msw v2 handlers go in this file | ||
| // see old.default_handlers.js for reference | ||
| // this note can get removed after migration | ||
|
|
||
| const myWikis = JSON.parse(localStorage.getItem('msw-myWikis')) || [] | ||
| let lastWikiId = (myWikis.length && myWikis[myWikis.length - 1].id) || 0 | ||
| let user = makeUser() | ||
| let getEntityImportCalledTimes = 0 | ||
|
|
||
| function makeUser (email = 'test@local') { | ||
| return { | ||
|
|
@@ -16,6 +15,121 @@ function makeUser (email = 'test@local') { | |
| } | ||
| } | ||
|
|
||
| const makeNewWiki = ({ domain, sitename }) => { | ||
| const newWiki = { | ||
| id: ++lastWikiId, | ||
| domain, | ||
| sitename, | ||
| deleted_at: null, | ||
| created_at: '2020-01-01', | ||
| updated_at: '2020-01-01', | ||
| pivot: { | ||
| user_id: user.id, | ||
| wiki_id: lastWikiId, | ||
| }, | ||
| wiki_managers: [{ | ||
| email: user.email, | ||
| pivot: { | ||
| user_id: user.id, | ||
| wiki_id: lastWikiId, | ||
| }, | ||
| }], | ||
| wiki_db_version: { | ||
| id: 101, | ||
| wiki_id: lastWikiId, | ||
| version: 'mw1.33-wbs1', | ||
| }, | ||
| public_settings: [], | ||
| } | ||
|
|
||
| myWikis.push(newWiki) | ||
| localStorage.setItem('msw-myWikis', JSON.stringify(myWikis)) | ||
|
|
||
| return newWiki | ||
| } | ||
|
|
||
| const removeWiki = wikiIndex => { | ||
| myWikis.splice(wikiIndex, 1) | ||
| localStorage.setItem('msw-myWikis', JSON.stringify(myWikis)) | ||
| } | ||
|
|
||
| const wikiDiscovery = (referrer, params) => { | ||
| const pseudorandom = { | ||
| seed: 1, | ||
| next: function () { | ||
| const x = Math.sin(this.seed++) * 10000 | ||
| return x - Math.floor(x) | ||
| }, | ||
| } | ||
|
|
||
| const names = [ | ||
| 'Wikibase Name', | ||
| 'A Very Long Wikibase Name', | ||
| ] | ||
|
|
||
| let wikis = [...Array(75).keys()].map((id) => { | ||
| const wiki = { | ||
| id, | ||
| domain: id + '-wikibase.wbaas.localhost', | ||
| sitename: id + ' - ' + names[id % names.length], | ||
| wiki_site_stats: null, | ||
| logo_url: null, | ||
| } | ||
|
|
||
| if (pseudorandom.next() >= 0.1) { | ||
| wiki.wiki_site_stats = { | ||
| pages: Math.ceil(pseudorandom.next() * 250), | ||
| } | ||
| } | ||
|
|
||
| if (pseudorandom.next() >= 0.5) { | ||
| wiki.logo_url = new URL(referrer).origin + '/favicon.ico' | ||
| } | ||
| return wiki | ||
| }) | ||
|
|
||
| if (parseInt(params.get('is_active'))) { | ||
| wikis = wikis.filter((wiki) => { | ||
| const stats = wiki.wiki_site_stats | ||
| return stats && stats.pages > 1 | ||
| }) | ||
| } | ||
|
|
||
| if (params.get('sort') === 'sitename') { | ||
| wikis = wikis.sort((a, b) => { | ||
| let sort = a.sitename.localeCompare(b.sitename, 'en', { numeric: true }) | ||
| if (params.get('direction') === 'desc') { | ||
| sort *= -1 | ||
| } | ||
| return sort | ||
| }) | ||
| } | ||
|
|
||
| if (params.get('sort') === 'pages') { | ||
| wikis = wikis.sort((a, b) => { | ||
| const aPages = a.wiki_site_stats ? a.wiki_site_stats.pages : 0 | ||
| const bPages = b.wiki_site_stats ? b.wiki_site_stats.pages : 0 | ||
| if (params.get('direction') === 'desc') { | ||
| return bPages - aPages | ||
| } | ||
| return aPages - bPages | ||
| }) | ||
| } | ||
|
|
||
| const currentPage = parseInt(params.get('page')) | ||
| const resultsPerPage = parseInt(params.get('per_page')) | ||
| const start = (currentPage - 1) * resultsPerPage | ||
| const end = start + resultsPerPage | ||
|
|
||
| return { | ||
| data: wikis.slice(start, end), | ||
| meta: { | ||
| last_page: Math.ceil(wikis.length / resultsPerPage), | ||
| total: wikis.length, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| export const handlers = [ | ||
| http.get('/api/auth/login', ({ cookies }) => { | ||
| const { authToken } = cookies | ||
|
|
@@ -25,13 +139,13 @@ export const handlers = [ | |
| }) | ||
| } | ||
|
|
||
| const user = makeUser() | ||
| user = makeUser() | ||
| return Response.json({ user }) | ||
| }), | ||
|
|
||
| http.post('/api/auth/login', async ({ request }) => { | ||
| const body = await request.json() | ||
| const user = makeUser(body.email) | ||
| user = makeUser(body.email) | ||
|
|
||
| return Response.json({ user }, { | ||
| headers: { 'set-cookie': 'authToken=token_value' }, | ||
|
|
@@ -53,13 +167,127 @@ export const handlers = [ | |
| return new Response('Success') | ||
| }), | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| http.post('/api/user/sendVerifyEmail', () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I already mentioned it in this comment
|
||
| return new Response('Already verified') | ||
| }), | ||
|
|
||
| http.post('/api/user/verifyEmail', () => { | ||
| return new Response('Already verified') | ||
| }), | ||
|
|
||
| http.post('/api/complaint/sendMessage', () => { | ||
| return new Response('Success') | ||
| }), | ||
|
|
||
| http.post('/api/contact/sendMessage', async ({ request }) => { | ||
| const body = await request.json() | ||
|
|
||
| if (body.name === '' || body.message === '' || body.subject === '') { | ||
| return new Response(null, { status: 400 }) | ||
| } | ||
|
|
||
| return new Response('Success') | ||
| }), | ||
|
|
||
| http.post('/api/wiki/mine', () => { | ||
| const data = { wikis: myWikis, count: myWikis.length, limit: false } | ||
| return Response.json(data) | ||
| }), | ||
|
|
||
| http.post('/api/wiki/entityImport', () => { | ||
| const data = { status: 'pending', payload: {}, started_at: new Date().toJSON() } | ||
| return Response.json(data) | ||
| }), | ||
|
|
||
| http.get('/api/wiki/entityImport', () => { | ||
| getEntityImportCalledTimes++ | ||
| switch (getEntityImportCalledTimes) { | ||
| case 1: | ||
| return Response.json({ data: [] }) | ||
| case 2: | ||
| return Response.json({ data: [{ status: 'pending' }] }) | ||
| default: | ||
| return Response.json({ data: [{ status: 'success' }] }) | ||
| } | ||
| }), | ||
|
|
||
| http.post('/api/wiki/create', async ({ request }) => { | ||
| const body = await request.json() | ||
| const newWiki = makeNewWiki(body) | ||
| return Response.json({ | ||
| success: true, | ||
| data: newWiki, | ||
| }) | ||
| }), | ||
|
|
||
| http.post('/api/wiki/delete', async ({ request }) => { | ||
| const body = await request.json() | ||
| const wikiId = body.wiki | ||
| const wikiIndex = myWikis.findIndex(w => w.id === Number(wikiId)) | ||
|
|
||
| if (wikiIndex < 0) { | ||
| return new Response(null, { status: 404 }) | ||
| } | ||
|
|
||
| removeWiki(wikiIndex) | ||
| return new Response('Success') | ||
| }), | ||
|
|
||
| http.post('/api/wiki/logo/update', () => { | ||
| return new Response('Success') | ||
| }), | ||
|
|
||
| http.post(/\/api\/wiki\/setting\/.*?\/update$/, () => { | ||
| return new Response('Success') | ||
| }), | ||
|
|
||
| http.post('/api/wiki/details', async ({ request }) => { | ||
| const body = await request.json() | ||
| const wikiId = body.wiki | ||
| const wikiDetails = myWikis.find(w => w.id === Number(wikiId)) | ||
|
|
||
| if (!wikiDetails) { | ||
| return new Response(null, { status: 404 }) | ||
| } | ||
|
|
||
| return Response.json({ | ||
| success: true, | ||
| data: wikiDetails, | ||
| }) | ||
| }), | ||
|
|
||
| http.get('/api/wiki', async ({ request }) => { | ||
| const referrer = await request.referrer | ||
| const url = new URL(await request.url) | ||
|
|
||
| return Response.json(wikiDiscovery(referrer, url.searchParams)) | ||
| }), | ||
|
|
||
| http.get('/api/v1/policies/missing', () => { | ||
| const items = [] | ||
| return Response.json({ items }) | ||
| }), | ||
|
|
||
| http.get('/api/v1/policies/current', () => { | ||
| const items = [ | ||
| { | ||
| metadata: { | ||
| policy_id: 2, | ||
| type: 'terms-of-use', | ||
| active_from: '2026-08-27', | ||
| content_vue_file: 'terms-of-use/version-2.vue', | ||
| }, | ||
| }, | ||
| { | ||
| metadata: { | ||
| policy_id: 3, | ||
| type: 'hosting-policy', | ||
| active_from: '2026-08-27', | ||
| content_vue_file: 'hosting-policy/version-1.vue', | ||
| }, | ||
| }, | ||
| ] | ||
|
|
||
| return Response.json({ items }) | ||
| }), | ||
| ] | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still a bit confused by this part. The mock’s GET
/api/auth/loginhandler can reset theuserwith a fresh user by callingmakeUser().Then POST
/api/auth/loginuses the submitted email to create the session user in line 146, but the next auth check at line 134 throws that away and recreates the default user?