from django.urls import reverse
from rest_framework.test import APITestCase

from account.models import User


def get_tokens_for_user(
        username='sedragurda', password='sedragurdaredips19', email='sedragurda@desoz.com'):

    user = User.objects.create_user(username=username, email=email, password=password)
    tokens = User.create_jwt_tokens(user)

    return {
        "username": username,
        "password": password,
        "email": email,
        "newsletter": True,
        "user": user,
        "tokens": tokens
    }


class AuthorizationTests(APITestCase):

    def setUp(self):
        data = get_tokens_for_user()
        self.username = data["username"]
        self.password = data["password"]

    def LoginTests(self):
        data = {
            'username_or_email': self.username,
            'password': self.password,
            'remember': True
        }
        response = self.client.post(reverse('auth:authorization:login'), data=data)
        self.assertEqual(response.data['access_token'], self.tokens.get('access_token'))
        self.assertEqual(response.data['refresh_token'], self.tokens.get('refresh_token'))

