Coverage for donation\test_views.py: 100%

63 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-06-20 12:38 +0100

1from django.contrib.auth.models import User 

2from django.test import TestCase 

3from django.urls import reverse 

4 

5from donation.models import Donation 

6 

7 

8class DonationViewTests(TestCase): 

9 def setUp(self): 

10 self.user = User.objects.create_superuser(username='superuser', 

11 password='12345') 

12 self.donation = Donation(title='Test Donation', 

13 goal=1000, description='Test Description') 

14 self.donation.save() 

15 

16 def test_delete_donation(self): 

17 self.client.login(username='superuser', password='12345') 

18 self.assertEqual(Donation.objects.count(), 1) 

19 self.client.post(reverse('delete_donation', 

20 kwargs={'donation_id': self.donation.id})) 

21 self.assertEqual(Donation.objects.count(), 0) 

22 

23 def test_render_donations_page_not_superuser(self): 

24 response = self.client.get(reverse('donations')) 

25 self.assertEqual(response.status_code, 200) 

26 self.assertIn(b"Donate now", response.content) 

27 self.assertNotIn(b"Edit", response.content) 

28 self.assertNotIn(b"Delete", response.content) 

29 

30 def test_render_donations_page_superuser(self): 

31 self.client.login(username='superuser', password='12345') 

32 response = self.client.get(reverse('donations')) 

33 self.assertEqual(response.status_code, 200) 

34 self.assertIn(b"Edit", response.content) 

35 self.assertIn(b"Delete", response.content) 

36 self.assertNotIn(b"Donate now", response.content) 

37 

38 def test_render_history_page(self): 

39 response = self.client.get(reverse('history')) 

40 self.assertEqual(response.status_code, 200) 

41 self.assertIn(b"Payment", response.content) 

42 self.assertIn(b"ID", response.content) 

43 self.assertIn(b"Stripe Charge ID", response.content) 

44 self.assertIn(b"Username", response.content) 

45 self.assertIn(b"Donation", response.content) 

46 self.assertIn(b"Title", response.content) 

47 self.assertIn(b"Amount", response.content) 

48 self.assertIn(b"Timestamp", response.content) 

49 self.assertIn(b"Total", response.content) 

50 

51 def test_redirect_to_donate_login(self): 

52 self.user = User.objects.create_user(username='user', password='12345') 

53 self.client.login(username='user', password='12345') 

54 response = self.client.get(reverse('redirect_to_donate', 

55 kwargs={ 

56 'donation_id': self.donation.id 

57 }), follow=True) 

58 self.assertEqual(response.status_code, 200) 

59 self.assertIn(b"You may change a cause", response.content) 

60 

61 def test_redirect_to_donate_not_login(self): 

62 response = self.client.get(reverse('redirect_to_donate', 

63 kwargs={ 

64 'donation_id': self.donation.id 

65 }), follow=True) 

66 self.assertEqual(response.status_code, 200) 

67 self.assertNotIn(b"You may change a cause", response.content) 

68 

69 def test_create_donation(self): 

70 self.client.login(username='superuser', password='12345') 

71 post_data = { 

72 'title': 'Test Donation2', 

73 'goal': 2000, 

74 'description': 'Test Description2' 

75 } 

76 self.assertEqual(Donation.objects.count(), 1) 

77 self.client.post(reverse('create_donation'), post_data) 

78 self.assertEqual(Donation.objects.count(), 2) 

79 

80 def test_update_donation(self): 

81 self.client.login(username='superuser', password='12345') 

82 post_data = { 

83 'title': 'Test Donation2', 

84 'goal': 2000, 

85 'description': 'Test Description2' 

86 } 

87 self.client.post(reverse('update_donation', 

88 kwargs={'donation_id': self.donation.id}), 

89 post_data) 

90 self.donation.refresh_from_db() 

91 self.assertEqual(self.donation.title, 'Test Donation2') 

92 self.assertEqual(self.donation.goal, 2000) 

93 self.assertEqual(self.donation.description, 'Test Description2')