fix: auth robot test
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Controller, Request, Post, Get, UseGuards, Res, Body } from '@nestjs/common';
|
||||
import { Controller, Request, Post, Get, UseGuards, Res, Body, Delete } from '@nestjs/common';
|
||||
import { AuthService } from './auth.service';
|
||||
import { JwtAuthGuard } from './jwt-auth.guard';
|
||||
import { LocalAuthGuard } from './local-auth.guard';
|
||||
@@ -40,4 +40,14 @@ export class AuthController {
|
||||
getProfile(@Request() req) {
|
||||
return req.user;
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiOkResponse({ description: 'Successfully deleted' })
|
||||
@ApiUnauthorizedResponse({ description: 'Invalid token' })
|
||||
@Delete('me')
|
||||
deleteSelf(@Request() req) {
|
||||
return this.usersService.deleteUser({"id": req.user.id})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ export class AuthService {
|
||||
) {}
|
||||
|
||||
async validateUser(username: string, password: string): Promise<PayloadInterface> {
|
||||
const user = await this.userService.user({username});
|
||||
const user = await this.userService.user({username});
|
||||
if (user && bcrypt.compareSync(password, user.password)) {
|
||||
return {
|
||||
username: user.username,
|
||||
|
||||
4
back/test/robot/.gitignore
vendored
Normal file
4
back/test/robot/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
log.html
|
||||
output.xml
|
||||
report.html
|
||||
env
|
||||
83
back/test/robot/auth/auth.robot
Normal file
83
back/test/robot/auth/auth.robot
Normal file
@@ -0,0 +1,83 @@
|
||||
*** Settings ***
|
||||
Documentation Tests of the /auth route.
|
||||
... Ensures that the user can authenticate on kyoo.
|
||||
Resource ../rest.resource
|
||||
|
||||
|
||||
*** Keywords ***
|
||||
Login
|
||||
[Documentation] Shortcut to login with the given username for future requests
|
||||
[Arguments] ${username}
|
||||
&{res}= POST /auth/login {"username": "${username}", "password": "password-${username}"}
|
||||
Output
|
||||
Integer response status 201
|
||||
String response body access_token
|
||||
Set Headers {"Authorization": "Bearer ${res.body.access_token}"}
|
||||
|
||||
Register
|
||||
[Documentation] Shortcut to register with the given username for future requests
|
||||
[Arguments] ${username}
|
||||
&{res}= POST
|
||||
... /auth/register
|
||||
... {"username": "${username}", "password": "password-${username}", "email": "${username}@chromacase.moe"}
|
||||
Output
|
||||
Integer response status 200
|
||||
|
||||
Logout
|
||||
[Documentation] Logout the current user, only the local client is affected.
|
||||
Set Headers {"Authorization": ""}
|
||||
|
||||
|
||||
*** Test Cases ***
|
||||
Me cant be accessed without an account
|
||||
Get /auth/me
|
||||
Output
|
||||
Integer response status 401
|
||||
|
||||
Bad Account
|
||||
[Documentation] Login fails if user does not exist
|
||||
POST /auth/login {"username": "i-don-t-exist", "password": "pass"}
|
||||
Output
|
||||
Integer response status 401
|
||||
|
||||
RegisterAndLogin
|
||||
[Documentation] Create a new user and login in it
|
||||
Register user-1
|
||||
Login user-1
|
||||
[Teardown] DELETE /auth/me
|
||||
|
||||
Register Duplicates
|
||||
[Documentation] If two users tries to register with the same username, it fails
|
||||
Register user-duplicate
|
||||
# We can't use the `Register` keyword because it assert for success
|
||||
POST /auth/register {"username": "user-duplicate", "password": "pass", "email": "mail@kyoo.moe"}
|
||||
Output
|
||||
Integer response status 400
|
||||
Login user-duplicate
|
||||
[Teardown] DELETE /auth/me
|
||||
|
||||
Delete Account
|
||||
[Documentation] Check if a user can delete it's account
|
||||
Register I-should-be-deleted
|
||||
Login I-should-be-deleted
|
||||
DELETE /auth/me
|
||||
Output
|
||||
Integer response status 200
|
||||
|
||||
Login
|
||||
[Documentation] Create a new user and login in it
|
||||
Register login-user
|
||||
Login login-user
|
||||
${res}= GET /auth/me
|
||||
Output
|
||||
Integer response status 200
|
||||
String response body username login-user
|
||||
|
||||
Logout
|
||||
Login login-user
|
||||
${me}= Get /auth/me
|
||||
Output
|
||||
Output ${me}
|
||||
Should Be Equal As Strings ${res["body"]} ${me["body"]}
|
||||
|
||||
[Teardown] DELETE /auth/me
|
||||
@@ -1,4 +1,4 @@
|
||||
*** Settings ***
|
||||
Documentation Common things to handle rest requests
|
||||
|
||||
Library REST http://localhost:3000/api
|
||||
Library REST http://localhost:3000
|
||||
|
||||
14
back/test/robot/users/users.robot
Normal file
14
back/test/robot/users/users.robot
Normal file
@@ -0,0 +1,14 @@
|
||||
*** Settings ***
|
||||
Documentation Tests of the /users route.
|
||||
... Ensures that the users CRUD works corectly.
|
||||
Resource ../rest.resource
|
||||
|
||||
|
||||
*** Keywords ***
|
||||
*** Test Cases ***
|
||||
Create a user
|
||||
[Documentation] Create a user
|
||||
POST /users {"username": "i-don-t-exist", "password": "pass", "email": "wow@gmail.com"}
|
||||
Output
|
||||
Integer response status 201
|
||||
[Teardown] DELETE /users/1
|
||||
@@ -1,6 +1,6 @@
|
||||
services:
|
||||
back:
|
||||
build: ./back
|
||||
build: ./back
|
||||
ports:
|
||||
- "3000:3000"
|
||||
depends_on:
|
||||
|
||||
Reference in New Issue
Block a user