1
1
import { expect } from "chai" ;
2
- import { authorization } from "../../middlewares/authorizeUsersAndService" ;
2
+ import { authorizeAndAuthenticate } from "../../middlewares/authorizeUsersAndService" ;
3
3
import bot from "../utils/generateBotToken" ;
4
4
const userData = require ( "../fixtures/user/user" ) ( ) ;
5
5
import authService from "../../services/authService" ;
@@ -41,46 +41,46 @@ describe("Middleware | Authorization", function () {
41
41
} ) ;
42
42
describe ( "Input validations" , function ( ) {
43
43
it ( "should throw an error for invalid roles" , function ( ) {
44
- expect ( ( ) => authorization ( [ "invalid_role" ] , [ Services . CRON_JOB_HANDLER ] ) ) . to . throw ( "Invalid role" ) ;
44
+ expect ( ( ) => authorizeAndAuthenticate ( [ "invalid_role" ] , [ Services . CRON_JOB_HANDLER ] ) ) . to . throw ( "Invalid role" ) ;
45
45
} ) ;
46
46
47
47
it ( "should throw an error for invalid services" , function ( ) {
48
- expect ( ( ) => authorization ( [ ROLES . APPOWNER ] , [ "invalid_service" ] ) ) . to . throw ( "Invalid service name" ) ;
48
+ expect ( ( ) => authorizeAndAuthenticate ( [ ROLES . APPOWNER ] , [ "invalid_service" ] ) ) . to . throw ( "Invalid service name" ) ;
49
49
} ) ;
50
50
} ) ;
51
51
52
52
describe ( "Service Authorization" , function ( ) {
53
53
it ( "should return unauthorized for invalid authorization header format" , async function ( ) {
54
54
req . headers . authorization = "InvalidHeader" ;
55
55
56
- await authorization ( [ ROLES . APPOWNER ] , [ Services . CRON_JOB_HANDLER ] ) ( req , res , next ) ;
56
+ await authorizeAndAuthenticate ( [ ROLES . APPOWNER ] , [ Services . CRON_JOB_HANDLER ] ) ( req , res , next ) ;
57
57
expect ( res . boom . unauthorized . calledOnce ) . to . be . equal ( true ) ;
58
58
} ) ;
59
59
60
60
it ( "should return unauthorized for invalid JWT token" , async function ( ) {
61
61
req . headers . authorization = "Bearer invalid_token" ;
62
- await authorization ( [ ROLES . APPOWNER ] , [ Services . CRON_JOB_HANDLER ] ) ( req , res , next ) ;
62
+ await authorizeAndAuthenticate ( [ ROLES . APPOWNER ] , [ Services . CRON_JOB_HANDLER ] ) ( req , res , next ) ;
63
63
expect ( res . boom . unauthorized . calledOnce ) . to . be . equal ( true ) ;
64
64
} ) ;
65
65
66
66
it ( "should call verifyCronJob for valid cron job token" , async function ( ) {
67
67
const jwtToken = bot . generateCronJobToken ( { name : CRON_JOB_HANDLER } ) ;
68
68
req . headers . authorization = `Bearer ${ jwtToken } ` ;
69
- await authorization ( [ ROLES . APPOWNER ] , [ Services . CRON_JOB_HANDLER ] ) ( req , res , next ) ;
69
+ await authorizeAndAuthenticate ( [ ROLES . APPOWNER ] , [ Services . CRON_JOB_HANDLER ] ) ( req , res , next ) ;
70
70
expect ( next . calledOnce ) . to . be . equal ( true ) ;
71
71
} ) ;
72
72
73
73
it ( "should call verifyDiscordBot for valid Discord bot token" , async function ( ) {
74
74
const jwtToken = bot . generateToken ( { name : CLOUDFLARE_WORKER } ) ;
75
75
req . headers . authorization = `Bearer ${ jwtToken } ` ;
76
- await authorization ( [ ROLES . APPOWNER ] , [ Services . CLOUDFLARE_WORKER ] ) ( req , res , next ) ;
76
+ await authorizeAndAuthenticate ( [ ROLES . APPOWNER ] , [ Services . CLOUDFLARE_WORKER ] ) ( req , res , next ) ;
77
77
expect ( next . calledOnce ) . to . be . equal ( true ) ;
78
78
} ) ;
79
79
80
80
it ( "should return unauthorized for unknown service names" , async function ( ) {
81
81
const jwtToken = bot . generateToken ( { name : "Invalid name" } ) ;
82
82
req . headers . authorization = `Bearer ${ jwtToken } ` ;
83
- await authorization ( [ ROLES . APPOWNER ] , [ Services . CLOUDFLARE_WORKER ] ) ( req , res , next ) ;
83
+ await authorizeAndAuthenticate ( [ ROLES . APPOWNER ] , [ Services . CLOUDFLARE_WORKER ] ) ( req , res , next ) ;
84
84
expect ( res . boom . unauthorized . calledOnce ) . to . be . equal ( true ) ;
85
85
} ) ;
86
86
} ) ;
@@ -89,7 +89,7 @@ describe("Middleware | Authorization", function () {
89
89
return res . json ( { message : "pong" } ) ;
90
90
} ;
91
91
92
- router . get ( "/for-super-user" , authorization ( [ ROLES . SUPERUSER ] , [ Services . CRON_JOB_HANDLER ] ) , pongHandler ) ;
92
+ router . get ( "/for-super-user" , authorizeAndAuthenticate ( [ ROLES . SUPERUSER ] , [ Services . CRON_JOB_HANDLER ] ) , pongHandler ) ;
93
93
94
94
const app = express ( ) ;
95
95
AppMiddlewares ( app ) ;
0 commit comments