1
0
Fork 0

Introduce login command

This commit is contained in:
Chocobozzz 2021-07-13 11:05:15 +02:00
parent 6c5065a011
commit 41d1d07501
No known key found for this signature in database
GPG key ID: 583A612D890159BE
63 changed files with 409 additions and 443 deletions

View file

@ -11,7 +11,6 @@ import {
ServerInfo,
setAccessTokensToServers,
uploadVideo,
userLogin,
waitJobs
} from '../../../../shared/extra-utils'
import { Video } from '../../../../shared/models/videos'
@ -36,7 +35,7 @@ describe('Test ActivityPub fetcher', function () {
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
}
const userAccessToken = await userLogin(servers[0], user)
const userAccessToken = await servers[0].loginCommand.getAccessToken(user)
await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video root' })
const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'bad video root' })

View file

@ -18,7 +18,6 @@ import {
ServerInfo,
setAccessTokensToServers,
uploadVideo,
userLogin,
waitJobs
} from '@shared/extra-utils'
import { AbuseCreate, AbuseState } from '@shared/models'
@ -47,7 +46,7 @@ describe('Test abuses API validators', function () {
const username = 'user1'
const password = 'my super password'
await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
userToken = await userLogin(server, { username, password })
userToken = await server.loginCommand.getAccessToken({ username, password })
userToken2 = await generateUserAccessToken(server, 'user_2')

View file

@ -11,8 +11,7 @@ import {
makeGetRequest,
makePostBodyRequest,
ServerInfo,
setAccessTokensToServers,
userLogin
setAccessTokensToServers
} from '../../../../shared/extra-utils'
import {
checkBadCountPagination,
@ -37,7 +36,7 @@ describe('Test blocklist API validators', function () {
const user = { username: 'user1', password: 'password' }
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
userAccessToken = await userLogin(server, user)
userAccessToken = await server.loginCommand.getAccessToken(user)
await doubleFollow(servers[0], servers[1])
})

View file

@ -6,8 +6,7 @@ import {
createUser,
flushAndRunServer,
ServerInfo,
setAccessTokensToServers,
userLogin
setAccessTokensToServers
} from '../../../../shared/extra-utils'
import { makePostBodyRequest } from '../../../../shared/extra-utils/requests/requests'
import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
@ -27,7 +26,7 @@ describe('Test bulk API validators', function () {
const user = { username: 'user1', password: 'password' }
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
userAccessToken = await userLogin(server, user)
userAccessToken = await server.loginCommand.getAccessToken(user)
})
describe('When removing comments of', function () {

View file

@ -11,8 +11,7 @@ import {
makeGetRequest,
makePutBodyRequest,
ServerInfo,
setAccessTokensToServers,
userLogin
setAccessTokensToServers
} from '@shared/extra-utils'
import { CustomConfig } from '@shared/models'
@ -208,7 +207,7 @@ describe('Test config API validators', function () {
password: 'password'
}
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
userAccessToken = await userLogin(server, user)
userAccessToken = await server.loginCommand.getAccessToken(user)
})
describe('When getting the configuration', function () {

View file

@ -7,8 +7,7 @@ import {
createUser,
flushAndRunServer,
ServerInfo,
setAccessTokensToServers,
userLogin
setAccessTokensToServers
} from '../../../../shared/extra-utils'
import { makeGetRequest, makePutBodyRequest } from '../../../../shared/extra-utils/requests/requests'
@ -29,7 +28,7 @@ describe('Test custom pages validators', function () {
const user = { username: 'user1', password: 'password' }
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
userAccessToken = await userLogin(server, user)
userAccessToken = await server.loginCommand.getAccessToken(user)
})
describe('When updating instance homepage', function () {

View file

@ -7,8 +7,7 @@ import {
createUser,
flushAndRunServer,
ServerInfo,
setAccessTokensToServers,
userLogin
setAccessTokensToServers
} from '../../../../shared/extra-utils'
import { makeGetRequest } from '../../../../shared/extra-utils/requests/requests'
import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
@ -32,7 +31,7 @@ describe('Test debug API validators', function () {
password: 'my super password'
}
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
userAccessToken = await userLogin(server, user)
userAccessToken = await server.loginCommand.getAccessToken(user)
})
describe('When getting debug endpoint', function () {

View file

@ -9,8 +9,7 @@ import {
makeDeleteRequest, makeGetRequest,
makePostBodyRequest,
ServerInfo,
setAccessTokensToServers,
userLogin
setAccessTokensToServers
} from '../../../../shared/extra-utils'
import {
checkBadCountPagination,
@ -42,7 +41,7 @@ describe('Test server follows API validators', function () {
}
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
userAccessToken = await userLogin(server, user)
userAccessToken = await server.loginCommand.getAccessToken(user)
})
describe('When adding follows', function () {

View file

@ -7,8 +7,7 @@ import {
createUser,
flushAndRunServer,
ServerInfo,
setAccessTokensToServers,
userLogin
setAccessTokensToServers
} from '../../../../shared/extra-utils'
import {
checkBadCountPagination,
@ -37,7 +36,7 @@ describe('Test jobs API validators', function () {
password: 'my super password'
}
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
userAccessToken = await userLogin(server, user)
userAccessToken = await server.loginCommand.getAccessToken(user)
})
describe('When listing jobs', function () {

View file

@ -17,8 +17,7 @@ import {
ServerInfo,
setAccessTokensToServers,
stopFfmpeg,
uploadVideoAndGetId,
userLogin
uploadVideoAndGetId
} from '../../../../shared/extra-utils'
describe('Test video lives API validator', function () {
@ -53,7 +52,7 @@ describe('Test video lives API validator', function () {
const username = 'user1'
const password = 'my super password'
await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
userAccessToken = await userLogin(server, { username, password })
userAccessToken = await server.loginCommand.getAccessToken({ username, password })
{
const res = await getMyUserInformation(server.url, server.accessToken)
@ -150,7 +149,7 @@ describe('Test video lives API validator', function () {
}
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
const accessTokenUser = await userLogin(server, user)
const accessTokenUser = await server.loginCommand.getAccessToken(user)
const res = await getMyUserInformation(server.url, accessTokenUser)
const customChannelId = res.body.videoChannels[0].id

View file

@ -7,8 +7,7 @@ import {
createUser,
flushAndRunServer,
ServerInfo,
setAccessTokensToServers,
userLogin
setAccessTokensToServers
} from '../../../../shared/extra-utils'
import { makeGetRequest } from '../../../../shared/extra-utils/requests/requests'
import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
@ -32,7 +31,7 @@ describe('Test logs API validators', function () {
password: 'my super password'
}
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
userAccessToken = await userLogin(server, user)
userAccessToken = await server.loginCommand.getAccessToken(user)
})
describe('When getting logs', function () {

View file

@ -13,8 +13,7 @@ import {
makePostBodyRequest,
makePutBodyRequest,
ServerInfo,
setAccessTokensToServers,
userLogin
setAccessTokensToServers
} from '@shared/extra-utils'
import { PeerTubePlugin, PluginType } from '@shared/models'
@ -45,7 +44,7 @@ describe('Test server plugins API validators', function () {
}
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
userAccessToken = await userLogin(server, user)
userAccessToken = await server.loginCommand.getAccessToken(user)
{
const res = await server.pluginsCommand.install({ npmName: npmPlugin })

View file

@ -19,7 +19,6 @@ import {
ServerInfo,
setAccessTokensToServers,
uploadVideoAndGetId,
userLogin,
waitJobs
} from '../../../../shared/extra-utils'
@ -45,7 +44,7 @@ describe('Test server redundancy API validators', function () {
}
await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
userAccessToken = await userLogin(servers[0], user)
userAccessToken = await servers[0].loginCommand.getAccessToken(user)
videoIdLocal = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video' })).id

View file

@ -15,7 +15,6 @@ import {
setDefaultVideoChannel,
updateUser,
uploadVideo,
userLogin,
waitJobs
} from '../../../../shared/extra-utils'
@ -50,7 +49,7 @@ describe('Test upload quota', function () {
const user = { username: 'registered' + randomInt(1, 1500), password: 'password' }
await registerUser(server.url, user.username, user.password)
const userAccessToken = await userLogin(server, user)
const userAccessToken = await server.loginCommand.getAccessToken(user)
const videoAttributes = { fixture: 'video_short2.webm' }
for (let i = 0; i < 5; i++) {
@ -65,7 +64,7 @@ describe('Test upload quota', function () {
const user = { username: 'registered' + randomInt(1, 1500), password: 'password' }
await registerUser(server.url, user.username, user.password)
const userAccessToken = await userLogin(server, user)
const userAccessToken = await server.loginCommand.getAccessToken(user)
const videoAttributes = { fixture: 'video_short2.webm' }
for (let i = 0; i < 5; i++) {

View file

@ -10,8 +10,7 @@ import {
makeGetRequest,
makePostBodyRequest,
ServerInfo,
setAccessTokensToServers,
userLogin
setAccessTokensToServers
} from '../../../../shared/extra-utils'
import {
@ -41,7 +40,7 @@ describe('Test user subscriptions API validators', function () {
password: 'my super password'
}
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
userAccessToken = await userLogin(server, user)
userAccessToken = await server.loginCommand.getAccessToken(user)
})
describe('When listing my subscriptions', function () {

View file

@ -27,8 +27,7 @@ import {
ServerInfo,
setAccessTokensToServers,
unblockUser,
uploadVideo,
userLogin
uploadVideo
} from '../../../../shared/extra-utils'
import { MockSmtpServer } from '../../../../shared/extra-utils/mock-servers/mock-email'
import {
@ -87,7 +86,7 @@ describe('Test users API validators', function () {
password: user.password,
videoQuota: videoQuota
})
userAccessToken = await userLogin(server, user)
userAccessToken = await server.loginCommand.getAccessToken(user)
}
{
@ -104,7 +103,7 @@ describe('Test users API validators', function () {
role: UserRole.MODERATOR
})
moderatorAccessToken = await userLogin(server, moderator)
moderatorAccessToken = await server.loginCommand.getAccessToken(moderator)
}
{
@ -394,7 +393,7 @@ describe('Test users API validators', function () {
username: 'user1',
password: 'my super password'
}
userAccessToken = await userLogin(server, user)
userAccessToken = await server.loginCommand.getAccessToken(user)
const fields = {
username: 'user3',

View file

@ -19,7 +19,6 @@ import {
ServerInfo,
setAccessTokensToServers,
uploadVideo,
userLogin,
waitJobs
} from '@shared/extra-utils'
import { VideoBlacklistType, VideoDetails } from '@shared/models'
@ -46,14 +45,14 @@ describe('Test video blacklist API validators', function () {
const username = 'user1'
const password = 'my super password'
await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: username, password: password })
userAccessToken1 = await userLogin(servers[0], { username, password })
userAccessToken1 = await servers[0].loginCommand.getAccessToken({ username, password })
}
{
const username = 'user2'
const password = 'my super password'
await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: username, password: password })
userAccessToken2 = await userLogin(servers[0], { username, password })
userAccessToken2 = await servers[0].loginCommand.getAccessToken({ username, password })
}
{

View file

@ -12,8 +12,7 @@ import {
makeUploadRequest,
ServerInfo,
setAccessTokensToServers,
uploadVideo,
userLogin
uploadVideo
} from '@shared/extra-utils'
import { VideoCreateResult } from '@shared/models'
@ -44,7 +43,7 @@ describe('Test video captions API validator', function () {
password: 'my super password'
}
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
userAccessToken = await userLogin(server, user)
userAccessToken = await server.loginCommand.getAccessToken(user)
}
})

View file

@ -18,8 +18,7 @@ import {
makePutBodyRequest,
makeUploadRequest,
ServerInfo,
setAccessTokensToServers,
userLogin
setAccessTokensToServers
} from '@shared/extra-utils'
import { VideoChannelUpdate } from '@shared/models'
@ -47,7 +46,7 @@ describe('Test video channels API validator', function () {
{
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
accessTokenUser = await userLogin(server, user)
accessTokenUser = await server.loginCommand.getAccessToken(user)
}
command = server.channelsCommand

View file

@ -15,8 +15,7 @@ import {
makePostBodyRequest,
ServerInfo,
setAccessTokensToServers,
uploadVideo,
userLogin
uploadVideo
} from '@shared/extra-utils'
import { VideoCreateResult } from '@shared/models'
@ -55,13 +54,13 @@ describe('Test video comments API validator', function () {
{
const user = { username: 'user1', password: 'my super password' }
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
userAccessToken = await userLogin(server, user)
userAccessToken = await server.loginCommand.getAccessToken(user)
}
{
const user = { username: 'user2', password: 'my super password' }
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
userAccessToken2 = await userLogin(server, user)
userAccessToken2 = await server.loginCommand.getAccessToken(user)
}
})

View file

@ -17,8 +17,7 @@ import {
makePostBodyRequest,
makeUploadRequest,
ServerInfo,
setAccessTokensToServers,
userLogin
setAccessTokensToServers
} from '@shared/extra-utils'
import { VideoPrivacy } from '@shared/models'
@ -40,7 +39,7 @@ describe('Test video imports API validator', function () {
const username = 'user1'
const password = 'my super password'
await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
userAccessToken = await userLogin(server, { username, password })
userAccessToken = await server.loginCommand.getAccessToken({ username, password })
{
const res = await getMyUserInformation(server.url, server.accessToken)
@ -167,7 +166,7 @@ describe('Test video imports API validator', function () {
}
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
const accessTokenUser = await userLogin(server, user)
const accessTokenUser = await server.loginCommand.getAccessToken(user)
const res = await getMyUserInformation(server.url, accessTokenUser)
const customChannelId = res.body.videoChannels[0].id

View file

@ -8,8 +8,7 @@ import {
makeGetRequest,
ServerInfo,
setAccessTokensToServers,
setDefaultVideoChannel,
userLogin
setDefaultVideoChannel
} from '../../../../shared/extra-utils'
import { UserRole } from '../../../../shared/models/users'
import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
@ -52,7 +51,7 @@ describe('Test video filters validators', function () {
const user = { username: 'user1', password: 'my super password' }
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
userAccessToken = await userLogin(server, user)
userAccessToken = await server.loginCommand.getAccessToken(user)
const moderator = { username: 'moderator', password: 'my super password' }
await createUser(
@ -66,7 +65,7 @@ describe('Test video filters validators', function () {
role: UserRole.MODERATOR
}
)
moderatorAccessToken = await userLogin(server, moderator)
moderatorAccessToken = await server.loginCommand.getAccessToken(moderator)
})
describe('When setting a video filter', function () {

View file

@ -22,8 +22,7 @@ import {
removeVideo,
root,
ServerInfo,
setAccessTokensToServers,
userLogin
setAccessTokensToServers
} from '../../../../shared/extra-utils'
import {
checkBadCountPagination,
@ -55,7 +54,7 @@ describe('Test videos API validator', function () {
const username = 'user1'
const password = 'my super password'
await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
userAccessToken = await userLogin(server, { username, password })
userAccessToken = await server.loginCommand.getAccessToken({ username, password })
{
const res = await getMyUserInformation(server.url, server.accessToken)
@ -286,7 +285,7 @@ describe('Test videos API validator', function () {
}
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
const accessTokenUser = await userLogin(server, user)
const accessTokenUser = await server.loginCommand.getAccessToken(user)
const res = await getMyUserInformation(server.url, accessTokenUser)
const customChannelId = res.body.videoChannels[0].id

View file

@ -17,7 +17,6 @@ import {
setAccessTokensToServers,
uploadVideo,
uploadVideoAndGetId,
userLogin,
waitJobs
} from '@shared/extra-utils'
import { AbuseMessage, AbusePredefinedReasonsString, AbuseState, AdminAbuse, UserAbuse } from '@shared/models'
@ -280,7 +279,7 @@ describe('Test abuses', function () {
// register a second user to have two reporters/reportees
const user = { username: 'user2', password: 'password' }
await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, ...user })
const userAccessToken = await userLogin(servers[0], user)
const userAccessToken = await servers[0].loginCommand.getAccessToken(user)
// upload a third video via this user
const video3Attributes = {

View file

@ -10,7 +10,6 @@ import {
ServerInfo,
setAccessTokensToServers,
uploadVideo,
userLogin,
waitJobs
} from '@shared/extra-utils'
import { UserNotificationType } from '@shared/models'
@ -86,7 +85,7 @@ describe('Test blocklist', function () {
videoQuotaDaily: -1
})
userToken1 = await userLogin(servers[0], user)
userToken1 = await servers[0].loginCommand.getAccessToken(user)
await uploadVideo(servers[0].url, userToken1, { name: 'video user 1' })
}
@ -94,14 +93,14 @@ describe('Test blocklist', function () {
const user = { username: 'user2', password: 'password' }
await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
userToken2 = await userLogin(servers[0], user)
userToken2 = await servers[0].loginCommand.getAccessToken(user)
}
{
const user = { username: 'user3', password: 'password' }
await createUser({ url: servers[1].url, accessToken: servers[1].accessToken, username: user.username, password: user.password })
remoteUserToken = await userLogin(servers[1], user)
remoteUserToken = await servers[1].loginCommand.getAccessToken(user)
}
await doubleFollow(servers[0], servers[1])

View file

@ -14,7 +14,6 @@ import {
ServerInfo,
setAccessTokensToServers,
uploadVideo,
userLogin,
waitJobs
} from '@shared/extra-utils'
import { UserNotificationType, Video } from '@shared/models'
@ -94,7 +93,7 @@ describe('Test blocklist', function () {
const user = { username: 'user1', password: 'password' }
await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
userToken1 = await userLogin(servers[0], user)
userToken1 = await servers[0].loginCommand.getAccessToken(user)
await uploadVideo(servers[0].url, userToken1, { name: 'video user 1' })
}
@ -102,14 +101,14 @@ describe('Test blocklist', function () {
const user = { username: 'moderator', password: 'password' }
await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
userModeratorToken = await userLogin(servers[0], user)
userModeratorToken = await servers[0].loginCommand.getAccessToken(user)
}
{
const user = { username: 'user2', password: 'password' }
await createUser({ url: servers[1].url, accessToken: servers[1].accessToken, username: user.username, password: user.password })
userToken2 = await userLogin(servers[1], user)
userToken2 = await servers[1].loginCommand.getAccessToken(user)
await uploadVideo(servers[1].url, userToken2, { name: 'video user 2' })
}

View file

@ -19,7 +19,6 @@ import {
setAccessTokensToServers,
updateVideo,
uploadVideo,
userLogin,
waitJobs
} from '@shared/extra-utils'
import { User, UserAdminFlag, UserRole, VideoBlacklist, VideoBlacklistType } from '@shared/models'
@ -366,7 +365,7 @@ describe('Test video blacklist', function () {
role: UserRole.USER
})
userWithoutFlag = await userLogin(servers[0], user)
userWithoutFlag = await servers[0].loginCommand.getAccessToken(user)
const res = await getMyUserInformation(servers[0].url, userWithoutFlag)
const body: User = res.body
@ -384,7 +383,7 @@ describe('Test video blacklist', function () {
role: UserRole.USER
})
userWithFlag = await userLogin(servers[0], user)
userWithFlag = await servers[0].loginCommand.getAccessToken(user)
}
await waitJobs(servers)

View file

@ -13,7 +13,6 @@ import {
updateMyUser,
updateVideo,
uploadVideo,
userLogin,
wait,
waitJobs
} from '@shared/extra-utils'
@ -47,7 +46,7 @@ describe('Test ActivityPub video channels search', function () {
{
const user = { username: 'user1_server2', password: 'password' }
await createUser({ url: servers[1].url, accessToken: servers[1].accessToken, username: user.username, password: user.password })
userServer2Token = await userLogin(servers[1], user)
userServer2Token = await servers[1].loginCommand.getAccessToken(user)
const channel = {
name: 'channel1_server2',

View file

@ -2,7 +2,6 @@
import 'mocha'
import * as chai from 'chai'
import { Video } from '@shared/models'
import {
BulkCommand,
cleanupTests,
@ -13,9 +12,9 @@ import {
ServerInfo,
setAccessTokensToServers,
uploadVideo,
userLogin,
waitJobs
} from '../../../../shared/extra-utils/index'
} from '@shared/extra-utils'
import { Video } from '@shared/models'
const expect = chai.expect
@ -41,21 +40,21 @@ describe('Test bulk actions', function () {
const user = { username: 'user1', password: 'password' }
await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
user1Token = await userLogin(servers[0], user)
user1Token = await servers[0].loginCommand.getAccessToken(user)
}
{
const user = { username: 'user2', password: 'password' }
await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
user2Token = await userLogin(servers[0], user)
user2Token = await servers[0].loginCommand.getAccessToken(user)
}
{
const user = { username: 'user3', password: 'password' }
await createUser({ url: servers[1].url, accessToken: servers[1].accessToken, username: user.username, password: user.password })
user3Token = await userLogin(servers[1], user)
user3Token = await servers[1].loginCommand.getAccessToken(user)
}
await doubleFollow(servers[0], servers[1])

View file

@ -15,7 +15,6 @@ import {
setAccessTokensToServers,
unblockUser,
uploadVideo,
userLogin,
verifyEmail
} from '../../../../shared/extra-utils'
import { MockSmtpServer } from '../../../../shared/extra-utils/mock-servers/mock-email'
@ -62,7 +61,7 @@ describe('Test emails', function () {
const res = await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
userId = res.body.user.id
userAccessToken = await userLogin(server, user)
userAccessToken = await server.loginCommand.getAccessToken(user)
}
{
@ -128,7 +127,7 @@ describe('Test emails', function () {
it('Should login with this new password', async function () {
user.password = 'super_password2'
await userLogin(server, user)
await server.loginCommand.getAccessToken(user)
})
})
@ -175,7 +174,7 @@ describe('Test emails', function () {
})
it('Should login with this new password', async function () {
await userLogin(server, {
await server.loginCommand.getAccessToken({
username: 'create_password',
password: 'newly_created_password'
})

View file

@ -15,8 +15,7 @@ import {
getVideoWithToken,
ServerInfo,
setAccessTokensToServers,
uploadVideo,
userLogin
uploadVideo
} from '../../../../shared/extra-utils'
const expect = chai.expect
@ -49,7 +48,7 @@ describe('Test follow constraints', function () {
password: 'super_password'
}
await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
userAccessToken = await userLogin(servers[0], user)
userAccessToken = await servers[0].loginCommand.getAccessToken(user)
await doubleFollow(servers[0], servers[1])
})

View file

@ -16,7 +16,6 @@ import {
setAccessTokensToServers,
testCaptionFile,
uploadVideo,
userLogin,
waitJobs
} from '@shared/extra-utils'
import { Video, VideoPrivacy } from '@shared/models'
@ -330,7 +329,7 @@ describe('Test follows', function () {
{
const user = { username: 'captain', password: 'password' }
await createUser({ url: servers[2].url, accessToken: servers[2].accessToken, username: user.username, password: user.password })
const userAccessToken = await userLogin(servers[2], user)
const userAccessToken = await servers[2].loginCommand.getAccessToken(user)
const resVideos = await getVideosList(servers[2].url)
video4 = resVideos.body.data.find(v => v.name === 'server3-4')

View file

@ -1,12 +1,17 @@
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
import 'mocha'
import * as chai from 'chai'
import { cleanupTests, getVideo, registerUser, uploadVideo, userLogin, viewVideo, wait } from '../../../../shared/extra-utils'
import { flushAndRunServer, setAccessTokensToServers } from '../../../../shared/extra-utils/index'
import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
const expect = chai.expect
import { expect } from 'chai'
import { HttpStatusCode } from '@shared/core-utils'
import {
cleanupTests,
flushAndRunServer,
getVideo,
registerUser,
setAccessTokensToServers,
uploadVideo,
viewVideo,
wait
} from '@shared/extra-utils'
describe('Test application behind a reverse proxy', function () {
let server = null
@ -97,10 +102,10 @@ describe('Test application behind a reverse proxy', function () {
const user = { username: 'root', password: 'fail' }
for (let i = 0; i < 19; i++) {
await userLogin(server, user, HttpStatusCode.BAD_REQUEST_400)
await server.loginCommand.getAccessToken(user, HttpStatusCode.BAD_REQUEST_400)
}
await userLogin(server, user, HttpStatusCode.TOO_MANY_REQUESTS_429)
await server.loginCommand.getAccessToken(user, HttpStatusCode.TOO_MANY_REQUESTS_429)
})
it('Should rate limit signup', async function () {

View file

@ -10,7 +10,6 @@ import {
ServerInfo,
setAccessTokensToServers,
uploadVideo,
userLogin,
viewVideo,
wait,
waitJobs
@ -119,7 +118,7 @@ describe('Test stats (excluding redundancy)', function () {
}
{
await userLogin(server, user)
await server.loginCommand.getAccessToken(user)
const data = await server.statsCommand.get()

View file

@ -13,7 +13,6 @@ import {
SubscriptionsCommand,
updateVideo,
uploadVideo,
userLogin,
waitJobs
} from '@shared/extra-utils'
@ -42,7 +41,7 @@ describe('Test users subscriptions', function () {
const user = { username: 'user' + server.serverNumber, password: 'password' }
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
const accessToken = await userLogin(server, user)
const accessToken = await server.loginCommand.getAccessToken(user)
users.push({ accessToken })
const videoName1 = 'video 1-' + server.serverNumber

View file

@ -19,7 +19,6 @@ import {
updateMyAvatar,
updateMyUser,
uploadVideo,
userLogin,
waitJobs
} from '@shared/extra-utils'
import { User } from '@shared/models'
@ -64,7 +63,7 @@ describe('Test users with multiple servers', function () {
password: user.password
})
userId = res.body.user.id
userAccessToken = await userLogin(servers[0], user)
userAccessToken = await servers[0].loginCommand.getAccessToken(user)
}
{

View file

@ -2,23 +2,21 @@
import 'mocha'
import * as chai from 'chai'
import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
import { HttpStatusCode } from '@shared/core-utils'
import {
cleanupTests,
flushAndRunServer,
getMyUserInformation,
getUserInformation,
login,
MockSmtpServer,
registerUser,
ServerInfo,
setAccessTokensToServers,
updateMyUser,
userLogin,
verifyEmail
} from '../../../../shared/extra-utils'
import { MockSmtpServer } from '../../../../shared/extra-utils/mock-servers/mock-email'
import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
import { User } from '../../../../shared/models/users'
verifyEmail,
waitJobs
} from '@shared/extra-utils'
import { User } from '@shared/models'
const expect = chai.expect
@ -91,15 +89,15 @@ describe('Test users account verification', function () {
})
it('Should not allow login for user with unverified email', async function () {
const resLogin = await login(server.url, server.client, user1, HttpStatusCode.BAD_REQUEST_400)
expect(resLogin.body.detail).to.contain('User email is not verified.')
const { detail } = await server.loginCommand.login({ user: user1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
expect(detail).to.contain('User email is not verified.')
})
it('Should verify the user via email and allow login', async function () {
await verifyEmail(server.url, userId, verificationString)
const res = await login(server.url, server.client, user1)
userAccessToken = res.body.access_token
const body = await server.loginCommand.login({ user: user1 })
userAccessToken = body.access_token
const resUserVerified = await getUserInformation(server.url, server.accessToken, userId)
expect(resUserVerified.body.emailVerified).to.be.true
@ -164,7 +162,7 @@ describe('Test users account verification', function () {
await waitJobs(server)
expect(emails).to.have.lengthOf(expectedEmailsLength)
const accessToken = await userLogin(server, user2)
const accessToken = await server.loginCommand.getAccessToken(user2)
const resMyUserInfo = await getMyUserInformation(server.url, accessToken)
expect(resMyUserInfo.body.emailVerified).to.be.null
@ -181,7 +179,7 @@ describe('Test users account verification', function () {
}
})
await userLogin(server, user2)
await server.loginCommand.getAccessToken(user2)
})
after(async function () {

View file

@ -18,11 +18,8 @@ import {
getUsersListPaginationAndSort,
getVideosList,
killallServers,
login,
logout,
makePutBodyRequest,
rateVideo,
refreshToken,
registerUserWithChannel,
removeUser,
removeVideo,
@ -35,7 +32,6 @@ import {
updateMyUser,
updateUser,
uploadVideo,
userLogin,
waitJobs
} from '@shared/extra-utils'
import { AbuseState, MyUser, OAuth2ErrorCode, User, UserAdminFlag, UserRole, Video, VideoPlaylistType } from '@shared/models'
@ -78,22 +74,22 @@ describe('Test users', function () {
it('Should not login with an invalid client id', async function () {
const client = { id: 'client', secret: server.client.secret }
const res = await login(server.url, client, server.user, HttpStatusCode.BAD_REQUEST_400)
const body = await server.loginCommand.login({ client, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
expect(res.body.code).to.equal(OAuth2ErrorCode.INVALID_CLIENT)
expect(res.body.error).to.contain('client is invalid')
expect(res.body.type.startsWith('https://')).to.be.true
expect(res.body.type).to.contain(OAuth2ErrorCode.INVALID_CLIENT)
expect(body.code).to.equal(OAuth2ErrorCode.INVALID_CLIENT)
expect(body.error).to.contain('client is invalid')
expect(body.type.startsWith('https://')).to.be.true
expect(body.type).to.contain(OAuth2ErrorCode.INVALID_CLIENT)
})
it('Should not login with an invalid client secret', async function () {
const client = { id: server.client.id, secret: 'coucou' }
const res = await login(server.url, client, server.user, HttpStatusCode.BAD_REQUEST_400)
const body = await server.loginCommand.login({ client, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
expect(res.body.code).to.equal(OAuth2ErrorCode.INVALID_CLIENT)
expect(res.body.error).to.contain('client is invalid')
expect(res.body.type.startsWith('https://')).to.be.true
expect(res.body.type).to.contain(OAuth2ErrorCode.INVALID_CLIENT)
expect(body.code).to.equal(OAuth2ErrorCode.INVALID_CLIENT)
expect(body.error).to.contain('client is invalid')
expect(body.type.startsWith('https://')).to.be.true
expect(body.type).to.contain(OAuth2ErrorCode.INVALID_CLIENT)
})
})
@ -101,22 +97,22 @@ describe('Test users', function () {
it('Should not login with an invalid username', async function () {
const user = { username: 'captain crochet', password: server.user.password }
const res = await login(server.url, server.client, user, HttpStatusCode.BAD_REQUEST_400)
const body = await server.loginCommand.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
expect(res.body.code).to.equal(OAuth2ErrorCode.INVALID_GRANT)
expect(res.body.error).to.contain('credentials are invalid')
expect(res.body.type.startsWith('https://')).to.be.true
expect(res.body.type).to.contain(OAuth2ErrorCode.INVALID_GRANT)
expect(body.code).to.equal(OAuth2ErrorCode.INVALID_GRANT)
expect(body.error).to.contain('credentials are invalid')
expect(body.type.startsWith('https://')).to.be.true
expect(body.type).to.contain(OAuth2ErrorCode.INVALID_GRANT)
})
it('Should not login with an invalid password', async function () {
const user = { username: server.user.username, password: 'mew_three' }
const res = await login(server.url, server.client, user, HttpStatusCode.BAD_REQUEST_400)
const body = await server.loginCommand.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
expect(res.body.code).to.equal(OAuth2ErrorCode.INVALID_GRANT)
expect(res.body.error).to.contain('credentials are invalid')
expect(res.body.type.startsWith('https://')).to.be.true
expect(res.body.type).to.contain(OAuth2ErrorCode.INVALID_GRANT)
expect(body.code).to.equal(OAuth2ErrorCode.INVALID_GRANT)
expect(body.error).to.contain('credentials are invalid')
expect(body.type.startsWith('https://')).to.be.true
expect(body.type).to.contain(OAuth2ErrorCode.INVALID_GRANT)
})
it('Should not be able to upload a video', async function () {
@ -139,20 +135,20 @@ describe('Test users', function () {
it('Should not be able to unfollow')
it('Should be able to login', async function () {
const res = await login(server.url, server.client, server.user, HttpStatusCode.OK_200)
const body = await server.loginCommand.login({ expectedStatus: HttpStatusCode.OK_200 })
accessToken = res.body.access_token
accessToken = body.access_token
})
it('Should be able to login with an insensitive username', async function () {
const user = { username: 'RoOt', password: server.user.password }
await login(server.url, server.client, user, HttpStatusCode.OK_200)
await server.loginCommand.login({ user, expectedStatus: HttpStatusCode.OK_200 })
const user2 = { username: 'rOoT', password: server.user.password }
await login(server.url, server.client, user2, HttpStatusCode.OK_200)
await server.loginCommand.login({ user: user2, expectedStatus: HttpStatusCode.OK_200 })
const user3 = { username: 'ROOt', password: server.user.password }
await login(server.url, server.client, user3, HttpStatusCode.OK_200)
await server.loginCommand.login({ user: user3, expectedStatus: HttpStatusCode.OK_200 })
})
})
@ -222,7 +218,7 @@ describe('Test users', function () {
describe('Logout', function () {
it('Should logout (revoke token)', async function () {
await logout(server.url, server.accessToken)
await server.loginCommand.logout({ token: server.accessToken })
})
it('Should not be able to get the user information', async function () {
@ -250,9 +246,9 @@ describe('Test users', function () {
})
it('Should be able to login again', async function () {
const res = await login(server.url, server.client, server.user)
server.accessToken = res.body.access_token
server.refreshToken = res.body.refresh_token
const body = await server.loginCommand.login()
server.accessToken = body.access_token
server.refreshToken = body.refresh_token
})
it('Should be able to get my user information again', async function () {
@ -268,11 +264,11 @@ describe('Test users', function () {
await killallServers([ server ])
await reRunServer(server)
await getMyUserInformation(server.url, server.accessToken, 401)
await getMyUserInformation(server.url, server.accessToken, HttpStatusCode.UNAUTHORIZED_401)
})
it('Should not be able to refresh an access token with an expired refresh token', async function () {
await refreshToken(server, server.refreshToken, 400)
await server.loginCommand.refreshToken({ refreshToken: server.refreshToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
})
it('Should refresh the token', async function () {
@ -284,7 +280,7 @@ describe('Test users', function () {
await killallServers([ server ])
await reRunServer(server)
const res = await refreshToken(server, server.refreshToken)
const res = await server.loginCommand.refreshToken({ refreshToken: server.refreshToken })
server.accessToken = res.body.access_token
server.refreshToken = res.body.refresh_token
})
@ -308,7 +304,7 @@ describe('Test users', function () {
})
it('Should be able to login with this user', async function () {
accessTokenUser = await userLogin(server, user)
accessTokenUser = await server.loginCommand.getAccessToken(user)
})
it('Should be able to get user information', async function () {
@ -562,6 +558,7 @@ describe('Test users', function () {
})
describe('Update my account', function () {
it('Should update my password', async function () {
await updateMyUser({
url: server.url,
@ -571,7 +568,7 @@ describe('Test users', function () {
})
user.password = 'new password'
await userLogin(server, user, HttpStatusCode.OK_200)
await server.loginCommand.login({ user })
})
it('Should be able to change the NSFW display attribute', async function () {
@ -781,7 +778,7 @@ describe('Test users', function () {
it('Should have removed the user token', async function () {
await getMyUserVideoQuotaUsed(server.url, accessTokenUser, HttpStatusCode.UNAUTHORIZED_401)
accessTokenUser = await userLogin(server, user)
accessTokenUser = await server.loginCommand.getAccessToken(user)
})
it('Should be able to update another user password', async function () {
@ -794,10 +791,10 @@ describe('Test users', function () {
await getMyUserVideoQuotaUsed(server.url, accessTokenUser, HttpStatusCode.UNAUTHORIZED_401)
await userLogin(server, user, HttpStatusCode.BAD_REQUEST_400)
await server.loginCommand.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
user.password = 'password updated'
accessTokenUser = await userLogin(server, user)
accessTokenUser = await server.loginCommand.getAccessToken(user)
})
})
@ -813,7 +810,7 @@ describe('Test users', function () {
})
it('Should not be able to login with this user', async function () {
await userLogin(server, user, HttpStatusCode.BAD_REQUEST_400)
await server.loginCommand.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
})
it('Should not have videos of this user', async function () {
@ -842,7 +839,7 @@ describe('Test users', function () {
password: 'my super password'
}
user15AccessToken = await userLogin(server, user15)
user15AccessToken = await server.loginCommand.getAccessToken(user15)
})
it('Should have the correct display name', async function () {
@ -897,13 +894,13 @@ describe('Test users', function () {
})
user16Id = resUser.body.user.id
user16AccessToken = await userLogin(server, user16)
user16AccessToken = await server.loginCommand.getAccessToken(user16)
await getMyUserInformation(server.url, user16AccessToken, HttpStatusCode.OK_200)
await blockUser(server.url, user16Id, server.accessToken)
await getMyUserInformation(server.url, user16AccessToken, HttpStatusCode.UNAUTHORIZED_401)
await userLogin(server, user16, HttpStatusCode.BAD_REQUEST_400)
await server.loginCommand.login({ user: user16, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
})
it('Should search user by banned status', async function () {
@ -930,7 +927,7 @@ describe('Test users', function () {
it('Should unblock a user', async function () {
await unblockUser(server.url, user16Id, server.accessToken)
user16AccessToken = await userLogin(server, user16)
user16AccessToken = await server.loginCommand.getAccessToken(user16)
await getMyUserInformation(server.url, user16AccessToken, HttpStatusCode.OK_200)
})
})
@ -952,7 +949,7 @@ describe('Test users', function () {
})
user17Id = resUser.body.user.id
user17AccessToken = await userLogin(server, user17)
user17AccessToken = await server.loginCommand.getAccessToken(user17)
const res = await getUserInformation(server.url, server.accessToken, user17Id, true)
const user: User = res.body

View file

@ -24,7 +24,6 @@ import {
testImage,
updateVideo,
uploadVideo,
userLogin,
viewVideo,
wait,
waitJobs,
@ -155,7 +154,7 @@ describe('Test multiple servers', function () {
password: 'super_password'
}
await createUser({ url: servers[1].url, accessToken: servers[1].accessToken, username: user.username, password: user.password })
const userAccessToken = await userLogin(servers[1], user)
const userAccessToken = await servers[1].loginCommand.getAccessToken(user)
const videoAttributes = {
name: 'my super name for server 2',

View file

@ -16,8 +16,7 @@ import {
ServerInfo,
setAccessTokensToServers,
setDefaultVideoChannel,
uploadVideo,
userLogin
uploadVideo
} from '../../../../shared/extra-utils'
import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
import { User } from '../../../../shared/models/users'
@ -82,8 +81,8 @@ describe('Test video change ownership - nominal', function () {
videoQuota: videoQuota
})
firstUserToken = await userLogin(servers[0], firstUser)
secondUserToken = await userLogin(servers[0], secondUser)
firstUserToken = await servers[0].loginCommand.getAccessToken(firstUser)
secondUserToken = await servers[0].loginCommand.getAccessToken(secondUser)
{
const res = await getMyUserInformation(servers[0].url, firstUserToken)
@ -323,8 +322,8 @@ describe('Test video change ownership - quota too small', function () {
videoQuota: limitedVideoQuota
})
firstUserToken = await userLogin(server, firstUser)
secondUserToken = await userLogin(server, secondUser)
firstUserToken = await server.loginCommand.getAccessToken(firstUser)
secondUserToken = await server.loginCommand.getAccessToken(secondUser)
// Upload some videos on the server
const video1Attributes = {

View file

@ -16,7 +16,6 @@ import {
testImage,
updateVideo,
uploadVideo,
userLogin,
wait
} from '../../../../shared/extra-utils'
import { getMyUserInformation, ServerInfo, setAccessTokensToServers, viewVideo } from '../../../../shared/extra-utils/index'
@ -391,7 +390,7 @@ describe('Test video channels', function () {
{
await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: 'toto', password: 'password' })
const accessToken = await userLogin(servers[0], { username: 'toto', password: 'password' })
const accessToken = await servers[0].loginCommand.getAccessToken({ username: 'toto', password: 'password' })
const res = await getMyUserInformation(servers[0].url, accessToken)
const videoChannel = res.body.videoChannels[0]

View file

@ -8,7 +8,6 @@ import {
createUser,
dateIsValid,
flushAndRunServer,
getAccessToken,
ServerInfo,
setAccessTokensToServers,
testImage,
@ -52,7 +51,7 @@ describe('Test video comments', function () {
username: 'user1',
password: 'password'
})
userAccessTokenServer1 = await getAccessToken(server.url, 'user1', 'password')
userAccessTokenServer1 = await server.loginCommand.getAccessToken('user1', 'password')
command = server.commentsCommand
})

View file

@ -15,8 +15,7 @@ import {
ServerInfo,
setAccessTokensToServers,
updateMyUser,
uploadVideo,
userLogin
uploadVideo
} from '@shared/extra-utils'
import { BooleanBothQuery, CustomConfig, ResultList, User, Video, VideosOverview } from '@shared/models'
@ -151,7 +150,7 @@ describe('Test video NSFW policy', function () {
const password = 'my super password'
await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
userAccessToken = await userLogin(server, { username, password })
userAccessToken = await server.loginCommand.getAccessToken({ username, password })
const res = await getMyUserInformation(server.url, userAccessToken)
const user = res.body

View file

@ -10,7 +10,6 @@ import {
doubleFollow,
flushAndRunMultipleServers,
generateUserAccessToken,
getAccessToken,
getMyUserInformation,
PlaylistsCommand,
removeUser,
@ -21,7 +20,6 @@ import {
updateVideo,
uploadVideo,
uploadVideoAndGetId,
userLogin,
wait,
waitJobs
} from '@shared/extra-utils'
@ -122,7 +120,7 @@ describe('Test video playlists', function () {
username: 'user1',
password: 'password'
})
userTokenServer1 = await getAccessToken(servers[0].url, 'user1', 'password')
userTokenServer1 = await servers[0].loginCommand.getAccessToken('user1', 'password')
}
await waitJobs(servers)
@ -1129,7 +1127,7 @@ describe('Test video playlists', function () {
})
const userId = res.body.user.id
const userAccessToken = await userLogin(servers[0], user)
const userAccessToken = await servers[0].loginCommand.getAccessToken(user)
const resChannel = await getMyUserInformation(servers[0].url, userAccessToken)
const userChannel = (resChannel.body as User).videoChannels[0]

View file

@ -2,23 +2,24 @@
import 'mocha'
import * as chai from 'chai'
import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
import { Video, VideoCreateResult } from '@shared/models'
import { HttpStatusCode } from '@shared/core-utils'
import {
cleanupTests,
createUser,
doubleFollow,
flushAndRunServer,
getMyVideos,
getVideo,
getVideosList,
getVideosListWithToken,
getVideoWithToken,
ServerInfo,
setAccessTokensToServers,
uploadVideo
} from '../../../../shared/extra-utils/index'
import { doubleFollow } from '../../../../shared/extra-utils/server/follows'
import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
import { userLogin } from '../../../../shared/extra-utils/users/login'
import { createUser } from '../../../../shared/extra-utils/users/users'
import { getMyVideos, getVideo, getVideoWithToken, updateVideo } from '../../../../shared/extra-utils/videos/videos'
import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum'
updateVideo,
uploadVideo,
waitJobs
} from '@shared/extra-utils'
import { Video, VideoCreateResult, VideoPrivacy } from '@shared/models'
const expect = chai.expect
@ -126,7 +127,7 @@ describe('Test video privacy', function () {
}
await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
anotherUserToken = await userLogin(servers[0], user)
anotherUserToken = await servers[0].loginCommand.getAccessToken(user)
await getVideoWithToken(servers[0].url, anotherUserToken, privateVideoUUID, HttpStatusCode.FORBIDDEN_403)
})

View file

@ -1,7 +1,8 @@
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
import * as chai from 'chai'
import 'mocha'
import * as chai from 'chai'
import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
import {
cleanupTests,
createUser,
@ -10,12 +11,10 @@ import {
makeGetRequest,
ServerInfo,
setAccessTokensToServers,
uploadVideo,
userLogin
uploadVideo
} from '../../../../shared/extra-utils'
import { Video, VideoPrivacy } from '../../../../shared/models/videos'
import { UserRole } from '../../../../shared/models/users'
import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
import { Video, VideoPrivacy } from '../../../../shared/models/videos'
const expect = chai.expect
@ -72,7 +71,7 @@ describe('Test videos filter', function () {
role: UserRole.MODERATOR
}
)
server['moderatorAccessToken'] = await userLogin(server, moderator)
server['moderatorAccessToken'] = await server.loginCommand.getAccessToken(moderator)
await uploadVideo(server.url, server.accessToken, { name: 'public ' + server.serverNumber })

View file

@ -16,7 +16,6 @@ import {
setAccessTokensToServers,
updateMyUser,
uploadVideo,
userLogin,
wait
} from '@shared/extra-utils'
import { Video, VideoDetails } from '@shared/models'
@ -61,7 +60,7 @@ describe('Test videos history', function () {
password: 'super password'
}
await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
userAccessToken = await userLogin(server, user)
userAccessToken = await server.loginCommand.getAccessToken(user)
})
it('Should get videos, without watching history', async function () {

View file

@ -21,7 +21,6 @@ import {
setAccessTokensToServers,
testHelloWorldRegisteredSettings,
uploadVideoAndGetId,
userLogin,
waitJobs
} from '../../../shared/extra-utils'
@ -41,7 +40,7 @@ describe('Test CLI wrapper', function () {
await createUser({ url: server.url, accessToken: server.accessToken, username: 'user_1', password: 'super_password' })
userAccessToken = await userLogin(server, { username: 'user_1', password: 'super_password' })
userAccessToken = await server.loginCommand.getAccessToken({ username: 'user_1', password: 'super_password' })
{
const attributes = { name: 'user_channel', displayName: 'User channel', support: 'super support text' }

View file

@ -1,14 +1,5 @@
import 'mocha'
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
import {
cleanupTests,
CLICommand,
createUser,
flushAndRunServer,
login,
ServerInfo,
setAccessTokensToServers
} from '../../../shared/extra-utils'
import { cleanupTests, CLICommand, createUser, flushAndRunServer, ServerInfo, setAccessTokensToServers } from '../../../shared/extra-utils'
describe('Test reset password scripts', function () {
let server: ServerInfo
@ -27,7 +18,7 @@ describe('Test reset password scripts', function () {
const env = server.cliCommand.getEnv()
await CLICommand.exec(`echo coucou | ${env} npm run reset-password -- -u user_1`)
await login(server.url, server.client, { username: 'user_1', password: 'coucou' }, HttpStatusCode.OK_200)
await server.loginCommand.login({ user: { username: 'user_1', password: 'coucou' } })
})
after(async function () {

View file

@ -2,8 +2,9 @@
import 'mocha'
import { expect } from 'chai'
import { HttpStatusCode } from '@shared/core-utils'
import { User } from '@shared/models/users/user.model'
import { blockUser, getMyUserInformation, setAccessTokensToServers, unblockUser, uploadVideo, userLogin } from '../../../shared/extra-utils'
import { blockUser, getMyUserInformation, setAccessTokensToServers, unblockUser, uploadVideo } from '../../../shared/extra-utils'
import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../shared/extra-utils/server/servers'
describe('Official plugin auth-ldap', function () {
@ -21,7 +22,7 @@ describe('Official plugin auth-ldap', function () {
})
it('Should not login with without LDAP settings', async function () {
await userLogin(server, { username: 'fry', password: 'fry' }, 400)
await server.loginCommand.login({ user: { username: 'fry', password: 'fry' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
})
it('Should not login with bad LDAP settings', async function () {
@ -39,7 +40,7 @@ describe('Official plugin auth-ldap', function () {
}
})
await userLogin(server, { username: 'fry', password: 'fry' }, 400)
await server.loginCommand.login({ user: { username: 'fry', password: 'fry' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
})
it('Should not login with good LDAP settings but wrong username/password', async function () {
@ -57,16 +58,16 @@ describe('Official plugin auth-ldap', function () {
}
})
await userLogin(server, { username: 'fry', password: 'bad password' }, 400)
await userLogin(server, { username: 'fryr', password: 'fry' }, 400)
await server.loginCommand.login({ user: { username: 'fry', password: 'bad password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
await server.loginCommand.login({ user: { username: 'fryr', password: 'fry' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
})
it('Should login with the appropriate username/password', async function () {
accessToken = await userLogin(server, { username: 'fry', password: 'fry' })
accessToken = await server.loginCommand.getAccessToken({ username: 'fry', password: 'fry' })
})
it('Should login with the appropriate email/password', async function () {
accessToken = await userLogin(server, { username: 'fry@planetexpress.com', password: 'fry' })
accessToken = await server.loginCommand.getAccessToken({ username: 'fry@planetexpress.com', password: 'fry' })
})
it('Should login get my profile', async function () {
@ -86,19 +87,25 @@ describe('Official plugin auth-ldap', function () {
it('Should not be able to login if the user is banned', async function () {
await blockUser(server.url, userId, server.accessToken)
await userLogin(server, { username: 'fry@planetexpress.com', password: 'fry' }, 400)
await server.loginCommand.login({
user: { username: 'fry@planetexpress.com', password: 'fry' },
expectedStatus: HttpStatusCode.BAD_REQUEST_400
})
})
it('Should be able to login if the user is unbanned', async function () {
await unblockUser(server.url, userId, server.accessToken)
await userLogin(server, { username: 'fry@planetexpress.com', password: 'fry' })
await server.loginCommand.login({ user: { username: 'fry@planetexpress.com', password: 'fry' } })
})
it('Should not login if the plugin is uninstalled', async function () {
await server.pluginsCommand.uninstall({ npmName: 'peertube-plugin-auth-ldap' })
await userLogin(server, { username: 'fry@planetexpress.com', password: 'fry' }, 400)
await server.loginCommand.login({
user: { username: 'fry@planetexpress.com', password: 'fry' },
expectedStatus: HttpStatusCode.BAD_REQUEST_400
})
})
after(async function () {

View file

@ -17,7 +17,6 @@ import {
setAccessTokensToServers,
uploadVideo,
uploadVideoAndGetId,
userLogin,
waitJobs
} from '@shared/extra-utils'
import { User, VideoPrivacy } from '@shared/models'
@ -64,7 +63,7 @@ describe('Test syndication feeds', () => {
{
const attr = { username: 'john', password: 'password' }
await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: attr.username, password: attr.password })
userAccessToken = await userLogin(servers[0], attr)
userAccessToken = await servers[0].loginCommand.getAccessToken(attr)
const res = await getMyUserInformation(servers[0].url, userAccessToken)
const user: User = res.body
@ -301,7 +300,7 @@ describe('Test syndication feeds', () => {
it('Should list no videos for a user with no videos and no subscriptions', async function () {
const attr = { username: 'feeduser', password: 'password' }
await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: attr.username, password: attr.password })
const feeduserAccessToken = await userLogin(servers[0], attr)
const feeduserAccessToken = await servers[0].loginCommand.getAccessToken(attr)
{
const res = await getMyUserInformation(servers[0].url, feeduserAccessToken)

View file

@ -14,7 +14,6 @@ import {
updateUser,
updateVideo,
uploadVideo,
userLogin,
viewVideo
} from '../../../shared/extra-utils'
import {
@ -138,7 +137,7 @@ describe('Test plugin action hooks', function () {
})
it('Should run action:api.user.oauth2-got-token', async function () {
await userLogin(servers[0], { username: 'created_user', password: 'super_password' })
await servers[0].loginCommand.getAccessToken('created_user', 'super_password')
await checkHook('action:api.user.oauth2-got-token')
})

View file

@ -9,14 +9,10 @@ import {
decodeQueryString,
flushAndRunServer,
getMyUserInformation,
loginUsingExternalToken,
logout,
PluginsCommand,
refreshToken,
ServerInfo,
setAccessTokensToServers,
updateMyUser,
userLogin,
wait
} from '@shared/extra-utils'
import { User, UserRole } from '@shared/models'
@ -43,12 +39,11 @@ async function loginExternal (options: {
const location = res.header.location
const { externalAuthToken } = decodeQueryString(location)
const resLogin = await loginUsingExternalToken(
options.server,
options.username,
externalAuthToken as string,
options.statusCodeExpectedStep2
)
const resLogin = await options.server.loginCommand.loginUsingExternalToken({
username: options.username,
externalAuthToken: externalAuthToken as string,
expectedStatus: options.statusCodeExpectedStep2
})
return resLogin.body
}
@ -110,13 +105,17 @@ describe('Test external auth plugins', function () {
})
it('Should reject auto external login with a missing or invalid token', async function () {
await loginUsingExternalToken(server, 'cyan', '', HttpStatusCode.BAD_REQUEST_400)
await loginUsingExternalToken(server, 'cyan', 'blabla', HttpStatusCode.BAD_REQUEST_400)
const command = server.loginCommand
await command.loginUsingExternalToken({ username: 'cyan', externalAuthToken: '', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
await command.loginUsingExternalToken({ username: 'cyan', externalAuthToken: 'blabla', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
})
it('Should reject auto external login with a missing or invalid username', async function () {
await loginUsingExternalToken(server, '', externalAuthToken, HttpStatusCode.BAD_REQUEST_400)
await loginUsingExternalToken(server, '', externalAuthToken, HttpStatusCode.BAD_REQUEST_400)
const command = server.loginCommand
await command.loginUsingExternalToken({ username: '', externalAuthToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
await command.loginUsingExternalToken({ username: '', externalAuthToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
})
it('Should reject auto external login with an expired token', async function () {
@ -124,7 +123,11 @@ describe('Test external auth plugins', function () {
await wait(5000)
await loginUsingExternalToken(server, 'cyan', externalAuthToken, HttpStatusCode.BAD_REQUEST_400)
await server.loginCommand.loginUsingExternalToken({
username: 'cyan',
externalAuthToken,
expectedStatus: HttpStatusCode.BAD_REQUEST_400
})
await server.serversCommand.waitUntilLog('expired external auth token', 2)
})
@ -182,7 +185,7 @@ describe('Test external auth plugins', function () {
it('Should refresh Cyan token, but not Kefka token', async function () {
{
const resRefresh = await refreshToken(server, cyanRefreshToken)
const resRefresh = await server.loginCommand.refreshToken({ refreshToken: cyanRefreshToken })
cyanAccessToken = resRefresh.body.access_token
cyanRefreshToken = resRefresh.body.refresh_token
@ -192,7 +195,7 @@ describe('Test external auth plugins', function () {
}
{
await refreshToken(server, kefkaRefreshToken, HttpStatusCode.BAD_REQUEST_400)
await server.loginCommand.refreshToken({ refreshToken: kefkaRefreshToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
}
})
@ -212,7 +215,7 @@ describe('Test external auth plugins', function () {
})
it('Should logout Cyan', async function () {
await logout(server.url, cyanAccessToken)
await server.loginCommand.logout({ token: cyanAccessToken })
})
it('Should have logged out Cyan', async function () {
@ -269,7 +272,7 @@ describe('Test external auth plugins', function () {
settings: { disableKefka: true }
})
await userLogin(server, { username: 'kefka', password: 'fake' }, HttpStatusCode.BAD_REQUEST_400)
await server.loginCommand.login({ user: { username: 'kefka', password: 'fake' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
await loginExternal({
server,
@ -307,9 +310,9 @@ describe('Test external auth plugins', function () {
statusCodeExpected: HttpStatusCode.NOT_FOUND_404
})
await userLogin(server, { username: 'cyan', password: null }, HttpStatusCode.BAD_REQUEST_400)
await userLogin(server, { username: 'cyan', password: '' }, HttpStatusCode.BAD_REQUEST_400)
await userLogin(server, { username: 'cyan', password: 'fake' }, HttpStatusCode.BAD_REQUEST_400)
await server.loginCommand.login({ user: { username: 'cyan', password: null }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
await server.loginCommand.login({ user: { username: 'cyan', password: '' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
await server.loginCommand.login({ user: { username: 'cyan', password: 'fake' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
})
it('Should not login kefka with another plugin', async function () {
@ -369,9 +372,8 @@ describe('Test external auth plugins', function () {
username: 'cid'
})
const resLogout = await logout(server.url, resLogin.access_token)
expect(resLogout.body.redirectUrl).to.equal('https://example.com/redirectUrl')
const { redirectUrl } = await server.loginCommand.logout({ token: resLogin.access_token })
expect(redirectUrl).to.equal('https://example.com/redirectUrl')
})
it('Should call the plugin\'s onLogout method with the request', async function () {
@ -382,8 +384,7 @@ describe('Test external auth plugins', function () {
username: 'cid'
})
const resLogout = await logout(server.url, resLogin.access_token)
expect(resLogout.body.redirectUrl).to.equal('https://example.com/redirectUrl?access_token=' + resLogin.access_token)
const { redirectUrl } = await server.loginCommand.logout({ token: resLogin.access_token })
expect(redirectUrl).to.equal('https://example.com/redirectUrl?access_token=' + resLogin.access_token)
})
})

View file

@ -2,19 +2,16 @@
import 'mocha'
import { expect } from 'chai'
import { HttpStatusCode } from '@shared/core-utils'
import {
cleanupTests,
flushAndRunServer,
getMyUserInformation,
getUsersList,
login,
logout,
PluginsCommand,
refreshToken,
ServerInfo,
setAccessTokensToServers,
updateMyUser,
userLogin,
wait
} from '@shared/extra-utils'
import { User, UserRole } from '@shared/models'
@ -52,11 +49,11 @@ describe('Test id and pass auth plugins', function () {
})
it('Should not login', async function () {
await userLogin(server, { username: 'toto', password: 'password' }, 400)
await server.loginCommand.login({ user: { username: 'toto', password: 'password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
})
it('Should login Spyro, create the user and use the token', async function () {
const accessToken = await userLogin(server, { username: 'spyro', password: 'spyro password' })
const accessToken = await server.loginCommand.getAccessToken({ username: 'spyro', password: 'spyro password' })
const res = await getMyUserInformation(server.url, accessToken)
@ -68,9 +65,9 @@ describe('Test id and pass auth plugins', function () {
it('Should login Crash, create the user and use the token', async function () {
{
const res = await login(server.url, server.client, { username: 'crash', password: 'crash password' })
crashAccessToken = res.body.access_token
crashRefreshToken = res.body.refresh_token
const body = await server.loginCommand.login({ user: { username: 'crash', password: 'crash password' } })
crashAccessToken = body.access_token
crashRefreshToken = body.refresh_token
}
{
@ -85,9 +82,9 @@ describe('Test id and pass auth plugins', function () {
it('Should login the first Laguna, create the user and use the token', async function () {
{
const res = await login(server.url, server.client, { username: 'laguna', password: 'laguna password' })
lagunaAccessToken = res.body.access_token
lagunaRefreshToken = res.body.refresh_token
const body = await server.loginCommand.login({ user: { username: 'laguna', password: 'laguna password' } })
lagunaAccessToken = body.access_token
lagunaRefreshToken = body.refresh_token
}
{
@ -102,7 +99,7 @@ describe('Test id and pass auth plugins', function () {
it('Should refresh crash token, but not laguna token', async function () {
{
const resRefresh = await refreshToken(server, crashRefreshToken)
const resRefresh = await server.loginCommand.refreshToken({ refreshToken: crashRefreshToken })
crashAccessToken = resRefresh.body.access_token
crashRefreshToken = resRefresh.body.refresh_token
@ -112,7 +109,7 @@ describe('Test id and pass auth plugins', function () {
}
{
await refreshToken(server, lagunaRefreshToken, 400)
await server.loginCommand.refreshToken({ refreshToken: lagunaRefreshToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
}
})
@ -132,7 +129,7 @@ describe('Test id and pass auth plugins', function () {
})
it('Should logout Crash', async function () {
await logout(server.url, crashAccessToken)
await server.loginCommand.logout({ token: crashAccessToken })
})
it('Should have logged out Crash', async function () {
@ -142,7 +139,7 @@ describe('Test id and pass auth plugins', function () {
})
it('Should login Crash and keep the old existing profile', async function () {
crashAccessToken = await userLogin(server, { username: 'crash', password: 'crash password' })
crashAccessToken = await server.loginCommand.getAccessToken({ username: 'crash', password: 'crash password' })
const res = await getMyUserInformation(server.url, crashAccessToken)
@ -162,16 +159,18 @@ describe('Test id and pass auth plugins', function () {
})
it('Should reject an invalid username, email, role or display name', async function () {
await userLogin(server, { username: 'ward', password: 'ward password' }, 400)
const command = server.loginCommand
await command.login({ user: { username: 'ward', password: 'ward password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
await server.serversCommand.waitUntilLog('valid username')
await userLogin(server, { username: 'kiros', password: 'kiros password' }, 400)
await command.login({ user: { username: 'kiros', password: 'kiros password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
await server.serversCommand.waitUntilLog('valid display name')
await userLogin(server, { username: 'raine', password: 'raine password' }, 400)
await command.login({ user: { username: 'raine', password: 'raine password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
await server.serversCommand.waitUntilLog('valid role')
await userLogin(server, { username: 'ellone', password: 'elonne password' }, 400)
await command.login({ user: { username: 'ellone', password: 'elonne password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
await server.serversCommand.waitUntilLog('valid email')
})
@ -181,8 +180,9 @@ describe('Test id and pass auth plugins', function () {
settings: { disableSpyro: true }
})
await userLogin(server, { username: 'spyro', password: 'spyro password' }, 400)
await userLogin(server, { username: 'spyro', password: 'fake' }, 400)
const command = server.loginCommand
await command.login({ user: { username: 'spyro', password: 'spyro password' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
await command.login({ user: { username: 'spyro', password: 'fake' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
})
it('Should have disabled this auth', async function () {
@ -198,7 +198,10 @@ describe('Test id and pass auth plugins', function () {
it('Should uninstall the plugin one and do not login existing Crash', async function () {
await server.pluginsCommand.uninstall({ npmName: 'peertube-plugin-test-id-pass-auth-one' })
await userLogin(server, { username: 'crash', password: 'crash password' }, 400)
await server.loginCommand.login({
user: { username: 'crash', password: 'crash password' },
expectedStatus: HttpStatusCode.BAD_REQUEST_400
})
})
it('Should display the correct configuration', async function () {

View file

@ -27,6 +27,7 @@ function makeGetRequest (options: {
range?: string
redirects?: number
accept?: string
host?: string
}) {
if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
if (options.contentType === undefined) options.contentType = 'application/json'
@ -38,6 +39,7 @@ function makeGetRequest (options: {
if (options.query) req.query(options.query)
if (options.range) req.set('Range', options.range)
if (options.accept) req.set('Accept', options.accept)
if (options.host) req.set('Host', options.host)
if (options.redirects) req.redirects(options.redirects)
return req.expect(options.statusCodeExpected)
@ -113,6 +115,7 @@ function makePostBodyRequest (options: {
path: string
token?: string
fields?: { [ fieldName: string ]: any }
type?: string
statusCodeExpected?: HttpStatusCode
}) {
if (!options.fields) options.fields = {}
@ -123,6 +126,7 @@ function makePostBodyRequest (options: {
.set('Accept', 'application/json')
if (options.token) req.set('Authorization', 'Bearer ' + options.token)
if (options.type) req.type(options.type)
return req.send(options.fields)
.expect(options.statusCodeExpected)

View file

@ -1,20 +0,0 @@
import * as request from 'supertest'
import { URL } from 'url'
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
function getClient (url: string) {
const path = '/api/v1/oauth-clients/local'
return request(url)
.get(path)
.set('Host', new URL(url).host)
.set('Accept', 'application/json')
.expect(HttpStatusCode.OK_200)
.expect('Content-Type', /json/)
}
// ---------------------------------------------------------------------------
export {
getClient
}

View file

@ -16,7 +16,7 @@ import { AbusesCommand } from '../moderation'
import { OverviewsCommand } from '../overviews'
import { SearchCommand } from '../search'
import { SocketIOCommand } from '../socket'
import { AccountsCommand, BlocklistCommand, NotificationsCommand, SubscriptionsCommand } from '../users'
import { AccountsCommand, BlocklistCommand, LoginCommand, NotificationsCommand, SubscriptionsCommand } from '../users'
import {
BlacklistCommand,
CaptionsCommand,
@ -126,6 +126,7 @@ interface ServerInfo {
sqlCommand?: SQLCommand
notificationsCommand?: NotificationsCommand
serversCommand?: ServersCommand
loginCommand?: LoginCommand
}
function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) {
@ -357,6 +358,7 @@ function assignCommands (server: ServerInfo) {
server.sqlCommand = new SQLCommand(server)
server.notificationsCommand = new NotificationsCommand(server)
server.serversCommand = new ServersCommand(server)
server.loginCommand = new LoginCommand(server)
}
async function reRunServer (server: ServerInfo, configOverride?: any) {

View file

@ -33,6 +33,7 @@ interface InternalGetCommandOptions extends InternalCommonCommandOptions {
accept?: string
redirects?: number
range?: string
host?: string
}
abstract class AbstractCommand {
@ -78,7 +79,7 @@ abstract class AbstractCommand {
}
protected getRequest (options: InternalGetCommandOptions) {
const { redirects, query, contentType, accept, range } = options
const { redirects, query, contentType, accept, range, host } = options
return makeGetRequest({
...this.buildCommonRequestOptions(options),
@ -87,6 +88,7 @@ abstract class AbstractCommand {
query,
contentType,
range,
host,
accept
})
}
@ -109,13 +111,15 @@ abstract class AbstractCommand {
protected postBodyRequest (options: InternalCommonCommandOptions & {
fields?: { [ fieldName: string ]: any }
type?: string
}) {
const { fields } = options
const { type, fields } = options
return makePostBodyRequest({
...this.buildCommonRequestOptions(options),
fields
fields,
type
})
}

View file

@ -2,6 +2,7 @@ export * from './accounts-command'
export * from './accounts'
export * from './blocklist-command'
export * from './login'
export * from './login-command'
export * from './notifications'
export * from './notifications-command'
export * from './subscriptions-command'

View file

@ -0,0 +1,134 @@
import { PeerTubeRequestError } from '@server/helpers/requests'
import { HttpStatusCode } from '@shared/core-utils'
import { PeerTubeProblemDocument } from '@shared/models'
import { unwrapBody } from '../requests'
import { AbstractCommand, OverrideCommandOptions } from '../shared'
export class LoginCommand extends AbstractCommand {
login (options: OverrideCommandOptions & {
client?: { id?: string, secret?: string }
user?: { username: string, password: string }
} = {}) {
const { client = this.server.client, user = this.server.user } = options
const path = '/api/v1/users/token'
const body = {
client_id: client.id,
client_secret: client.secret,
username: user.username,
password: user.password,
response_type: 'code',
grant_type: 'password',
scope: 'upload'
}
return unwrapBody<{ access_token: string, refresh_token: string } & PeerTubeProblemDocument>(this.postBodyRequest({
...options,
path,
type: 'form',
fields: body,
implicitToken: false,
defaultExpectedStatus: HttpStatusCode.OK_200
}))
}
getAccessToken (user?: { username: string, password: string }): Promise<string>
getAccessToken (username: string, password: string): Promise<string>
async getAccessToken (arg1?: { username: string, password: string } | string, password?: string) {
let user: { username: string, password: string }
if (!arg1) user = this.server.user
else if (typeof arg1 === 'object') user = arg1
else user = { username: arg1, password }
try {
const body = await this.login({ user })
return body.access_token
} catch (err) {
throw new Error('Cannot authenticate. Please check your username/password.')
}
}
loginUsingExternalToken (options: OverrideCommandOptions & {
username: string
externalAuthToken: string
}) {
const { username, externalAuthToken } = options
const path = '/api/v1/users/token'
const body = {
client_id: this.server.client.id,
client_secret: this.server.client.secret,
username: username,
response_type: 'code',
grant_type: 'password',
scope: 'upload',
externalAuthToken
}
return this.postBodyRequest({
...options,
path,
type: 'form',
fields: body,
implicitToken: false,
defaultExpectedStatus: HttpStatusCode.OK_200
})
}
logout (options: OverrideCommandOptions & {
token: string
}) {
const path = '/api/v1/users/revoke-token'
return unwrapBody<{ redirectUrl: string }>(this.postBodyRequest({
...options,
path,
type: 'form',
implicitToken: false,
defaultExpectedStatus: HttpStatusCode.OK_200
}))
}
refreshToken (options: OverrideCommandOptions & {
refreshToken: string
}) {
const path = '/api/v1/users/token'
const body = {
client_id: this.server.client.id,
client_secret: this.server.client.secret,
refresh_token: options.refreshToken,
response_type: 'code',
grant_type: 'refresh_token'
}
return this.postBodyRequest({
...options,
path,
type: 'form',
fields: body,
implicitToken: false,
defaultExpectedStatus: HttpStatusCode.OK_200
})
}
getClient (options: OverrideCommandOptions = {}) {
const path = '/api/v1/oauth-clients/local'
return this.getRequestBody<{ client_id: string, client_secret: string }>({
...options,
path,
host: this.server.host,
implicitToken: false,
defaultExpectedStatus: HttpStatusCode.OK_200
})
}
}

View file

@ -1,133 +1,19 @@
import * as request from 'supertest'
import { ServerInfo } from '../server/servers'
import { getClient } from '../server/clients'
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
type Client = { id?: string, secret?: string }
type User = { username: string, password: string }
type Server = { url?: string, client?: Client, user?: User }
function login (url: string, client: Client, user: User, expectedStatus = HttpStatusCode.OK_200) {
const path = '/api/v1/users/token'
const body = {
client_id: client.id,
client_secret: client.secret,
username: user.username,
password: user.password,
response_type: 'code',
grant_type: 'password',
scope: 'upload'
}
return request(url)
.post(path)
.type('form')
.send(body)
.expect(expectedStatus)
}
function logout (url: string, token: string, expectedStatus = HttpStatusCode.OK_200) {
const path = '/api/v1/users/revoke-token'
return request(url)
.post(path)
.set('Authorization', 'Bearer ' + token)
.type('form')
.expect(expectedStatus)
}
async function serverLogin (server: Server) {
const res = await login(server.url, server.client, server.user, HttpStatusCode.OK_200)
return res.body.access_token as string
}
function refreshToken (server: ServerInfo, refreshToken: string, expectedStatus = HttpStatusCode.OK_200) {
const path = '/api/v1/users/token'
const body = {
client_id: server.client.id,
client_secret: server.client.secret,
refresh_token: refreshToken,
response_type: 'code',
grant_type: 'refresh_token'
}
return request(server.url)
.post(path)
.type('form')
.send(body)
.expect(expectedStatus)
}
async function userLogin (server: Server, user: User, expectedStatus = HttpStatusCode.OK_200) {
const res = await login(server.url, server.client, user, expectedStatus)
return res.body.access_token as string
}
async function getAccessToken (url: string, username: string, password: string) {
const resClient = await getClient(url)
const client = {
id: resClient.body.client_id,
secret: resClient.body.client_secret
}
const user = { username, password }
try {
const res = await login(url, client, user)
return res.body.access_token
} catch (err) {
throw new Error('Cannot authenticate. Please check your username/password.')
}
}
function setAccessTokensToServers (servers: ServerInfo[]) {
const tasks: Promise<any>[] = []
for (const server of servers) {
const p = serverLogin(server).then(t => { server.accessToken = t })
const p = server.loginCommand.getAccessToken()
.then(t => { server.accessToken = t })
tasks.push(p)
}
return Promise.all(tasks)
}
function loginUsingExternalToken (server: Server, username: string, externalAuthToken: string, expectedStatus = HttpStatusCode.OK_200) {
const path = '/api/v1/users/token'
const body = {
client_id: server.client.id,
client_secret: server.client.secret,
username: username,
response_type: 'code',
grant_type: 'password',
scope: 'upload',
externalAuthToken
}
return request(server.url)
.post(path)
.type('form')
.send(body)
.expect(expectedStatus)
}
// ---------------------------------------------------------------------------
export {
login,
logout,
serverLogin,
refreshToken,
userLogin,
getAccessToken,
setAccessTokensToServers,
Server,
Client,
User,
loginUsingExternalToken
setAccessTokensToServers
}

View file

@ -7,7 +7,7 @@ import { UserNotification, UserNotificationSetting, UserNotificationSettingValue
import { MockSmtpServer } from '../mock-servers/mock-email'
import { doubleFollow } from '../server/follows'
import { flushAndRunMultipleServers, ServerInfo } from '../server/servers'
import { setAccessTokensToServers, userLogin } from './login'
import { setAccessTokensToServers } from './login'
import { createUser, getMyUserInformation } from './users'
function getAllNotificationsSettings (): UserNotificationSetting {
@ -662,7 +662,7 @@ async function prepareNotificationsTest (serversCount = 3, overrideConfigArg: an
password: user.password,
videoQuota: 10 * 1000 * 1000
})
const userAccessToken = await userLogin(servers[0], user)
const userAccessToken = await servers[0].loginCommand.getAccessToken(user)
await servers[0].notificationsCommand.updateMySettings({ token: userAccessToken, settings: getAllNotificationsSettings() })
await servers[0].notificationsCommand.updateMySettings({ settings: getAllNotificationsSettings() })

View file

@ -7,7 +7,6 @@ import { UserRegister } from '../../models/users/user-register.model'
import { UserRole } from '../../models/users/user-role'
import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, updateImageRequest } from '../requests/requests'
import { ServerInfo } from '../server/servers'
import { userLogin } from './login'
function createUser (parameters: {
url: string
@ -55,7 +54,7 @@ async function generateUser (server: ServerInfo, username: string) {
const password = 'my super password'
const resCreate = await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
const token = await userLogin(server, { username, password })
const token = await server.loginCommand.getAccessToken({ username, password })
const resMe = await getMyUserInformation(server.url, token)
@ -70,7 +69,7 @@ async function generateUserAccessToken (server: ServerInfo, username: string) {
const password = 'my super password'
await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
return userLogin(server, { username, password })
return server.loginCommand.getAccessToken({ username, password })
}
function registerUser (url: string, username: string, password: string, specialStatus = HttpStatusCode.NO_CONTENT_204) {