Index: main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_student.py
===================================================================
--- main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_student.py	(revision 8402)
+++ main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_student.py	(revision 8403)
@@ -18,16 +18,23 @@
 """Tests for students and related.
 """
+import os
+import re
+from cStringIO import StringIO
 from datetime import tzinfo
+from zope.component import getUtility
 from zope.component.interfaces import IFactory
 from zope.interface import verify
-from waeup.kofa.students.student import Student, StudentFactory
+from waeup.kofa.interfaces import IExtFileStore, IFileStoreNameChooser
+from waeup.kofa.students.export import EXPORTER_NAMES
+from waeup.kofa.students.student import (
+    Student, StudentFactory, handle_student_removed)
 from waeup.kofa.students.studycourse import StudentStudyCourse
 from waeup.kofa.students.studylevel import StudentStudyLevel, CourseTicket
 from waeup.kofa.students.payments import StudentPaymentsContainer
 from waeup.kofa.students.accommodation import StudentAccommodation, BedTicket
-from waeup.kofa.applicants.interfaces import IApplicantBaseData
 from waeup.kofa.students.interfaces import (
-    IStudent, IStudentStudyCourse, IStudentPaymentsContainer, IStudentAccommodation,
-    IStudentStudyLevel, ICourseTicket, IBedTicket)
+    IStudent, IStudentStudyCourse, IStudentPaymentsContainer,
+    IStudentAccommodation, IStudentStudyLevel, ICourseTicket, IBedTicket)
+from waeup.kofa.students.tests.test_batching import StudentImportExportSetup
 from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase
 from waeup.kofa.university.department import Department
@@ -83,4 +90,103 @@
         return
 
+class StudentRemovalTests(StudentImportExportSetup):
+    # Test handle_student_removed
+    #
+    # This is a complex action updating several CSV files and moving
+    # stored files to a backup location.
+    #
+    # These tests make no assumptions about the CSV files except that
+    # they contain a deletion timestamp at end of each data row
+
+    layer = FunctionalLayer
+
+    def setUp(self):
+        super(StudentRemovalTests, self).setUp()
+        self.setup_for_export()
+        return
+
+    def create_passport_img(self, student):
+        # create some faked passport file for `student`
+        storage = getUtility(IExtFileStore)
+        file_id = IFileStoreNameChooser(student).chooseName(
+            attr='passport.jpg')
+        storage.createFile(file_id, StringIO('I am a fake image.'))
+
+    def test_backup_single_student_data(self):
+        # when a single student is removed, the data is backed up.
+        self.setup_student(self.student)
+        # Add a fake image
+        self.create_passport_img(self.student)
+        handle_student_removed(self.student, None)
+        del_dir = self.app['datacenter'].deleted_path
+        del_img_path = os.path.join(
+            del_dir, 'media', 'students', 'A', 'A111111',
+            'passport_A111111.jpg')
+
+        # The image was copied over
+        self.assertTrue(os.path.isfile(del_img_path))
+        self.assertEqual(
+            open(del_img_path, 'rb').read(),
+            'I am a fake image.')
+
+        # The student data were put into CSV files
+        for name in EXPORTER_NAMES:
+            csv_path = os.path.join(del_dir, '%s.csv' % name)
+            self.assertTrue(os.path.isfile(csv_path))
+            contents = open(csv_path, 'rb').read().split('\r\n')
+            # We expect 3 lines output including a linebreak at end of file.
+            self.assertEqual(len(contents), 3)
+        return
+
+    def test_backup_append_csv(self):
+        # when several students are removed, existing CSVs are appended
+        self.setup_student(self.student)
+        # Add a fake image
+        self.create_passport_img(self.student)
+        del_dir = self.app['datacenter'].deleted_path
+        # put fake data into students.csv with trailing linebreak
+        students_csv = os.path.join(del_dir, 'students.csv')
+        open(students_csv, 'wb').write('line1\r\nline2\r\n')
+        handle_student_removed(self.student, None)
+        contents = open(students_csv, 'rb').read().split('\r\n')
+        # there should be 4 lines in result csv (including trailing linebreak)
+        self.assertEqual(len(contents), 4)
+        return
+
+    def test_old_files_removed(self):
+        # make sure old files are not accessible any more
+        self.setup_student(self.student)
+        # Add a fake image
+        self.create_passport_img(self.student)
+        # make sure we can access the image before removal
+        file_store = getUtility(IExtFileStore)
+        image = file_store.getFileByContext(self.student, attr='passport.jpg')
+        self.assertTrue(image is not None)
+
+        # remove image (hopefully)
+        handle_student_removed(self.student, None)
+
+        # the is not accessible anymore
+        image = file_store.getFileByContext(self.student, attr='passport.jpg')
+        self.assertEqual(image, None)
+        return
+
+    def test_csv_file_entries_have_timestamp(self):
+        # each row in written csv files has a ``del_date`` column to
+        # tell when the associated student was deleted
+        self.setup_student(self.student)
+        del_dir = self.app['datacenter'].deleted_path
+        students_csv = os.path.join(del_dir, 'students.csv')
+        handle_student_removed(self.student, None)
+        contents = open(students_csv, 'rb').read().split('\r\n')
+        # the CSV header ends with a ``del_date`` column
+        self.assertTrue(contents[0].endswith(',del_date'))
+        # each line ends with an UTC timestamp
+        timestamp = contents[1][-23:]
+        self.assertTrue(re.match(
+            '^\d\d-\d\d-\d\d \d\d:\d\d:\d\d\+00:00$', timestamp))
+        return
+
+
 class StudentFactoryTest(FunctionalTestCase):
 
