Monday, September 23, 2024

Re: testing signal with django allauth

Sorry, I did not add the code before. This is how I have my tests set up at the moment. I cannot pass the 'form is valid' step. 

from django.test import TestCase,Client
from django.urls import reverse, reverse_lazy
from django.contrib.auth import get_user_model
from .forms import StyleForm
from allauth.account.forms import LoginForm
from django.test.utils import override_settings
from allauth.account import app_settings



class ProfileLinkUserAutenticate(TestCase):
"""to test if the open profile test saves after login and signup """

fixtures = ['between_app/fixtures/PersonalStyleGroup.yaml',
'between_app/fixtures/PersonalStyleSection.yaml'
]
def setup(self):
self.client = Client() #to explore templates in request
@classmethod
def setUpTestData(cls):
cls.login_data = {'password':'test123%%HH','remember':'t',
'username':'usertest'}
cls.user = get_user_model().objects.create_user(
username = 'usertest',
email = 'test@test.com',
password = 'test123%%HH',
)
cls.data = {
'follower_1':90,
'propositive_1':2,
'challenger_1':6,
'acceptant_1':10,
'intensive_1':20,
'extensive_1':6,
'divider_1':3,
'containment_1':30,
'becoming_1':1,
'development_1':60,
'individuation_1':10,
'belonging_1':3,

}

def test_client_login(self):
self.client.login(
username = 'usertest',
email = 'test@test.com',
password = 'test123',
)
self.assertTrue(self.user.is_authenticated)
self.client.logout()
def test_form_valid(self):
form_invalid = StyleForm(data={"name": "Computer", "price": 400.1234})
self.assertFalse(form_invalid.is_valid())
form_valid = StyleForm(data=self.data)
self.assertTrue(form_valid.is_valid())
def test_post_form(self):
response = self.client.post('/profile_test/',self.data, follow=True)
self.assertEqual(self.client.session['linked'],"false")
self.assertContains(response,"Compassionate")

def test_login_form_is_valid(self):
login_valid = LoginForm(data=self.login_data)
self.assertTrue(login_valid.is_valid())
def test_login_after_test(self):
self.client.logout()
response = self.client.post('/profile_test/',self.data, follow=True)
self.assertEqual(self.client.session['linked'],"false")
response = self.client.post(reverse('account_login'),data=self.login_data)
#self.assertRedirects(response=response,expected_url='/')
#self.client.login(username='usertest',password='test123')
response = self.client.get('')
#self.assertContains(response,"Welcome back")
self.assertEqual(self.client.session['linked'],"true")
#self.assertContains(response,"Welcome back")


On Saturday 21 September 2024 at 21:08:39 UTC+1 Gabriel Soler wrote:
Hi all, 

Thanks for reading. I am at a stage where I am needing to do my testing, and its is hard!
I managed to add a function after sign up and log in, using the signals of allauth. As I am trying to add my tests as I work, I have added a little change to the Session, to mark if my functions have don what they should, and I also would like to test if actually is doing the thing.
Now, when I try with the unit tests to send a dictionary with the login I am not being able to do a form that is valid for allauth. Can somebody help we figuring out how to test this? Or some wisdom about testing? (My function is working, but I am learning about testing, and test driven development, and I came to it because I have been fearing to do changes and then create unknown bugs, so it is time!)

Gabriel

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/44e9f138-a9b0-417e-8665-f9003521d6c4n%40googlegroups.com.

No comments:

Post a Comment