Commit 895649e216f5a454dee71933a6f35cfb5e9ba388
1 parent
36dfc230
Exists in
master
and in
5 other branches
Test files #128 #129 #130
Showing
2 changed files
with
183 additions
and
0 deletions
Show diff stats
| ... | ... | @@ -0,0 +1,183 @@ |
| 1 | +from django.test import TestCase, Client | |
| 2 | +from django.core.urlresolvers import reverse | |
| 3 | +from django.core.files.uploadedfile import SimpleUploadedFile | |
| 4 | +from rolepermissions.shortcuts import assign_role | |
| 5 | +from django.utils.translation import ugettext_lazy as _ | |
| 6 | +from core.models import MimeType | |
| 7 | +from users.models import User | |
| 8 | +from files.models import TopicFile | |
| 9 | +from files.forms import FileForm, UpdateFileForm | |
| 10 | +from courses.models import CourseCategory, Course, Subject, Topic | |
| 11 | + | |
| 12 | +class FileTestCase(TestCase): | |
| 13 | + | |
| 14 | + def setUp(self): | |
| 15 | + self.client = Client() | |
| 16 | + | |
| 17 | + self.admin = User.objects.create_user( | |
| 18 | + username = 'admin', | |
| 19 | + email = 'testing@amadeus.com', | |
| 20 | + is_staff = True, | |
| 21 | + is_active = True, | |
| 22 | + password = 'testing123' | |
| 23 | + ) | |
| 24 | + assign_role(self.admin, 'system_admin') | |
| 25 | + | |
| 26 | + self.teacher = User.objects.create_user( | |
| 27 | + username = 'teacher', | |
| 28 | + email = 'teacherg@school.com', | |
| 29 | + is_staff = False, | |
| 30 | + is_active = True, | |
| 31 | + password = 'teaching123' | |
| 32 | + ) | |
| 33 | + assign_role(self.teacher, 'professor') | |
| 34 | + | |
| 35 | + self.student = User.objects.create_user( | |
| 36 | + username = 'student', | |
| 37 | + email = 'student@amadeus.com', | |
| 38 | + is_staff = False, | |
| 39 | + is_active = True, | |
| 40 | + password = 'testing123', | |
| 41 | + type_profile = 2 | |
| 42 | + ) | |
| 43 | + assign_role(self.student, 'student') | |
| 44 | + | |
| 45 | + self.category = CourseCategory( | |
| 46 | + name = 'Categoria Teste', | |
| 47 | + slug = 'categoria_teste' | |
| 48 | + ) | |
| 49 | + self.category.save() | |
| 50 | + | |
| 51 | + self.course = Course( | |
| 52 | + name = 'Curso Teste', | |
| 53 | + slug = 'curso_teste', | |
| 54 | + max_students = 50, | |
| 55 | + init_register_date = '2016-08-26', | |
| 56 | + end_register_date = '2016-10-01', | |
| 57 | + init_date = '2016-10-05', | |
| 58 | + end_date = '2017-10-05', | |
| 59 | + category = self.category, | |
| 60 | + public = True, | |
| 61 | + ) | |
| 62 | + self.course.save() | |
| 63 | + | |
| 64 | + self.subject = Subject( | |
| 65 | + name = 'Subject Test', | |
| 66 | + description = "description of the subject test file", | |
| 67 | + visible = True, | |
| 68 | + init_date = '2016-10-05', | |
| 69 | + end_date = '2017-10-05', | |
| 70 | + course = self.course, | |
| 71 | + ) | |
| 72 | + self.subject.save() | |
| 73 | + | |
| 74 | + self.subject.professors.add(self.teacher) | |
| 75 | + | |
| 76 | + self.topic = Topic( | |
| 77 | + name = 'Topic Test', | |
| 78 | + description = "description of the topic test file", | |
| 79 | + subject = self.subject, | |
| 80 | + owner = self.teacher, | |
| 81 | + ) | |
| 82 | + self.topic.save() | |
| 83 | + | |
| 84 | + """ | |
| 85 | + Manual upload file | |
| 86 | + Change directory for a file in your computer and be happy... | |
| 87 | + """ | |
| 88 | + upload_file = open('/home/ailson/Pictures/teste.png', 'rb') | |
| 89 | + self.file = SimpleUploadedFile(upload_file.name, upload_file.read()) | |
| 90 | + | |
| 91 | + def test_create_file_ok(self): | |
| 92 | + self.client.login(username='admin', password = 'testing123') | |
| 93 | + | |
| 94 | + files = TopicFile.objects.all().count() | |
| 95 | + self.assertEqual(TopicFile.objects.all().count(), files) #Macthing no file | |
| 96 | + | |
| 97 | + topic = Topic.objects.get(name = 'Topic Test') | |
| 98 | + | |
| 99 | + url = reverse('course:file:create_file', kwargs={'slug': topic.slug}) | |
| 100 | + data = { | |
| 101 | + 'name' : 'testFile', | |
| 102 | + "file_url" : self.file | |
| 103 | + } | |
| 104 | + data['topic'] = topic | |
| 105 | + | |
| 106 | + # Get modal | |
| 107 | + response = self.client.get(url) | |
| 108 | + self.assertEqual(response.status_code, 200) | |
| 109 | + | |
| 110 | + # Create file | |
| 111 | + response = self.client.post(url, data) | |
| 112 | + file_created = TopicFile.objects.get(name = data['name']) | |
| 113 | + self.assertEqual(TopicFile.objects.filter(name= file_created.name).exists(),True) | |
| 114 | + self.assertEqual(TopicFile.objects.all().count(), files + 1) | |
| 115 | + self.assertEqual(response.status_code, 302) | |
| 116 | + self.assertTemplateUsed(template_name = 'files/create_file.html') | |
| 117 | + | |
| 118 | + def test_update_file_ok(self): | |
| 119 | + self.client.login(username='admin', password = 'testing123') | |
| 120 | + | |
| 121 | + topic = Topic.objects.get(name = 'Topic Test') | |
| 122 | + | |
| 123 | + # File type | |
| 124 | + mime_type = MimeType.objects.create( | |
| 125 | + typ = 'image/png', | |
| 126 | + icon = 'photo' | |
| 127 | + ) | |
| 128 | + self.file_update = TopicFile.objects.create( | |
| 129 | + name = 'testinglink', | |
| 130 | + file_url = self.file, | |
| 131 | + file_type = mime_type, | |
| 132 | + topic = topic | |
| 133 | + ) | |
| 134 | + | |
| 135 | + url = reverse('course:file:update_file',kwargs={'slug': self.file_update.slug}) | |
| 136 | + | |
| 137 | + upload_file_update = open('/home/ailson/Pictures/update.png', 'rb') | |
| 138 | + new_file = SimpleUploadedFile(upload_file_update.name, upload_file_update.read()) | |
| 139 | + data = { | |
| 140 | + 'name' : 'updated', | |
| 141 | + 'file_url': new_file | |
| 142 | + } | |
| 143 | + | |
| 144 | + # Get modal | |
| 145 | + response = self.client.get(url) | |
| 146 | + self.assertEqual(response.status_code, 200) | |
| 147 | + | |
| 148 | + response = self.client.post(url, data) | |
| 149 | + self.assertEqual(TopicFile.objects.all()[0].name, 'updated') # new file name | |
| 150 | + self.assertEqual(response.status_code, 302) | |
| 151 | + self.assertTemplateUsed(template_name = 'files/update_file.html') | |
| 152 | + | |
| 153 | + def test_delete_file(self): | |
| 154 | + self.client.login(username='admin', password = 'testing123') | |
| 155 | + | |
| 156 | + topic = Topic.objects.get(name = 'Topic Test') | |
| 157 | + | |
| 158 | + # File type | |
| 159 | + mime_type = MimeType.objects.create( | |
| 160 | + typ = 'image/png', | |
| 161 | + icon = 'photo' | |
| 162 | + ) | |
| 163 | + self.file_delete = TopicFile.objects.create( | |
| 164 | + name = 'testinglink', | |
| 165 | + file_url = self.file, | |
| 166 | + file_type = mime_type, | |
| 167 | + topic = topic | |
| 168 | + ) | |
| 169 | + | |
| 170 | + url = reverse('course:file:delete_file',kwargs={'slug': self.file_delete.slug}) | |
| 171 | + | |
| 172 | + # Get modal | |
| 173 | + response = self.client.get(url) | |
| 174 | + self.assertEqual(response.status_code, 200) | |
| 175 | + | |
| 176 | + response = self.client.post(url) | |
| 177 | + self.assertEqual(TopicFile.objects.all().count(), 0) # new file name | |
| 178 | + self.assertEqual(response.status_code, 302) | |
| 179 | + self.assertTemplateUsed(template_name = 'files/delete_file.html') | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | ... | ... |