From ad9e39fb815d85e5e718c40540fa75471474fa17 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 25 May 2018 09:57:16 +0200 Subject: Only use account name in routes --- server/controllers/api/accounts.ts | 12 ++++----- server/controllers/services.ts | 2 +- server/helpers/custom-validators/accounts.ts | 3 ++- server/middlewares/validators/account.ts | 32 +++--------------------- server/middlewares/validators/video-channels.ts | 6 ++--- server/tests/api/check-params/accounts.ts | 4 +-- server/tests/api/check-params/video-channels.ts | 10 +++----- server/tests/api/check-params/videos.ts | 6 ++--- server/tests/api/users/users-multiple-servers.ts | 15 ++++++----- server/tests/api/videos/video-channels.ts | 5 ++-- server/tests/api/videos/video-nsfw.ts | 6 ++--- server/tests/utils/users/accounts.ts | 4 +-- server/tests/utils/videos/video-channels.ts | 4 +-- server/tests/utils/videos/videos.ts | 4 +-- 14 files changed, 43 insertions(+), 70 deletions(-) (limited to 'server') diff --git a/server/controllers/api/accounts.ts b/server/controllers/api/accounts.ts index ccae0436b..8e937276c 100644 --- a/server/controllers/api/accounts.ts +++ b/server/controllers/api/accounts.ts @@ -8,7 +8,7 @@ import { setDefaultPagination, setDefaultSort } from '../../middlewares' -import { accountsGetValidator, accountsSortValidator, videosSortValidator } from '../../middlewares/validators' +import { accountsNameWithHostGetValidator, accountsSortValidator, videosSortValidator } from '../../middlewares/validators' import { AccountModel } from '../../models/account/account' import { VideoModel } from '../../models/video/video' import { isNSFWHidden } from '../../helpers/express-utils' @@ -24,13 +24,13 @@ accountsRouter.get('/', asyncMiddleware(listAccounts) ) -accountsRouter.get('/:id', - asyncMiddleware(accountsGetValidator), +accountsRouter.get('/:accountName', + asyncMiddleware(accountsNameWithHostGetValidator), getAccount ) -accountsRouter.get('/:id/videos', - asyncMiddleware(accountsGetValidator), +accountsRouter.get('/:accountName/videos', + asyncMiddleware(accountsNameWithHostGetValidator), paginationValidator, videosSortValidator, setDefaultSort, @@ -39,7 +39,7 @@ accountsRouter.get('/:id/videos', asyncMiddleware(listAccountVideos) ) -accountsRouter.get('/:accountId/video-channels', +accountsRouter.get('/:accountName/video-channels', asyncMiddleware(listVideoAccountChannelsValidator), asyncMiddleware(listVideoAccountChannels) ) diff --git a/server/controllers/services.ts b/server/controllers/services.ts index c272edccd..a58a5b8cf 100644 --- a/server/controllers/services.ts +++ b/server/controllers/services.ts @@ -10,7 +10,7 @@ servicesRouter.use('/oembed', asyncMiddleware(oembedValidator), generateOEmbed ) -servicesRouter.use('/redirect/accounts/:nameWithHost', +servicesRouter.use('/redirect/accounts/:accountName', asyncMiddleware(accountsNameWithHostGetValidator), redirectToAccountUrl ) diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts index 00dea9039..0607d661c 100644 --- a/server/helpers/custom-validators/accounts.ts +++ b/server/helpers/custom-validators/accounts.ts @@ -5,6 +5,7 @@ import * as validator from 'validator' import { AccountModel } from '../../models/account/account' import { isUserDescriptionValid, isUserUsernameValid } from './users' import { exists } from './misc' +import { CONFIG } from '../../initializers' function isAccountNameValid (value: string) { return isUserUsernameValid(value) @@ -40,7 +41,7 @@ function isAccountNameWithHostExist (nameWithDomain: string, res: Response, send const [ accountName, host ] = nameWithDomain.split('@') let promise: Bluebird - if (!host) promise = AccountModel.loadLocalByName(accountName) + if (!host || host === CONFIG.WEBSERVER.HOST) promise = AccountModel.loadLocalByName(accountName) else promise = AccountModel.loadLocalByNameAndHost(accountName, host) return isAccountExist(promise, res, sendNotFound) diff --git a/server/middlewares/validators/account.ts b/server/middlewares/validators/account.ts index c01e742da..b3a51e631 100644 --- a/server/middlewares/validators/account.ts +++ b/server/middlewares/validators/account.ts @@ -1,15 +1,8 @@ import * as express from 'express' import { param } from 'express-validator/check' -import { - isAccountIdExist, - isAccountIdValid, - isAccountNameValid, - isAccountNameWithHostExist, - isLocalAccountNameExist -} from '../../helpers/custom-validators/accounts' +import { isAccountNameValid, isAccountNameWithHostExist, isLocalAccountNameExist } from '../../helpers/custom-validators/accounts' import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' -import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' const localAccountValidator = [ param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'), @@ -24,32 +17,14 @@ const localAccountValidator = [ } ] -const accountsGetValidator = [ - param('id').custom(isAccountIdValid).withMessage('Should have a valid id/uuid/name/name with host'), - - async (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking accountsGetValidator parameters', { parameters: req.params }) - - if (areValidationErrors(req, res)) return - - let accountFetched = false - if (isIdOrUUIDValid(req.params.id)) accountFetched = await isAccountIdExist(req.params.id, res, false) - if (!accountFetched) accountFetched = await isAccountNameWithHostExist(req.params.id, res, true) - - if (!accountFetched) return - - return next() - } -] - const accountsNameWithHostGetValidator = [ - param('nameWithHost').exists().withMessage('Should have an account name with host'), + param('accountName').exists().withMessage('Should have an account name with host'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking accountsNameWithHostGetValidator parameters', { parameters: req.params }) if (areValidationErrors(req, res)) return - if (!await isAccountNameWithHostExist(req.params.nameWithHost, res)) return + if (!await isAccountNameWithHostExist(req.params.accountName, res)) return return next() } @@ -59,6 +34,5 @@ const accountsNameWithHostGetValidator = [ export { localAccountValidator, - accountsGetValidator, accountsNameWithHostGetValidator } diff --git a/server/middlewares/validators/video-channels.ts b/server/middlewares/validators/video-channels.ts index 92c0de419..a5be5f114 100644 --- a/server/middlewares/validators/video-channels.ts +++ b/server/middlewares/validators/video-channels.ts @@ -1,7 +1,7 @@ import * as express from 'express' import { body, param } from 'express-validator/check' import { UserRight } from '../../../shared' -import { isAccountIdExist } from '../../helpers/custom-validators/accounts' +import { isAccountIdExist, isAccountNameWithHostExist } from '../../helpers/custom-validators/accounts' import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' import { isVideoChannelDescriptionValid, @@ -15,13 +15,13 @@ import { VideoChannelModel } from '../../models/video/video-channel' import { areValidationErrors } from './utils' const listVideoAccountChannelsValidator = [ - param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'), + param('accountName').exists().withMessage('Should have a valid account name'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body }) if (areValidationErrors(req, res)) return - if (!await isAccountIdExist(req.params.accountId, res)) return + if (!await isAccountNameWithHostExist(req.params.accountName, res)) return return next() } diff --git a/server/tests/api/check-params/accounts.ts b/server/tests/api/check-params/accounts.ts index 50dc0804e..9e0b1e35c 100644 --- a/server/tests/api/check-params/accounts.ts +++ b/server/tests/api/check-params/accounts.ts @@ -35,8 +35,8 @@ describe('Test users API validators', function () { }) describe('When getting an account', function () { - it('Should return 404 with a non existing id', async function () { - await getAccount(server.url, 4545454, 404) + it('Should return 404 with a non existing name', async function () { + await getAccount(server.url, 'arfaze', 404) }) }) diff --git a/server/tests/api/check-params/video-channels.ts b/server/tests/api/check-params/video-channels.ts index 56b990be6..5080af2c9 100644 --- a/server/tests/api/check-params/video-channels.ts +++ b/server/tests/api/check-params/video-channels.ts @@ -7,7 +7,8 @@ import { createUser, deleteVideoChannel, flushTests, - getAccountVideoChannelsList, getMyUserInformation, + getAccountVideoChannelsList, + getMyUserInformation, getVideoChannelsList, immutableAssign, killallServers, @@ -20,7 +21,6 @@ import { userLogin } from '../../utils' import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '../../utils/requests/check-api-params' -import { getAccountsList } from '../../utils/users/accounts' import { User } from '../../../../shared/models/users' const expect = chai.expect @@ -74,12 +74,8 @@ describe('Test video channels API validator', function () { }) describe('When listing account video channels', function () { - it('Should fail with bad account', async function () { - await getAccountVideoChannelsList(server.url, 'hello', 400) - }) - it('Should fail with a unknown account', async function () { - await getAccountVideoChannelsList(server.url, 154, 404) + await getAccountVideoChannelsList(server.url, 'unknown', 404) }) }) diff --git a/server/tests/api/check-params/videos.ts b/server/tests/api/check-params/videos.ts index c81e9752e..7b40b91e7 100644 --- a/server/tests/api/check-params/videos.ts +++ b/server/tests/api/check-params/videos.ts @@ -18,7 +18,7 @@ describe('Test videos API validator', function () { const path = '/api/v1/videos/' let server: ServerInfo let userAccessToken = '' - let accountUUID: string + let accountName: string let channelId: number let channelUUID: string let videoId @@ -43,7 +43,7 @@ describe('Test videos API validator', function () { const res = await getMyUserInformation(server.url, server.accessToken) channelId = res.body.videoChannels[ 0 ].id channelUUID = res.body.videoChannels[ 0 ].uuid - accountUUID = res.body.account.uuid + accountName = res.body.account.name + '@' + res.body.account.host } }) @@ -116,7 +116,7 @@ describe('Test videos API validator', function () { let path: string before(async function () { - path = '/api/v1/accounts/' + accountUUID + '/videos' + path = '/api/v1/accounts/' + accountName + '/videos' }) it('Should fail with a bad start pagination', async function () { diff --git a/server/tests/api/users/users-multiple-servers.ts b/server/tests/api/users/users-multiple-servers.ts index 8b9b63348..0e1e6c97d 100644 --- a/server/tests/api/users/users-multiple-servers.ts +++ b/server/tests/api/users/users-multiple-servers.ts @@ -26,7 +26,7 @@ const expect = chai.expect describe('Test users with multiple servers', function () { let servers: ServerInfo[] = [] let user: User - let userAccountUUID: string + let userAccountName: string let userVideoChannelUUID: string let userId: number let videoUUID: string @@ -56,12 +56,15 @@ describe('Test users with multiple servers', function () { password: 'password' } const res = await createUser(servers[ 0 ].url, servers[ 0 ].accessToken, user.username, user.password) - userAccountUUID = res.body.user.account.uuid userId = res.body.user.id - userAccessToken = await userLogin(servers[ 0 ], user) } + { + const res = await getMyUserInformation(servers[0].url, userAccessToken) + userAccountName = res.body.account.name + '@' + res.body.account.host + } + { const res = await getMyUserInformation(servers[ 0 ].url, servers[ 0 ].accessToken) const user: User = res.body @@ -135,7 +138,7 @@ describe('Test users with multiple servers', function () { const rootServer1List = resAccounts.body.data.find(a => a.name === 'root' && a.host === 'localhost:9001') as Account expect(rootServer1List).not.to.be.undefined - const resAccount = await getAccount(server.url, rootServer1List.id) + const resAccount = await getAccount(server.url, rootServer1List.name + '@' + rootServer1List.host) const rootServer1Get = resAccount.body as Account expect(rootServer1Get.name).to.equal('root') expect(rootServer1Get.host).to.equal('localhost:9001') @@ -148,7 +151,7 @@ describe('Test users with multiple servers', function () { it('Should list account videos', async function () { for (const server of servers) { - const res = await getAccountVideos(server.url, server.accessToken, userAccountUUID, 0, 5) + const res = await getAccountVideos(server.url, server.accessToken, userAccountName, 0, 5) expect(res.body.total).to.equal(1) expect(res.body.data).to.be.an('array') @@ -193,7 +196,7 @@ describe('Test users with multiple servers', function () { it('Should not have actor files', async () => { for (const server of servers) { - await checkActorFilesWereRemoved(userAccountUUID, server.serverNumber) + await checkActorFilesWereRemoved(userAccountName, server.serverNumber) await checkActorFilesWereRemoved(userVideoChannelUUID, server.serverNumber) } }) diff --git a/server/tests/api/videos/video-channels.ts b/server/tests/api/videos/video-channels.ts index 35c418f7c..7ae505fd7 100644 --- a/server/tests/api/videos/video-channels.ts +++ b/server/tests/api/videos/video-channels.ts @@ -17,7 +17,6 @@ import { setAccessTokensToServers, updateVideoChannel } from '../../utils/index' -import { getAccountsList } from '../../utils/users/accounts' const expect = chai.expect @@ -99,7 +98,7 @@ describe('Test video channels', function () { }) it('Should have two video channels when getting account channels on server 1', async function () { - const res = await getAccountVideoChannelsList(servers[0].url, userInfo.account.uuid) + const res = await getAccountVideoChannelsList(servers[0].url, userInfo.account.name + '@' + userInfo.account.host) expect(res.body.total).to.equal(2) expect(res.body.data).to.be.an('array') expect(res.body.data).to.have.lengthOf(2) @@ -112,7 +111,7 @@ describe('Test video channels', function () { }) it('Should have one video channel when getting account channels on server 2', async function () { - const res = await getAccountVideoChannelsList(servers[1].url, userInfo.account.uuid) + const res = await getAccountVideoChannelsList(servers[1].url, userInfo.account.name + '@' + userInfo.account.host) expect(res.body.total).to.equal(1) expect(res.body.data).to.be.an('array') expect(res.body.data).to.have.lengthOf(1) diff --git a/server/tests/api/videos/video-nsfw.ts b/server/tests/api/videos/video-nsfw.ts index b8c85f45b..a8f152561 100644 --- a/server/tests/api/videos/video-nsfw.ts +++ b/server/tests/api/videos/video-nsfw.ts @@ -32,13 +32,13 @@ describe('Test video NSFW policy', function () { .then(res => { const user: User = res.body const videoChannelUUID = user.videoChannels[0].uuid - const accountUUID = user.account.uuid + const accountName = user.account.name + '@' + user.account.host if (token) { return Promise.all([ getVideosListWithToken(server.url, token), searchVideoWithToken(server.url, 'n', token), - getAccountVideos(server.url, token, accountUUID, 0, 5), + getAccountVideos(server.url, token, accountName, 0, 5), getVideoChannelVideos(server.url, token, videoChannelUUID, 0, 5) ]) } @@ -46,7 +46,7 @@ describe('Test video NSFW policy', function () { return Promise.all([ getVideosList(server.url), searchVideo(server.url, 'n'), - getAccountVideos(server.url, undefined, accountUUID, 0, 5), + getAccountVideos(server.url, undefined, accountName, 0, 5), getVideoChannelVideos(server.url, undefined, videoChannelUUID, 0, 5) ]) }) diff --git a/server/tests/utils/users/accounts.ts b/server/tests/utils/users/accounts.ts index a5c13c319..30b3c54f8 100644 --- a/server/tests/utils/users/accounts.ts +++ b/server/tests/utils/users/accounts.ts @@ -19,8 +19,8 @@ function getAccountsList (url: string, sort = '-createdAt', statusCodeExpected = }) } -function getAccount (url: string, accountId: number | string, statusCodeExpected = 200) { - const path = '/api/v1/accounts/' + accountId +function getAccount (url: string, accountName: string, statusCodeExpected = 200) { + const path = '/api/v1/accounts/' + accountName return makeGetRequest({ url, diff --git a/server/tests/utils/videos/video-channels.ts b/server/tests/utils/videos/video-channels.ts index 021c4c420..a064598f4 100644 --- a/server/tests/utils/videos/video-channels.ts +++ b/server/tests/utils/videos/video-channels.ts @@ -16,8 +16,8 @@ function getVideoChannelsList (url: string, start: number, count: number, sort?: .expect('Content-Type', /json/) } -function getAccountVideoChannelsList (url: string, accountId: number | string, specialStatus = 200) { - const path = '/api/v1/accounts/' + accountId + '/video-channels' +function getAccountVideoChannelsList (url: string, accountName: string, specialStatus = 200) { + const path = '/api/v1/accounts/' + accountName + '/video-channels' return request(url) .get(path) diff --git a/server/tests/utils/videos/videos.ts b/server/tests/utils/videos/videos.ts index 07c4ffc77..46fa5f79d 100644 --- a/server/tests/utils/videos/videos.ts +++ b/server/tests/utils/videos/videos.ts @@ -167,8 +167,8 @@ function getMyVideos (url: string, accessToken: string, start: number, count: nu .expect('Content-Type', /json/) } -function getAccountVideos (url: string, accessToken: string, accountId: number | string, start: number, count: number, sort?: string) { - const path = '/api/v1/accounts/' + accountId + '/videos' +function getAccountVideos (url: string, accessToken: string, accountName: string, start: number, count: number, sort?: string) { + const path = '/api/v1/accounts/' + accountName + '/videos' return makeGetRequest({ url, -- cgit v1.2.3