Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
lecture-devops-app
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
s80984
lecture-devops-app
Commits
3cc2de4e
Commit
3cc2de4e
authored
3 years ago
by
Daniel Fott
Committed by
Lucendio
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Create tests for the authentication middleware
parent
573fac18
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
app/server/src/middlewares/auth.test.js
+59
-0
59 additions, 0 deletions
app/server/src/middlewares/auth.test.js
with
59 additions
and
0 deletions
app/server/src/middlewares/auth.test.js
0 → 100644
+
59
−
0
View file @
3cc2de4e
const
auth
=
require
(
'
../middlewares/auth
'
);
const
jwt
=
require
(
'
jsonwebtoken
'
);
const
{
model
:
Users
}
=
require
(
'
../models/Users
'
);
describe
(
'
Testing the authorization middleware
'
,
()
=>
{
let
mockRequest
;
let
mockResponse
;
let
nextFunction
=
jest
.
fn
();
const
token
=
'
mytoken
'
;
beforeEach
(()
=>
{
mockRequest
=
{
cookies
:
{
'
todo-jt
'
:
token
,
}
};
mockResponse
=
{
redirect
:
jest
.
fn
(),
status
:
jest
.
fn
(()
=>
{
return
{
send
:
jest
.
fn
()
}
})
};
});
test
(
'
should redirect, if the value of the todo-jt cookie is an empty string
'
,
async
()
=>
{
mockRequest
.
cookies
[
'
todo-jt
'
]
=
''
;
auth
(
mockRequest
,
mockResponse
,
nextFunction
);
expect
(
mockResponse
.
redirect
).
toHaveBeenCalledWith
(
401
,
'
/login
'
);
});
test
(
'
should save the found user and token in the request
'
,
async
()
=>
{
const
mockUser
=
{
username
:
'
testname
'
,
password
:
'
testpw
'
};
jest
.
spyOn
(
jwt
,
'
verify
'
).
mockReturnValue
(
token
);
jest
.
spyOn
(
Users
,
'
findOne
'
).
mockResolvedValue
(
mockUser
);
await
auth
(
mockRequest
,
mockResponse
,
nextFunction
);
expect
(
mockRequest
.
token
).
toEqual
(
token
)
expect
(
mockRequest
.
user
).
toEqual
(
mockUser
);
});
test
(
'
should sent a 401 status code, if the user could not be found
'
,
async
()
=>
{
spyOn
(
jwt
,
'
verify
'
).
mockReturnValue
(
token
);
spyOn
(
Users
,
'
findOne
'
).
mockResolvedValue
(
undefined
);
await
auth
(
mockRequest
,
mockResponse
,
nextFunction
);
expect
(
mockRequest
.
user
).
toBeUndefined
;
expect
(
mockResponse
.
status
).
toHaveBeenCalledWith
(
401
);
});
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment