from django.test import TestCase
from django.test.client import Client

from users.models import User

from django.core.exceptions import ObjectDoesNotExist

from users.views import SignUp

class SignUpPageTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.valid_email = "test@example.com"
        self.valid_password = "test_password"
        self.invalid_password = "a"*5

        # need to find more eloquent way to test redirect url.
        self.TEST_SERVER_URL = "http://testserver"

    def test_get_signup_page_request_should_return_200(self):
        response = self.client.get("/signup/")
        self.assertEqual(response.status_code, 200)
    
    def test_check_uniqueness_of_new_user_information_from_signup_page(self):
        User.objects.create_user(self.valid_email, self.valid_password)

        try:
            response = self.client.post("/signup/", {
                "email":self.valid_email,
                "password":self.valid_password,
                })
        except:
            pass
    
    def test_post_signup_page_with_available_new_user_information_should_return_302(self):
        response = self.client.post("/signup/", {
            "email": self.valid_email,
            "password": self.valid_password,
            })
        self.assertEqual(response.status_code, 302)
    
    def test_post_signup_page_with_available_user_information_should_redirect_to_home(self):
        response = self.client.post("/signup/", {
            "email": self.valid_email,
            "password": self.valid_password,
            })
        self.assertEqual(response["Location"], self.TEST_SERVER_URL + "/")
   
    def test_post_signup_page_with_non_available_user_information_should_return_302(self):
        response = self.client.post("/signup/", {
            "email": self.valid_email,
            "password": self.invalid_password,
            })
        self.assertEqual(response.status_code, 302)
    
    def test_post_signup_page_with_non_available_user_information_should_redirect_to_signup(self):
        response = self.client.post("/signup/", {
            "email": self.valid_email,
            "password": self.invalid_password,
            })
        self.assertEqual(response["Location"], self.TEST_SERVER_URL + "/signup/")
    
    # POST with no user information
    def test_post_signup_page_with_no_user_information_should_have_error_message(self):
        response = self.client.post("/signup/", {})
        self.assertContains(response, "This field is required.")
    

@@ -534,45 +534,31 @@
 get_
-signup_page_request_should_return_200
+response_to_signup_page
 (sel
@@ -602,24 +602,39 @@
 t(%22/signup/%22
+, follow = True
 )%0A        se
@@ -661,24 +661,37 @@
 nse.
-status_code, 200
+wsgi_request.path, %22/signup/%22
 )%0A  
@@ -1034,16 +1034,31 @@
   except
+ IntegrityError
 :%0A      
@@ -1071,305 +1071,8 @@
 pass
-%0A    %0A    def test_post_signup_page_with_available_new_user_information_should_return_302(self):%0A        response = self.client.post(%22/signup/%22, %7B%0A            %22email%22: self.valid_email,%0A            %22password%22: self.valid_password,%0A            %7D)%0A        self.assertEqual(response.status_code, 302)
 %0A   
@@ -1091,33 +1091,16 @@
 st_post_
-signup_page_with_
 availabl
@@ -1093,32 +1093,36 @@
 _post_available_
+new_
 user_information
@@ -1114,39 +1114,32 @@
 ser_information_
-should_
 redirect_to_home
@@ -1138,11 +1138,17 @@
 _to_
-hom
+login_pag
 e(se
@@ -1291,33 +1291,33 @@
 d,%0A            %7D
-)
+,
 %0A        self.as
@@ -1313,462 +1313,148 @@
     
-self.assertEqual(response%5B%22Location%22%5D, self.TEST_SERVER_URL + %22/%22)%0A   %0A    def test_post_signup_page_with_non_available_user_information_should_return_302(self):%0A        response = self.client.post(%22/signup/%22, %7B%0A            %22email%22: self.valid_email,%0A            %22password%22: self.invalid_password,%0A            %7D)%0A        self.assertEqual(response.status_code, 302)%0A    %0A    def test_post_signup_page_with_non_available_user_information_should_redirect_to
+    follow = True%0A        )%0A        self.assertEqual(response._request.path, %22/login/%22)%0A  %0A    def test_post_invalid_user_information_return
 _sig
@@ -1617,79 +1617,94 @@
    %7D
-)%0A        self.assertEqual(response%5B%22Location%22%5D, self.TEST_SERVER_URL +
+,%0A            follow = True%0A        )%0A        self.assertEqual(response._request.path,
  %22/s

test user sign page test more precise