Beispiel #1
0
    def test_required(self) -> None:
        product_plan = self.ProductPlanFactory.create()
        data: Dict[str, str] = {}
        form = SignupForm(product_plan, data=data)

        self.assertFalse(form.is_valid())
        self.assertIn("username", form.errors)
        self.assertIn("email", form.errors)
        self.assertIn("password", form.errors)
        self.assertIn("stripe_token", form.errors)
        self.assertNotIn("postal_code", form.errors)
Beispiel #2
0
    def test_valid(self) -> None:
        product_plan = self.ProductPlanFactory.create()
        data = {
            "username": "******",
            "email": "*****@*****.**",
            "password": "******",
            "stripe_token": "tok_1234",
            "postal_code": "12345",
        }
        form = SignupForm(product_plan, data=data)

        self.assertTrue(form.is_valid())
        self.assertEqual(product_plan, form.product_plan)
Beispiel #3
0
    def test_unique_username(self) -> None:
        product_plan = self.ProductPlanFactory.create()
        self.UserFactory.create(username="******")
        data = {
            "username": "******",
            "email": "*****@*****.**",
            "password": "******",
            "stripe_token": "tok_1234",
            "postal_code": "12345",
        }
        form = SignupForm(product_plan, data=data)

        self.assertFalse(form.is_valid())
        self.assertIn("username", form.errors)
Beispiel #4
0
    def test_invalid_password(self) -> None:
        product_plan = self.ProductPlanFactory.create()
        # Test similar username and password to ensure a user instance
        # is present and valuable.
        data = {
            "username": "******",
            "email": "*****@*****.**",
            "password": "******",
            "stripe_token": "tok_1234",
            "postal_code": "12345",
        }
        form = SignupForm(product_plan, data=data)

        self.assertFalse(form.is_valid())
        self.assertIn("password", form.errors)
Beispiel #5
0
    def test_missing_postal_code(self, stripe_gateway: mock.MagicMock) -> None:
        product_plan = self.ProductPlanFactory.create()
        stripe_gateway.create_customer.return_value = "cus_1234"
        data = {
            "username": "******",
            "email": "*****@*****.**",
            "password": "******",
            "stripe_token": "tok_1234",
            "postal_code": None,
        }
        form = SignupForm(product_plan, data=data)
        self.assertTrue(form.is_valid())

        user = form.save()

        self.assertEqual(user.profile.postal_code, "")
Beispiel #6
0
def signup(request: HttpRequest) -> HttpResponse:
    """Sign up a new user."""
    product_plan = ProductPlan.objects.filter(active=True).last()

    if request.method == "POST":
        form = SignupForm(product_plan, data=request.POST)
        if form.is_valid():
            user = form.save()
            login(request, user)
            return JsonResponse({"status": "success"})
        return JsonResponse({
            "status": "error",
            "errors": dict(form.errors.items())
        })

    context = {
        "product_plan": product_plan,
        "stripe_publishable_key": settings.STRIPE_PUBLISHABLE_KEY,
    }
    return render(request, "accounts/signup.html", context)