]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/extra-utils/users/login.ts
Add overview of a user's actions in user-edit (#2558)
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / users / login.ts
index ddeb9df2adedc8f925c2ab141de4c13869d8b551..4fe54a74a5f86db964b8e45b9eed532208b063db 100644 (file)
@@ -1,6 +1,7 @@
 import * as request from 'supertest'
 
 import { ServerInfo } from '../server/servers'
+import { getClient } from '../server/clients'
 
 type Client = { id: string, secret: string }
 type User = { username: string, password: string }
@@ -38,11 +39,28 @@ async function userLogin (server: Server, user: User, expectedStatus = 200) {
   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 = serverLogin(server).then(t => { server.accessToken = t })
     tasks.push(p)
   }
 
@@ -55,6 +73,7 @@ export {
   login,
   serverLogin,
   userLogin,
+  getAccessToken,
   setAccessTokensToServers,
   Server,
   Client,