Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tdt4242-base-APU
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
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
Tor Martin Frøberg Wang
tdt4242-base-APU
Commits
b5dab063
Commit
b5dab063
authored
4 years ago
by
Haakon Gunleiksrud
Browse files
Options
Downloads
Patches
Plain Diff
get statement coverage for the first class in permissions.py
parent
ec8d745f
No related branches found
Branches containing commit
No related tags found
3 merge requests
!15
Dev
,
!10
Dev
,
!9
Fr5 bb testing
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
backend/secfit/workouts/tests.py
+29
-27
29 additions, 27 deletions
backend/secfit/workouts/tests.py
with
29 additions
and
27 deletions
backend/secfit/workouts/tests.py
+
29
−
27
View file @
b5dab063
...
@@ -4,9 +4,10 @@ Tests for the workouts application.
...
@@ -4,9 +4,10 @@ Tests for the workouts application.
from
django.test
import
TestCase
from
django.test
import
TestCase
from
workouts.models
import
Workout
from
workouts.models
import
Workout
from
users.models
import
User
from
users.models
import
User
from
rest_framework.test
import
Requests
Client
from
rest_framework.test
import
API
Client
from
requests.auth
import
HTTPBasicAuth
from
requests.auth
import
HTTPBasicAuth
import
requests
import
requests
import
json
# Create your tests here.
# Create your tests here.
"""
"""
...
@@ -19,33 +20,34 @@ class IsOwnerTestCase(TestCase):
...
@@ -19,33 +20,34 @@ class IsOwnerTestCase(TestCase):
User
.
objects
.
create
(
id
=
"
2
"
,
username
=
"
Alice
"
,
password
=
"
supersecret
"
)
User
.
objects
.
create
(
id
=
"
2
"
,
username
=
"
Alice
"
,
password
=
"
supersecret
"
)
Workout
.
objects
.
create
(
id
=
"
1
"
,
name
=
"
workout
"
,
date
=
"
2021-02-23 14:00
"
,
owner_id
=
"
1
"
)
Workout
.
objects
.
create
(
id
=
"
1
"
,
name
=
"
workout
"
,
date
=
"
2021-02-23 14:00
"
,
owner_id
=
"
1
"
)
self
.
user_1
=
User
.
objects
.
get
(
id
=
"
1
"
)
self
.
user_2
=
User
.
objects
.
get
(
id
=
"
2
"
)
self
.
workout
=
Workout
.
objects
.
get
(
name
=
"
workout
"
)
self
.
client_1
=
APIClient
()
self
.
client_2
=
APIClient
()
def
test_has_object_permission
(
self
):
def
test_has_object_permission
(
self
):
user_1
=
User
.
objects
.
get
(
id
=
"
1
"
)
user_2
=
User
.
objects
.
get
(
id
=
"
2
"
)
self
.
client_1
.
login
(
username
=
"
Bill
"
,
password
=
"
secret
"
)
workout
=
Workout
.
objects
.
get
(
name
=
"
workout
"
)
self
.
client_2
.
login
(
username
=
"
Alice
"
,
password
=
"
supersecret
"
)
client_1
=
RequestsClient
()
self
.
client_1
.
force_authenticate
(
user
=
self
.
user_1
)
#client_1.auth = HTTPBasicAuth('user', 'pass')
self
.
client_2
.
force_authenticate
(
user
=
self
.
user_2
)
#print("client_1.headers:\n\n",client_1.headers,"\n\n")
#client_1.headers.update({'Cookie': 'access=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjE0MzI3ODA4LCJqdGkiOiI2N2JmNzQ5YjQ1ZWY0Njg1YTM4MTc1MDMwZTZiNTNiMSIsInVzZXJfaWQiOjJ9.I7hOEFPqmWSgCa7CMa5INmeUhAuerIpBD3ps4WBykZ0; refresh=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTYxNDQxMzkwOCwianRpIjoiN2E3ZjQ1ODFhOWQ3NGU4NmE5NWUzYzlhZWIwNTExMWYiLCJ1c2VyX2lkIjoyfQ.FCHh4rKkKr4AhnC_ZGk3dvn3EyJeu7-thhbevD4u9WM'})
#Disse må kanskje endres når vi setter de inn i CI.
#client_1.headers.update({'authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjE0MzI3ODA4LCJqdGkiOiI2N2JmNzQ5YjQ1ZWY0Njg1YTM4MTc1MDMwZTZiNTNiMSIsInVzZXJfaWQiOjJ9.I7hOEFPqmWSgCa7CMa5INmeUhAuerIpBD3ps4WBykZ0'})
request_1
=
self
.
client_1
.
get
(
"
http://testserver/api/workouts/1/
"
)
#print("client_1.headers:\n\n",client_1.headers,"\n\n")
request_2
=
self
.
client_2
.
get
(
"
http://testserver/api/workouts/1/
"
)
#client2 = RequestsClient()
#Asserting that the owner of the workout (user 1) gets access and that others do not
#client2.login(username="Alice",password="supersecret")
#Disse må fort endres når vi setter de inn i CI.
request_1
=
client_1
.
get
(
"
http://testserver/api/workouts/1/
"
,
auth
=
(
'
Bill
'
,
'
secret
'
))
#request2 = client2.get("/api/workouts/1")¨
#print(request_1.__dict__)
#self.assertEqual(user_1.username,client1.)
print
(
"
request1.status_code:
"
,
request_1
.
status_code
)
print
(
"
request_1:
\n\n
"
,
request_1
.
__dict__
,
"
\n\n
"
)
self
.
assertTrue
(
request_1
.
status_code
==
200
)
self
.
assertTrue
(
request_1
.
status_code
==
200
)
#self.assertEqual(request_1.client, client_1)
self
.
assertTrue
(
request_2
.
status_code
==
403
)
self
.
assertTrue
(
workout
.
owner_id
==
user_1
.
id
)
self
.
assertFalse
(
workout
.
owner_id
==
user_2
.
id
)
#Formating the response data
response_data_1
=
json
.
loads
(
json
.
dumps
(
request_1
.
data
))
#Asserting that the owner of the fetched workout is user 1, which created the workout in the setup method.
self
.
assertEqual
(
response_data_1
[
"
owner
"
],
"
http://testserver/api/users/
"
+
str
(
self
.
user_1
.
id
)
+
"
/
"
)
self
.
assertNotEqual
(
response_data_1
[
"
owner
"
],
"
http://testserver/api/users/
"
+
str
(
self
.
user_2
.
id
)
+
"
/
"
)
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