Commit 3780067a authored by Christopher Løkken's avatar Christopher Løkken
Browse files

Pylinting of workout views

parent 68bbc75b
"""Contains views for the workouts application. These are mostly class-based views.
"""
import base64
import pickle
from collections import namedtuple
from rest_framework import generics, mixins
from rest_framework import permissions
......@@ -7,8 +10,11 @@ from rest_framework.parsers import (
JSONParser,
)
from rest_framework.response import Response
from django.db.models import Q
from rest_framework import filters
from rest_framework_simplejwt.tokens import RefreshToken
from django.db.models import Q
from django.core.exceptions import PermissionDenied
from django.core.signing import Signer
from workouts.parsers import MultipartJsonParser
from workouts.permissions import (
IsOwner,
......@@ -24,36 +30,28 @@ from workouts.models import Workout, Exercise, ExerciseInstance, WorkoutFile
from workouts.serializers import WorkoutSerializer, ExerciseSerializer
from workouts.serializers import RememberMeSerializer
from workouts.serializers import ExerciseInstanceSerializer, WorkoutFileSerializer
from django.core.exceptions import PermissionDenied
from rest_framework_simplejwt.tokens import RefreshToken
from rest_framework.response import Response
from collections import namedtuple
import base64
import pickle
from django.core.signing import Signer
# Allow users to save a persistent session in their browser
class RememberMe(
mixins.ListModelMixin,
mixins.CreateModelMixin,
mixins.DestroyModelMixin,
generics.GenericAPIView,
):
mixins.ListModelMixin,
mixins.CreateModelMixin,
mixins.DestroyModelMixin,
generics.GenericAPIView,
):
serializer_class = RememberMeSerializer
def get(self, request):
if request.user.is_authenticated == False:
if not request.user.is_authenticated:
raise PermissionDenied
else:
return Response({"remember_me": self.rememberme()})
def post(self, request):
cookieObject = namedtuple("Cookies", request.COOKIES.keys())(
cookie_object = namedtuple("Cookies", request.COOKIES.keys())(
*request.COOKIES.values()
)
user = self.get_user(cookieObject)
user = self.get_user(cookie_object)
refresh = RefreshToken.for_user(user)
return Response(
{
......@@ -62,8 +60,8 @@ class RememberMe(
}
)
def get_user(self, cookieObject):
decode = base64.b64decode(cookieObject.remember_me)
def get_user(self, cookie_object):
decode = base64.b64decode(cookie_object.remember_me)
user, sign = pickle.loads(decode)
# Validate signature
......@@ -81,8 +79,10 @@ class RememberMe(
class WorkoutList(
mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView
):
mixins.ListModelMixin,
mixins.CreateModelMixin,
generics.GenericAPIView
):
"""Class defining the web response for the creation of a Workout, or displaying a list
of Workouts
......@@ -125,11 +125,11 @@ class WorkoutList(
class WorkoutDetail(
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
generics.GenericAPIView,
):
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
generics.GenericAPIView,
):
"""Class defining the web response for the details of an individual Workout.
HTTP methods: GET, PUT, DELETE
......@@ -154,8 +154,10 @@ class WorkoutDetail(
class ExerciseList(
mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView
):
mixins.ListModelMixin,
mixins.CreateModelMixin,
generics.GenericAPIView
):
"""Class defining the web response for the creation of an Exercise, or
a list of Exercises.
......@@ -174,11 +176,11 @@ class ExerciseList(
class ExerciseDetail(
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
generics.GenericAPIView,
):
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
generics.GenericAPIView,
):
"""Class defining the web response for the details of an individual Exercise.
HTTP methods: GET, PUT, PATCH, DELETE
......@@ -202,11 +204,11 @@ class ExerciseDetail(
class ExerciseInstanceList(
mixins.ListModelMixin,
mixins.CreateModelMixin,
CreateListModelMixin,
generics.GenericAPIView,
):
mixins.ListModelMixin,
mixins.CreateModelMixin,
CreateListModelMixin,
generics.GenericAPIView,
):
"""Class defining the web response for the creation"""
serializer_class = ExerciseInstanceSerializer
......@@ -233,11 +235,11 @@ class ExerciseInstanceList(
class ExerciseInstanceDetail(
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
generics.GenericAPIView,
):
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
generics.GenericAPIView,
):
serializer_class = ExerciseInstanceSerializer
permission_classes = [
permissions.IsAuthenticated
......@@ -261,11 +263,11 @@ class ExerciseInstanceDetail(
class WorkoutFileList(
mixins.ListModelMixin,
mixins.CreateModelMixin,
CreateListModelMixin,
generics.GenericAPIView,
):
mixins.ListModelMixin,
mixins.CreateModelMixin,
CreateListModelMixin,
generics.GenericAPIView,
):
queryset = WorkoutFile.objects.all()
serializer_class = WorkoutFileSerializer
......@@ -297,11 +299,11 @@ class WorkoutFileList(
class WorkoutFileDetail(
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
generics.GenericAPIView,
):
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
generics.GenericAPIView,
):
queryset = WorkoutFile.objects.all()
serializer_class = WorkoutFileSerializer
......
Cypress.Commands.add('randomUser', () => {
const user = `user${Math.floor(Math.random() * 1000000)}`
cy.get('input[name="username"]').type(user),{force: true}
cy.get('input[name="username"]').type(user), { force: true }
})
Cypress.Commands.add('inputValueToField', (inputName, value)=>{
cy.get(`input[name=${inputName}]`).type(value,{force: true})
Cypress.Commands.add('inputValueToField', (inputName, value) => {
cy.get(`input[name=${inputName}]`).type(value, { force: true })
})
const inputName = [
......@@ -28,9 +28,9 @@ const inputDict = {
"street_address": ["hoyskoleringen", "123456"]
}
Cypress.Commands.add('runTest',(a,b,c,d,e,f,g,h, status)=>{
Cypress.Commands.add('runTest', (a, b, c, d, e, f, g, h, status) => {
cy.visit('/register.html')
if(!a){
if (!a) {
cy.randomUser()
} else {
cy.inputValueToField(inputName[0], " ")
......@@ -42,115 +42,90 @@ Cypress.Commands.add('runTest',(a,b,c,d,e,f,g,h, status)=>{
cy.inputValueToField(inputName[5], inputDict[inputName[5]][f])
cy.inputValueToField(inputName[6], inputDict[inputName[6]][g])
cy.inputValueToField(inputName[7], inputDict[inputName[7]][h])
cy.intercept({method: 'POST', url:'/api/users/'}).as('registerRequest')
cy.intercept({ method: 'POST', url: '/api/users/' }).as('registerRequest')
cy.get('#btn-create-account').click()
cy.wait('@registerRequest').its('response.statusCode').should('eq', status)
cy.visit('/logout.html')
})
describe('Two way domain tests', () => {
<<<<<<< HEAD
before(()=>{
cy.visit('/index.html')
})
it('all valid variables', () => {
cy.runTest(0,0,0,0,0,0,0,0,201)
})
it('valid username, invalid peer variable', () => {
cy.runTest(0,1,0,0,0,0,0,0,400)
cy.runTest(0,0,1,0,0,0,0,0,400)
cy.runTest(0,0,0,1,0,0,0,0,400)
cy.runTest(0,0,0,0,1,0,0,0,201)
cy.runTest(0,0,0,0,0,1,0,0,201)
cy.runTest(0,0,0,0,0,0,1,0,201)
cy.runTest(0,0,0,0,0,0,0,1,201)
})
it('invalid username, invalid peer variable', () => {
cy.runTest(1,1,0,0,0,0,0,0,400)
cy.runTest(1,0,1,0,0,0,0,0,400)
cy.runTest(1,0,0,1,0,0,0,0,400)
cy.runTest(1,0,0,0,1,0,0,0,400)
cy.runTest(1,0,0,0,0,1,0,0,400)
cy.runTest(1,0,0,0,0,0,1,0,400)
cy.runTest(1,0,0,0,0,0,0,1,400)
})
it('valid email, invalid peer variable', () => {
cy.runTest(1,0,0,0,0,0,0,0,400)
// cy.runTest(0,0,1,0,0,0,0,0,400) done allready
// cy.runTest(0,0,0,1,0,0,0,0,400) done allready
// cy.runTest(0,0,0,0,1,0,0,0,201) done allready
// cy.runTest(0,0,0,0,0,1,0,0,201) done allready
// cy.runTest(0,0,0,0,0,0,1,0,201) done allready
// cy.runTest(0,0,0,0,0,0,0,1,201) done allready
})
it('invalid email, invalid peer variable', () => {
//cy.runTest(1,1,0,0,0,0,0,0,400) done allready
cy.runTest(0,1,1,0,0,0,0,0,400)
cy.runTest(0,1,0,1,0,0,0,0,400)
cy.runTest(0,1,0,0,1,0,0,0,400)
cy.runTest(0,1,0,0,0,1,0,0,400)
cy.runTest(0,1,0,0,0,0,1,0,400)
cy.runTest(0,1,0,0,0,0,0,1,400)
})
it('valid password, invalid peer variable', () => {
// cy.runTest(1,0,0,0,0,0,0,0,400) done allready
// cy.runTest(0,1,0,0,0,0,0,0,400) done allready
// cy.runTest(0,0,0,1,0,0,0,0,400) done allready
// cy.runTest(0,0,0,0,1,0,0,0,400) done allready
// cy.runTest(0,0,0,0,0,1,0,0,400) done allready
// cy.runTest(0,0,0,0,0,0,1,0,400) done allready
// cy.runTest(0,0,0,0,0,0,0,1,400) done allready
})
it('invalid password, invalid peer variable', () => {
// cy.runTest(1,0,1,0,0,0,0,0,400) done allready
// cy.runTest(0,1,1,0,0,0,0,0,400) done allready
cy.runTest(0,0,1,1,0,0,0,0,400)
cy.runTest(0,0,1,0,1,0,0,0,400)
cy.runTest(0,0,1,0,0,1,0,0,400)
cy.runTest(0,0,1,0,0,0,1,0,400)
cy.runTest(0,0,1,0,0,0,0,1,400)
})
it('valid password1, invalid peer variable', () => {
// cy.runTest(1,0,0,0,0,0,0,0,400) done allready
// cy.runTest(0,1,0,0,0,0,0,0,400) done allready
// cy.runTest(0,0,1,0,0,0,0,0,400) done allready
// cy.runTest(0,0,0,0,1,0,0,0,400) done allready
// cy.runTest(0,0,0,0,0,1,0,0,400) done allready
// cy.runTest(0,0,0,0,0,0,1,0,400) done allready
// cy.runTest(0,0,0,0,0,0,0,1,400) done allready
})
it('invalid password1, invalid peer variable', () => {
// cy.runTest(1,0,0,1,0,0,0,0,400) done allready
// cy.runTest(0,1,0,1,0,0,0,0,400) done allready
// cy.runTest(0,0,1,1,0,0,0,0,400) done allready
cy.runTest(0,0,0,1,1,0,0,0,201)
cy.runTest(0,0,0,1,0,1,0,0,201)
cy.runTest(0,0,0,1,0,0,1,0,201)
cy.runTest(0,0,0,1,0,0,0,1,201)
})
=======
before(() => {
cy.visit('/index.html')
})
it('valid username, valid peer variable', () => {
cy.visit('/register.html')
cy.randomUser()
cy.inputValueToField(inputName[1], inputDict[inputName[1]][0])
cy.inputValueToField(inputName[2], inputDict[inputName[2]][0])
cy.inputValueToField(inputName[3], inputDict[inputName[3]][0])
cy.inputValueToField(inputName[4], inputDict[inputName[4]][0])
cy.inputValueToField(inputName[5], inputDict[inputName[5]][0])
cy.inputValueToField(inputName[6], inputDict[inputName[6]][0])
cy.inputValueToField(inputName[7], inputDict[inputName[7]][0])
cy.intercept({ method: 'POST', url: '/api/users/' }).as('registerRequest')
cy.intercept({ method: 'POST', url: '/api/token/' }).as('token')
cy.get('#btn-create-account').click()
cy.wait('@registerRequest').its('response.statusCode').should('eq', 201)
it('all valid variables', () => {
cy.runTest(0, 0, 0, 0, 0, 0, 0, 0, 201)
})
it('valid username, invalid peer variable', () => {
cy.runTest(0, 1, 0, 0, 0, 0, 0, 0, 400)
cy.runTest(0, 0, 1, 0, 0, 0, 0, 0, 400)
cy.runTest(0, 0, 0, 1, 0, 0, 0, 0, 400)
cy.runTest(0, 0, 0, 0, 1, 0, 0, 0, 201)
cy.runTest(0, 0, 0, 0, 0, 1, 0, 0, 201)
cy.runTest(0, 0, 0, 0, 0, 0, 1, 0, 201)
cy.runTest(0, 0, 0, 0, 0, 0, 0, 1, 201)
})
it('invalid username, invalid peer variable', () => {
cy.runTest(1, 1, 0, 0, 0, 0, 0, 0, 400)
cy.runTest(1, 0, 1, 0, 0, 0, 0, 0, 400)
cy.runTest(1, 0, 0, 1, 0, 0, 0, 0, 400)
cy.runTest(1, 0, 0, 0, 1, 0, 0, 0, 400)
cy.runTest(1, 0, 0, 0, 0, 1, 0, 0, 400)
cy.runTest(1, 0, 0, 0, 0, 0, 1, 0, 400)
cy.runTest(1, 0, 0, 0, 0, 0, 0, 1, 400)
})
it('valid email, invalid peer variable', () => {
cy.runTest(1, 0, 0, 0, 0, 0, 0, 0, 400)
// cy.runTest(0,0,1,0,0,0,0,0,400) done allready
// cy.runTest(0,0,0,1,0,0,0,0,400) done allready
// cy.runTest(0,0,0,0,1,0,0,0,201) done allready
// cy.runTest(0,0,0,0,0,1,0,0,201) done allready
// cy.runTest(0,0,0,0,0,0,1,0,201) done allready
// cy.runTest(0,0,0,0,0,0,0,1,201) done allready
})
it('invalid email, invalid peer variable', () => {
//cy.runTest(1,1,0,0,0,0,0,0,400) done allready
cy.runTest(0, 1, 1, 0, 0, 0, 0, 0, 400)
cy.runTest(0, 1, 0, 1, 0, 0, 0, 0, 400)
cy.runTest(0, 1, 0, 0, 1, 0, 0, 0, 400)
cy.runTest(0, 1, 0, 0, 0, 1, 0, 0, 400)
cy.runTest(0, 1, 0, 0, 0, 0, 1, 0, 400)
cy.runTest(0, 1, 0, 0, 0, 0, 0, 1, 400)
})
it('valid password, invalid peer variable', () => {
// cy.runTest(1,0,0,0,0,0,0,0,400) done allready
// cy.runTest(0,1,0,0,0,0,0,0,400) done allready
// cy.runTest(0,0,0,1,0,0,0,0,400) done allready
// cy.runTest(0,0,0,0,1,0,0,0,400) done allready
// cy.runTest(0,0,0,0,0,1,0,0,400) done allready
// cy.runTest(0,0,0,0,0,0,1,0,400) done allready
// cy.runTest(0,0,0,0,0,0,0,1,400) done allready
})
it('invalid password, invalid peer variable', () => {
// cy.runTest(1,0,1,0,0,0,0,0,400) done allready
// cy.runTest(0,1,1,0,0,0,0,0,400) done allready
cy.runTest(0, 0, 1, 1, 0, 0, 0, 0, 400)
cy.runTest(0, 0, 1, 0, 1, 0, 0, 0, 400)
cy.runTest(0, 0, 1, 0, 0, 1, 0, 0, 400)
cy.runTest(0, 0, 1, 0, 0, 0, 1, 0, 400)
cy.runTest(0, 0, 1, 0, 0, 0, 0, 1, 400)
})
it('valid password1, invalid peer variable', () => {
// cy.runTest(1,0,0,0,0,0,0,0,400) done allready
// cy.runTest(0,1,0,0,0,0,0,0,400) done allready
// cy.runTest(0,0,1,0,0,0,0,0,400) done allready
// cy.runTest(0,0,0,0,1,0,0,0,400) done allready
// cy.runTest(0,0,0,0,0,1,0,0,400) done allready
// cy.runTest(0,0,0,0,0,0,1,0,400) done allready
// cy.runTest(0,0,0,0,0,0,0,1,400) done allready
})
it('valid username, valid peer variable', () => {
cy.visit('/logout.html')
it('invalid password1, invalid peer variable', () => {
// cy.runTest(1,0,0,1,0,0,0,0,400) done allready
// cy.runTest(0,1,0,1,0,0,0,0,400) done allready
// cy.runTest(0,0,1,1,0,0,0,0,400) done allready
cy.runTest(0, 0, 0, 1, 1, 0, 0, 0, 201)
cy.runTest(0, 0, 0, 1, 0, 1, 0, 0, 201)
cy.runTest(0, 0, 0, 1, 0, 0, 1, 0, 201)
cy.runTest(0, 0, 0, 1, 0, 0, 0, 1, 201)
})
>>>>>>> d8acf47f0e78635cc266c011e5d29cb1f3776b05
})
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment