<reponame>NeilsC/prestige<gh_stars>100-1000
import json
from http import HTTPStatus

from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse

APPLICATION_JSON = "application/json"


class SignupTests(TestCase):
	fixtures = ["users.json"]

	def test_signup_valid(self):
		response = self.client.post(
			reverse("signup"),
			content_type=APPLICATION_JSON,
			data=json.dumps({
				"email": "<EMAIL>",
				"password": "<PASSWORD>",
			})
		)

		self.assertEqual(response.status_code, HTTPStatus.CREATED)
		self.assertEqual(get_user_model().objects.filter(email="<EMAIL>").count(), 1)

	def test_signup_duplicate_email(self):
		response = self.client.post(
			reverse("signup"),
			content_type=APPLICATION_JSON,
			data=json.dumps({
				"email": "<EMAIL>",
				"password": "<PASSWORD>",
			})
		)

		self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST)
		self.assertEqual(response.json(), {
			"error": {
				"code": "email-duplicate",
				"message": "This email already has an account.",
			},
		})

	def test_signup_duplicate_username(self):
		response = self.client.post(
			reverse("signup"),
			content_type=APPLICATION_JSON,
			data=json.dumps({
				"email": "<EMAIL>",
				"password": "<PASSWORD>",
				"username": "u1",
			}),
		)

		self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST)
		self.assertEqual(response.json(), {
			"error": {
				"code": "username-duplicate",
				"message": "This username already has an account.",
			},
		})

	def test_missing_email(self):
		response = self.client.post(
			reverse("signup"),
			content_type=APPLICATION_JSON,
			data=json.dumps({
				"password": "<PASSWORD>",
			}),
		)

		self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST)
		self.assertEqual(response.json(), {
			"error": {
				"message": "Missing/empty email field in body.",
			},
		})
		self.assertEqual(get_user_model().objects.filter(email="<EMAIL>").count(), 0)

	def test_missing_password(self):
		response = self.client.post(
			reverse("signup"),
			content_type=APPLICATION_JSON,
			data=json.dumps({
				"email": "<EMAIL>",
			}),
		)

		self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST)
		self.assertEqual(response.json(), {
			"error": {
				"message": "Missing/empty password field in body.",
			},
		})
		self.assertEqual(get_user_model().objects.filter(email="<EMAIL>").count(), 0)

	def test_missing_username(self):
		response = self.client.post(
			reverse("signup"),
			content_type=APPLICATION_JSON,
			data=json.dumps({
				"email": "<EMAIL>",
				"password": "<PASSWORD>",
			}),
		)

		self.assertEqual(response.status_code, HTTPStatus.CREATED)
		self.assertEqual(response.json(), {
			'user': {
				'email': '<EMAIL>',
				'username': '<EMAIL>',
				'isGitHubConnected': False,
			},
		})
		self.assertEqual(get_user_model().objects.filter(email="<EMAIL>").count(), 1)

	def test_empty_username(self):
		response = self.client.post(
			reverse("signup"),
			content_type=APPLICATION_JSON,
			data=json.dumps({
				"email": "<EMAIL>",
				"password": "<PASSWORD>",
				"username": "",
			}),
		)

		self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST)
		self.assertEqual(response.json(), {
			"error": {
				"message": "Empty username field in body.",
			},
		})
		self.assertEqual(get_user_model().objects.filter(email="<EMAIL>").count(), 0)
