<reponame>Nobay/SampleStore
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APITestCase

from .serializers import UserSerializer


class VisitorCreateUserTest(APITestCase):
    def setUp(self):
        self.data = {'username': 'mike', 'password': '<PASSWORD>', 'first_name': 'Mike',
                     'last_name': 'Tyson', 'email': '<EMAIL>'}

    def test_can_create_user(self):
        response = self.client.post("/users/", self.data)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)


class AdminCreateUserTest(APITestCase):
    def setUp(self):
        self.superuser = User.objects.create_superuser('john', '<EMAIL>', '<PASSWORD>')
        self.client.login(username='john', password='<PASSWORD>')
        self.data = {'username': 'moke', 'password': '<PASSWORD>', 'first_name': 'tato',
                     'last_name': 'tota', 'email': '<EMAIL>'}

    def test_can_create_user2(self):
        response = self.client.post('/users/', self.data)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)


class UserCreateUserTest(APITestCase):
    def setUp(self):
        self.user = User.objects.create_user('jack', '<EMAIL>', '<PASSWORD>')
        self.data = {'username': 'moykes', 'password': '<PASSWORD>', 'first_name': 'tarantino',
                     'last_name': 'koppachino', 'email': '<EMAIL>'}

    def get_token(self):
        return self.client.post("/get_auth_token/", {'username': 'jack', 'password': '<PASSWORD>'})

    def test_cant_create_user3(self):
        token = self.get_token()
        response = self.client.post('/users/', self.data, **{"HTTP_AUTHORIZATION": "Token "+token.json()['token']})
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

"""
class ReadUserTest(APITestCase):
    def setUp(self):
        self.superuser = User.objects.create_superuser('john', '<EMAIL>', '<PASSWORD>')
        self.client.login(username='john', password='<PASSWORD>')
        self.user = User.objects.create(username="mike")

    def test_can_read_user_list(self):
        response = self.client.get(reverse('user-list'))
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_can_read_user_detail(self):
        response = self.client.get(reverse('user-detail', args=[self.user.id]))
        self.assertEqual(response.status_code, status.HTTP_200_OK)


class UpdateUserTest(APITestCase):
    def setUp(self):
        self.superuser = User.objects.create_superuser('john', '<EMAIL>', '<PASSWORD>')
        self.client.login(username='john', password='<PASSWORD>')
        self.user = User.objects.create(username="mike", first_name="Tyson")
        self.data = UserSerializer(self.user).data
        self.data.update({'first_name': 'Changed'})

    def test_can_update_user(self):
        response = self.client.put(reverse('user-detail', args=[self.user.id]), self.data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)


class DeleteUserTest(APITestCase):
    def setUp(self):
        self.superuser = User.objects.create_superuser('john', '<EMAIL>', '<PASSWORD>')
        self.client.login(username='john', password='<PASSWORD>')
        self.user = User.objects.create(username="mikey")

    def test_can_delete_user(self):
        response = self.client.delete(reverse('user-detail', args=[self.user.id]))
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
"""
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APITestCase

from .serializers import UserSerializer


class VisitorCreateUserTest(APITestCase):
    def setUp(self):
        self.data = {'username': 'mike', 'password': '<PASSWORD>', 'first_name': 'Mike',
                     'last_name': 'Tyson', 'email': '<EMAIL>'}

    def test_can_create_user(self):
        response = self.client.post("/users/", self.data)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)


class AdminCreateUserTest(APITestCase):
    def setUp(self):
        self.superuser = User.objects.create_superuser('john', '<EMAIL>', '<PASSWORD>')
        self.client.login(username='john', password='<PASSWORD>')
        self.data = {'username': 'moke', 'password': '<PASSWORD>', 'first_name': 'tato',
                     'last_name': 'tota', 'email': '<EMAIL>'}

    def test_can_create_user2(self):
        response = self.client.post('/users/', self.data)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)


class UserCreateUserTest(APITestCase):
    def setUp(self):
        self.user = User.objects.create_user('jack', '<EMAIL>', '<PASSWORD>')
        self.data = {'username': 'moykes', 'password': '<PASSWORD>', 'first_name': 'tarantino',
                     'last_name': 'koppachino', 'email': '<EMAIL>'}

    def get_token(self):
        return self.client.post("/get_auth_token/", {'username': 'jack', 'password': '<PASSWORD>'})

    def test_cant_create_user3(self):
        token = self.get_token()
        response = self.client.post('/users/', self.data, **{"HTTP_AUTHORIZATION": "Token "+token.json()['token']})
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

"""
class ReadUserTest(APITestCase):
    def setUp(self):
        self.superuser = User.objects.create_superuser('john', '<EMAIL>', '<PASSWORD>')
        self.client.login(username='john', password='<PASSWORD>')
        self.user = User.objects.create(username="mike")

    def test_can_read_user_list(self):
        response = self.client.get(reverse('user-list'))
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_can_read_user_detail(self):
        response = self.client.get(reverse('user-detail', args=[self.user.id]))
        self.assertEqual(response.status_code, status.HTTP_200_OK)


class UpdateUserTest(APITestCase):
    def setUp(self):
        self.superuser = User.objects.create_superuser('john', '<EMAIL>', '<PASSWORD>')
        self.client.login(username='john', password='<PASSWORD>')
        self.user = User.objects.create(username="mike", first_name="Tyson")
        self.data = UserSerializer(self.user).data
        self.data.update({'first_name': 'Changed'})

    def test_can_update_user(self):
        response = self.client.put(reverse('user-detail', args=[self.user.id]), self.data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)


class DeleteUserTest(APITestCase):
    def setUp(self):
        self.superuser = User.objects.create_superuser('john', '<EMAIL>', '<PASSWORD>')
        self.client.login(username='john', password='<PASSWORD>')
        self.user = User.objects.create(username="mikey")

    def test_can_delete_user(self):
        response = self.client.delete(reverse('user-detail', args=[self.user.id]))
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
"""
class ReadUserTest(APITestCase):     def setUp(self):         self.superuser = User.objects.create_superuser('john', '<EMAIL>', '<PASSWORD>')         self.client.login(username='john', password='<PASSWORD>')         self.user = User.objects.create(username="mike")      def test_can_read_user_list(self):         response = self.client.get(reverse('user-list'))         self.assertEqual(response.status_code, status.HTTP_200_OK)      def test_can_read_user_detail(self):         response = self.client.get(reverse('user-detail', args=[self.user.id]))         self.assertEqual(response.status_code, status.HTTP_200_OK)   class UpdateUserTest(APITestCase):     def setUp(self):         self.superuser = User.objects.create_superuser('john', '<EMAIL>', '<PASSWORD>')         self.client.login(username='john', password='<PASSWORD>')         self.user = User.objects.create(username="mike", first_name="Tyson")         self.data = UserSerializer(self.user).data         self.data.update({'first_name': 'Changed'})      def test_can_update_user(self):         response = self.client.put(reverse('user-detail', args=[self.user.id]), self.data)         self.assertEqual(response.status_code, status.HTTP_200_OK)   class DeleteUserTest(APITestCase):     def setUp(self):         self.superuser = User.objects.create_superuser('john', '<EMAIL>', '<PASSWORD>')         self.client.login(username='john', password='<PASSWORD>')         self.user = User.objects.create(username="mikey")      def test_can_delete_user(self):         response = self.client.delete(reverse('user-detail', args=[self.user.id]))         self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)