simplify profile edition and test it
This commit is contained in:
parent
0420839b20
commit
1d7499d3b2
2 changed files with 22 additions and 8 deletions
|
@ -65,12 +65,27 @@ class SimpleTest(TestCase):
|
|||
class TestProfile(TestCase):
|
||||
@patch('kfet.signals.messages')
|
||||
def test_profile(self, mock_messages):
|
||||
# Test that the root user can log in
|
||||
User.objects.create_superuser('root', 'foo@bar.com', 'root')
|
||||
c = Client()
|
||||
c.login(username='root', password='root')
|
||||
response = c.get('/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
# Test the profile edition
|
||||
post_data = {
|
||||
"first_name": "foo",
|
||||
"last_name": "bar",
|
||||
"phone": "0123456789",
|
||||
"departement": "baz"
|
||||
}
|
||||
c.post("/profile", post_data)
|
||||
user = User.objects.get(username="root")
|
||||
self.assertEqual(user.first_name, post_data['first_name'])
|
||||
self.assertEqual(user.last_name, post_data["last_name"])
|
||||
self.assertEqual(user.profile.phone, post_data["phone"])
|
||||
self.assertEqual(user.profile.departement, post_data["departement"])
|
||||
|
||||
|
||||
class AuthTest(TestCase):
|
||||
def test_login(self):
|
||||
|
|
|
@ -57,16 +57,15 @@ def logout(request):
|
|||
@login_required
|
||||
def profile(request):
|
||||
success = False
|
||||
user = request.user
|
||||
if request.method == "POST":
|
||||
user_form = UserForm(request.POST, instance=request.user)
|
||||
if user_form.is_valid():
|
||||
user = user_form.save()
|
||||
profile_form = ProfileForm(request.POST, instance=user.profile)
|
||||
if profile_form.is_valid():
|
||||
profile_form.save()
|
||||
success = True
|
||||
user_form = UserForm(request.POST, instance=user)
|
||||
profile_form = ProfileForm(request.POST, instance=user.profile)
|
||||
if (user_form.is_valid() and profile_form.is_valid()):
|
||||
user_form.save()
|
||||
profile_form.save()
|
||||
success = True
|
||||
else:
|
||||
user = request.user
|
||||
user_form = UserForm(instance=user)
|
||||
profile_form = ProfileForm(instance=user.profile)
|
||||
return render(request, "gestion/profile.html",
|
||||
|
|
Loading…
Reference in a new issue