tests.py 5.89 KB
from django.test import TestCase
from rest_framework.test import APITestCase
from rest_framework.authtoken.models import Token
from user_manager.models import * 
from routine_manager.models import *
from django.urls import reverse

# Create your tests here.

class TestAPI(APITestCase):
    fixtures = ['tests/complete_fixture.json']
    def test_login_successful(self):
        user = PyrosUser.objects.first()
        test_password = "testpassword"
        user.set_password(test_password)
        user.save()
        url = reverse("api_token_auth")
        response = self.client.post(
                    url, data={"username": user.username, "password": test_password})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(["token"],list(response.data.keys()))

    def test_login_unsuccessful(self):
        user = PyrosUser.objects.first()
        test_password = "testpassword"
        url = reverse("api_token_auth")
        response = self.client.post(
                    url, data={"username": user.username, "password": test_password})
        self.assertEqual(response.status_code, 400)
    def test_logout_successful(self):
        user = PyrosUser.objects.get(username="pyros")
        test_password = "testpassword"
        user.set_password(test_password)
        user.save()
        url = reverse("api_token_auth")
        response = self.client.post(
                    url, data={"username": user.username, "password": test_password})
        token = response.data["token"]
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
        url = reverse("logout")
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    def test_user_view_list(self):
        user = PyrosUser.objects.get(username="pyros")
        self.client.force_authenticate(user=user)
        url = '/api/users/'
        response = self.client.get(url,follow=True)
        self.assertEqual(response.status_code,200)
        self.assertEqual(len(response.data),PyrosUser.objects.all().count())

    def test_user_view_detail(self):
        user = PyrosUser.objects.get(username="pyros")
        self.client.force_authenticate(user=user)
        url = '/api/users/1'
        response = self.client.get(url,follow=True)
        self.assertEqual(response.status_code,200)
        self.assertEqual(response.data["username"],PyrosUser.objects.get(id=1).username)

    def test_sequence_list(self):
        user = PyrosUser.objects.get(username="pyros")
        self.client.force_authenticate(user=user)
        url = '/api/sequences/'
        response = self.client.get(url,follow=True)
        self.assertEqual(response.status_code,200)
        self.assertEqual(len(response.data),Sequence.objects.all().count())


    def test_sequence_detail(self):
        user = PyrosUser.objects.get(username="pyros")
        self.client.force_authenticate(user=user)
        url = '/api/sequences/1'
        response = self.client.get(url,follow=True)
        self.assertEqual(response.status_code,200)
        self.assertEqual(response.data.get("name"),Sequence.objects.get(id=1).name)


    def test_album_list(self):
        user = PyrosUser.objects.get(username="pyros")
        self.client.force_authenticate(user=user)
        url = '/api/albums/'
        response = self.client.get(url,follow=True)
        self.assertEqual(response.status_code,200)
        self.assertEqual(len(response.data),Album.objects.all().count())


    def test_album_detail(self):
        user = PyrosUser.objects.get(username="pyros")
        self.client.force_authenticate(user=user)
        url = '/api/albums/1'
        response = self.client.get(url,follow=True)
        self.assertEqual(response.status_code,200)
        self.assertEqual(response.data.get("name"),Album.objects.get(id=1).name)

    def test_plan_list(self):
        user = PyrosUser.objects.get(username="pyros")
        self.client.force_authenticate(user=user)
        url = '/api/plans/'
        response = self.client.get(url,follow=True)
        self.assertEqual(response.status_code,200)
        self.assertEqual(len(response.data),Plan.objects.all().count())


    def test_plan_detail(self):
        user = PyrosUser.objects.get(username="pyros")
        self.client.force_authenticate(user=user)
        url = '/api/plans/1'
        response = self.client.get(url,follow=True)
        self.assertEqual(response.status_code,200)
        self.assertEqual(response.data.get("name"),Plan.objects.get(id=1).name)

    def test_full_sequence_list(self):
        user = PyrosUser.objects.get(username="pyros")
        self.client.force_authenticate(user=user)
        url = '/api/full_sequences/'
        response = self.client.get(url,follow=True)
        self.assertEqual(response.status_code,200)
        self.assertEqual(len(response.data),Sequence.objects.all().count())


    def test_full_sequence_detail(self):
        user = PyrosUser.objects.get(username="pyros")
        self.client.force_authenticate(user=user)
        url = '/api/full_sequences/1'
        response = self.client.get(url,follow=True)
        self.assertEqual(response.status_code,200)
        self.assertEqual(response.data.get("name"),Sequence.objects.get(id=1).name)

    def test_scientific_program_list(self):
        user = PyrosUser.objects.get(username="pyros")
        self.client.force_authenticate(user=user)
        url = '/api/scientific_programs/'
        response = self.client.get(url,follow=True)
        self.assertEqual(response.status_code,200)
        self.assertEqual(len(response.data),ScientificProgram.objects.all().count())


    def test_full_sequence_detail(self):
        user = PyrosUser.objects.get(username="pyros")
        self.client.force_authenticate(user=user)
        url = '/api/scientific_programs/1'
        response = self.client.get(url,follow=True)
        self.assertEqual(response.status_code,200)
        self.assertEqual(response.data.get("name"),ScientificProgram.objects.get(id=1).name)