def test_no_expired_data_download(self):
        """management command doesn't error when no data is expired"""
        out = StringIO()
        call_command(expireuserdatadownloads.Command(), stdout=out)
        command_output = out.getvalue().splitlines()[0].strip()

        self.assertEqual(command_output, "Data downloads expired: 0")
    def test_skip_expired_data_download(self):
        """management command skips pending data downloads"""
        data_download = request_user_data_download(self.user)
        data_download.status = DataDownload.STATUS_EXPIRED
        data_download.save()

        out = StringIO()
        call_command(expireuserdatadownloads.Command(), stdout=out)
        command_output = out.getvalue().splitlines()[0].strip()

        self.assertEqual(command_output, "Data downloads expired: 0")

        updated_data_download = DataDownload.objects.get(pk=data_download.pk)
        self.assertEqual(updated_data_download.status, DataDownload.STATUS_EXPIRED)
    def test_delete_expired_data_download(self):
        """management command deletes expired data download"""
        data_download = request_user_data_download(self.user)
        data_download.status = DataDownload.STATUS_READY
        with open(TEST_FILE_PATH, 'rb') as download_file:
            data_download.file = File(download_file)
            data_download.save()

        out = StringIO()
        call_command(expireuserdatadownloads.Command(), stdout=out)
        command_output = out.getvalue().splitlines()[0].strip()

        self.assertEqual(command_output, "Data downloads expired: 1")

        updated_data_download = DataDownload.objects.get(pk=data_download.pk)
        self.assertEqual(updated_data_download.status, DataDownload.STATUS_EXPIRED)
        self.assertFalse(updated_data_download.file)
    def test_skip_not_expired_data_download(self):
        """management command skips data download that expires in future"""
        data_download = request_user_data_download(self.user)
        data_download.status = DataDownload.STATUS_READY
        data_download.expires_on += timedelta(hours=1)
        with open(TEST_FILE_PATH, 'rb') as download_file:
            data_download.file = File(download_file)
            data_download.save()

        out = StringIO()
        call_command(expireuserdatadownloads.Command(), stdout=out)
        command_output = out.getvalue().splitlines()[0].strip()

        self.assertEqual(command_output, "Data downloads expired: 0")

        updated_data_download = DataDownload.objects.get(pk=data_download.pk)
        self.assertEqual(updated_data_download.status, DataDownload.STATUS_READY)
        self.assertTrue(updated_data_download.file)