|
13 | 13 | ProjectAttribute, |
14 | 14 | ProjectAttributeType, |
15 | 15 | ) |
16 | | -from coldfront.core.project.utils import generate_project_code |
| 16 | +from coldfront.core.project.utils import ( |
| 17 | + determine_automated_institution_choice, |
| 18 | + generate_project_code, |
| 19 | +) |
17 | 20 | from coldfront.core.test_helpers.factories import ( |
18 | 21 | FieldOfScienceFactory, |
19 | 22 | PAttributeTypeFactory, |
@@ -276,3 +279,103 @@ def test_different_prefix_padding(self): |
276 | 279 | # Test the generated project codes |
277 | 280 | self.assertEqual(project_with_code_padding1, "BFO001") |
278 | 281 | self.assertEqual(project_with_code_padding2, "BFO002") |
| 282 | + |
| 283 | + |
| 284 | +class TestInstitution(TestCase): |
| 285 | + def setUp(self): |
| 286 | + self.user = UserFactory(username="capeo") |
| 287 | + self.field_of_science = FieldOfScienceFactory(description="Physics") |
| 288 | + self.status = ProjectStatusChoiceFactory(name="Active") |
| 289 | + |
| 290 | + def create_project_with_institution(self, title, institution_dict=None): |
| 291 | + """Helper method to create a project and assign a institution value based on the argument passed""" |
| 292 | + # Project Creation |
| 293 | + project = Project.objects.create( |
| 294 | + title=title, |
| 295 | + pi=self.user, |
| 296 | + status=self.status, |
| 297 | + field_of_science=self.field_of_science, |
| 298 | + ) |
| 299 | + |
| 300 | + if institution_dict: |
| 301 | + determine_automated_institution_choice(project, institution_dict) |
| 302 | + |
| 303 | + project.save() |
| 304 | + |
| 305 | + return project.institution |
| 306 | + |
| 307 | + @patch( |
| 308 | + "coldfront.config.core.PROJECT_INSTITUTION_EMAIL_MAP", |
| 309 | + {"inst.ac.com": "AC", "inst.edu.com": "EDU", "bfo.ac.uk": "BFO"}, |
| 310 | + ) |
| 311 | + def test_institution_is_none(self): |
| 312 | + from coldfront.config.core import PROJECT_INSTITUTION_EMAIL_MAP |
| 313 | + |
| 314 | + """Test to check if institution is none after both env vars are enabled. """ |
| 315 | + |
| 316 | + # Create project with both institution |
| 317 | + project_institution = self.create_project_with_institution("Project 1", PROJECT_INSTITUTION_EMAIL_MAP) |
| 318 | + |
| 319 | + # Create the first project |
| 320 | + self.assertEqual(project_institution, "None") |
| 321 | + |
| 322 | + @patch( |
| 323 | + "coldfront.config.core.PROJECT_INSTITUTION_EMAIL_MAP", |
| 324 | + {"inst.ac.com": "AC", "inst.edu.com": "EDU", "bfo.ac.uk": "BFO"}, |
| 325 | + ) |
| 326 | + def test_institution_multiple_users(self): |
| 327 | + from coldfront.config.core import PROJECT_INSTITUTION_EMAIL_MAP |
| 328 | + |
| 329 | + """Test to check multiple projects with different user email addresses, """ |
| 330 | + |
| 331 | + # Create project for user 1 |
| 332 | + self.user.email = "user@inst.ac.com" |
| 333 | + self.user.save() |
| 334 | + project_institution_one = self.create_project_with_institution("Project 1", PROJECT_INSTITUTION_EMAIL_MAP) |
| 335 | + self.assertEqual(project_institution_one, "AC") |
| 336 | + |
| 337 | + # Create project for user 2 |
| 338 | + self.user.email = "user@bfo.ac.uk" |
| 339 | + self.user.save() |
| 340 | + project_institution_two = self.create_project_with_institution("Project 2", PROJECT_INSTITUTION_EMAIL_MAP) |
| 341 | + self.assertEqual(project_institution_two, "BFO") |
| 342 | + |
| 343 | + # Create project for user 3 |
| 344 | + self.user.email = "user@inst.edu.com" |
| 345 | + self.user.save() |
| 346 | + project_institution_three = self.create_project_with_institution("Project 3", PROJECT_INSTITUTION_EMAIL_MAP) |
| 347 | + self.assertEqual(project_institution_three, "EDU") |
| 348 | + |
| 349 | + @patch( |
| 350 | + "coldfront.config.core.PROJECT_INSTITUTION_EMAIL_MAP", |
| 351 | + {"inst.ac.com": "AC", "inst.edu.com": "EDU", "bfo.ac.uk": "BFO"}, |
| 352 | + ) |
| 353 | + def test_determine_automated_institution_choice_does_not_save_to_database(self): |
| 354 | + from coldfront.config.core import PROJECT_INSTITUTION_EMAIL_MAP |
| 355 | + |
| 356 | + """Test that the function only modifies project in memory, not in database""" |
| 357 | + |
| 358 | + self.user.email = "user@inst.ac.com" |
| 359 | + self.user.save() |
| 360 | + |
| 361 | + # Create project, similar to create_project_with_institution, but without the save function. |
| 362 | + project = Project.objects.create( |
| 363 | + title="Test Project", |
| 364 | + pi=self.user, |
| 365 | + status=self.status, |
| 366 | + field_of_science=self.field_of_science, |
| 367 | + institution="Default", |
| 368 | + ) |
| 369 | + |
| 370 | + original_db_project = Project.objects.get(id=project.id) |
| 371 | + self.assertEqual(original_db_project.institution, "Default") |
| 372 | + |
| 373 | + # Call the function and check object was modified in memory. |
| 374 | + determine_automated_institution_choice(project, PROJECT_INSTITUTION_EMAIL_MAP) |
| 375 | + self.assertEqual(project.institution, "AC") |
| 376 | + |
| 377 | + # Check that database was NOT modified |
| 378 | + current_db_project = Project.objects.get(id=project.id) |
| 379 | + self.assertEqual(original_db_project.institution, "Default") |
| 380 | + |
| 381 | + self.assertNotEqual(project.institution, current_db_project.institution) |
0 commit comments