from django.test import TestCase
from django.contrib.auth.models import User, Group
from rest_framework.test import APIClient


class LoggedInTestCase(TestCase):
    def setUp(self):
        # These objects are recreated afresh for every test method below.
        # Data updated or created in a test method will not persist to another test method.
        self._client = APIClient()

        self._joeBloggs = User.objects.create_user(username='Joe Bloggs',
                                                   email='<EMAIL>', password='<PASSWORD>')
        self._joeBloggs.groups.add(Group.objects.create(name="joe_group"))
        self._janeDoe = User.objects.create_user(username='Jane Doe',
                                                 email='<EMAIL>', password='<PASSWORD>')
        self._janeDoe.groups.add(Group.objects.create(name="jane_group"))
        self._adminUser = User.objects.create_user(username='Super Man',
                                                   email='<EMAIL>', password='<PASSWORD>')
        self._adminUser.groups.add(Group.objects.get(name="admin"))
        # Admin users are also staff for the purposes of permissions.
        self._adminUser.groups.add(Group.objects.get(name="staff"))
        self._staffUser = User.objects.create_user(username='Lab Tech',
                                                   email='<EMAIL>', password='<PASSWORD>')
        self._staffUser.groups.add(Group.objects.get(name="staff"))

    # Utility function to switch user. Joe is a normal user in group joe_group
    def _asJoeBloggs(self):
        self._client.logout()
        self._client.login(username="Joe Bloggs", password="<PASSWORD>")

    # Utility function to switch user. Jane is a user in group jane_group.
    def _asJaneDoe(self):
        self._client.logout()
        self._client.login(username="Jane Doe", password="<PASSWORD>")

    # Utility function to switch user. Admin is an admin user.
    def _asAdmin(self):
        self._client.logout()
        self._client.login(username="Super Man", password="<PASSWORD>")

    # Utility function to switch user. Staff is a staff user.
    def _asStaff(self):
        self._client.logout()
        self._client.login(username="Lab Tech", password="<PASSWORD>")

    # Utility function to switch user
    def _asAnonymous(self):
        self._client.logout()

    # Utility function to switch user
    def _asInvalid(self):
        self._client.logout()
        self._client.login(username="Non Existent", password="<PASSWORD>")

from django.test import TestCase
from django.contrib.auth.models import User, Group
from rest_framework.test import APIClient


class LoggedInTestCase(TestCase):
    def setUp(self):
        # These objects are recreated afresh for every test method below.
        # Data updated or created in a test method will not persist to another test method.
        self._client = APIClient()

        self._joeBloggs = User.objects.create_user(username='Joe Bloggs',
                                                   email='<EMAIL>', password='<PASSWORD>')
        self._joeBloggs.groups.add(Group.objects.create(name="joe_group"))
        self._janeDoe = User.objects.create_user(username='Jane Doe',
                                                 email='<EMAIL>', password='<PASSWORD>')
        self._janeDoe.groups.add(Group.objects.create(name="jane_group"))
        self._adminUser = User.objects.create_user(username='Super Man',
                                                   email='<EMAIL>', password='<PASSWORD>')
        self._adminUser.groups.add(Group.objects.get(name="admin"))
        # Admin users are also staff for the purposes of permissions.
        self._adminUser.groups.add(Group.objects.get(name="staff"))
        self._staffUser = User.objects.create_user(username='Lab Tech',
                                                   email='<EMAIL>', password='<PASSWORD>')
        self._staffUser.groups.add(Group.objects.get(name="staff"))

    # Utility function to switch user. Joe is a normal user in group joe_group
    def _asJoeBloggs(self):
        self._client.logout()
        self._client.login(username="Joe Bloggs", password="<PASSWORD>")

    # Utility function to switch user. Jane is a user in group jane_group.
    def _asJaneDoe(self):
        self._client.logout()
        self._client.login(username="Jane Doe", password="<PASSWORD>")

    # Utility function to switch user. Admin is an admin user.
    def _asAdmin(self):
        self._client.logout()
        self._client.login(username="Super Man", password="<PASSWORD>")

    # Utility function to switch user. Staff is a staff user.
    def _asStaff(self):
        self._client.logout()
        self._client.login(username="Lab Tech", password="<PASSWORD>")

    # Utility function to switch user
    def _asAnonymous(self):
        self._client.logout()

    # Utility function to switch user
    def _asInvalid(self):
        self._client.logout()
        self._client.login(username="Non Existent", password="<PASSWORD>")

# These objects are recreated afresh for every test method below. # Data updated or created in a test method will not persist to another test method. # Admin users are also staff for the purposes of permissions. # Utility function to switch user. Joe is a normal user in group joe_group # Utility function to switch user. Jane is a user in group jane_group. # Utility function to switch user. Admin is an admin user. # Utility function to switch user. Staff is a staff user. # Utility function to switch user # Utility function to switch user