Exemplo n.º 1
0
 def test_post_token_created(self):
     create_user('testuser', email='*****@*****.**')
     self.assertEqual(GenericTokenWithMetadata.objects.count(), 0)
     self.client.post(self.url, {'email': '*****@*****.**'})
     self.assertEqual(GenericTokenWithMetadata.objects.count(), 1)
     token = GenericTokenWithMetadata.objects.first()
     self.assertIsNotNone(token.expiration_datetime)
Exemplo n.º 2
0
    def test_post_user_found(self):
        create_user('testuser', email='*****@*****.**')
        with mock.patch.object(BeginPasswordResetView, '_generate_token', lambda s, user: '******'):
            with self.settings(CRADMIN_LEGACY_SITENAME='Testsite'):
                response = self.client.post(self.url, {'email': '*****@*****.**'})
        self.assertRedirects(response, reverse('cradmin-resetpassword-email-sent'))

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, 'Reset your Testsite password')
        self.assertIn('http://testserver/cradmin_resetpassword/reset/testtoken',
                      mail.outbox[0].alternatives[0][0])
        self.assertIn('We received a request to reset the password for your '
                      'Testsite account, testuser',
                      mail.outbox[0].alternatives[0][0])
Exemplo n.º 3
0
 def test_post_email_not_unique(self):
     create_user('testuser', email='*****@*****.**')
     response = self.client.post(
         self.url, {
             'username': '******',
             'password1': 'test',
             'password2': 'test',
             'email': '*****@*****.**'
         })
     self.assertEqual(response.status_code, 200)
     selector = htmls.S(response.content)
     self.assertIn(
         'Account with this email address already exists',
         selector.one('form#cradmin_legacy_register_account_form').
         alltext_normalized)
    def test_uploadform_post_multiple_images_fails(self):
        testuser = create_user('testuser')
        testimage1 = create_image(200, 100)
        testimage2 = create_image(100, 100)
        collection = TemporaryFileCollection.objects.create(user=testuser)
        temporaryfile = TemporaryFile(collection=collection,
                                      filename='testfile1.png')
        temporaryfile.file.save('testfile1.png', ContentFile(testimage1))
        temporaryfile = TemporaryFile(collection=collection,
                                      filename='testfile2.png')
        temporaryfile.file.save('testfile2.png', ContentFile(testimage2))

        testrole = baker.make('cradmin_legacy_testapp.TstRole')
        cradmin_app = mock.MagicMock()
        cradmin_app.reverse_appurl.return_value = '/success'

        self.assertEqual(ArchiveImage.objects.count(), 0)
        mockresponse = self.mock_http200_postrequest_htmls(
            requestuser=testuser,
            cradmin_role=testrole,
            cradmin_app=cradmin_app,
            requestkwargs={'data': {
                'filecollectionid': collection.id
            }})
        self.assertEqual(ArchiveImage.objects.count(), 0)
        self.assertTrue(
            mockresponse.selector.exists('#div_id_filecollectionid.has-error'))
        self.assertIn(
            'You must upload exactly one image.',
            mockresponse.selector.one(
                '#div_id_filecollectionid').alltext_normalized)
    def test_uploadform_post_single_image(self):
        testuser = create_user('testuser')
        testimage = create_image(200, 100)
        collection = TemporaryFileCollection.objects.create(user=testuser)
        temporaryfile = TemporaryFile(collection=collection,
                                      filename='testfile.png')
        temporaryfile.file.save('testfile.png', ContentFile(testimage))

        testrole = baker.make('cradmin_legacy_testapp.TstRole')
        cradmin_app = mock.MagicMock()
        self.assertEqual(ArchiveImage.objects.count(), 0)
        mockresponse = self.mock_http302_postrequest(
            requestuser=testuser,
            cradmin_role=testrole,
            cradmin_app=cradmin_app,
            requestkwargs={'data': {
                'filecollectionid': collection.id
            }})

        self.assertEqual(ArchiveImage.objects.count(), 1)
        created_image = ArchiveImage.objects.first()
        self.assertTrue(mockresponse.response['Location'].endswith(
            '?foreignkey_selected_value={}'.format(created_image.pk)))
        self.assertEqual(created_image.image.read(), testimage)
        self.assertEqual(created_image.name, 'testfile.png')
        self.assertEqual(created_image.description, '')
Exemplo n.º 6
0
    def test_get_render_is_authenticated(self):
        request = self.factory.get('/test')
        request.user = create_user('testuser')
        token = self.__create_token()
        with self.settings(CRADMIN_LEGACY_SITENAME='Testsite'):
            response = AcceptInviteView.as_view()(request, token=token.token)
        self.assertEqual(response.status_code, 200)
        response.render()
        selector = htmls.S(response.content)
        self.assertTrue(selector.exists('button#cradmin_legacy_invite_accept_as_button'))
        self.assertEqual(
            selector.one('#cradmin_legacy_invite_accept_register_account_button')['href'],
            '/cradmin_register_account/begin?next=http%3A%2F%2Ftestserver%2Ftest')
        self.assertEqual(
            selector.one('#cradmin_legacy_invite_accept_login_as_different_user_button')['href'],
            '/cradmin_authenticate/logout?'
            'next=%2Faccounts%2Flogin%2F%3Fnext%3Dhttp%253A%252F%252Ftestserver%252Ftest')

        self.assertEqual(
            selector.one('button#cradmin_legacy_invite_accept_as_button').alltext_normalized,
            'Accept as testuser')
        self.assertEqual(
            selector.one('#cradmin_legacy_invite_accept_register_account_button').alltext_normalized,
            'Sign up for Testsite')
        self.assertEqual(
            selector.one('#cradmin_legacy_invite_accept_login_as_different_user_button').alltext_normalized,
            'Sign in as another user')
Exemplo n.º 7
0
 def clean_validates_accept(self):
     collection = TemporaryFileCollection.objects.create(
         user=create_user('testuser'), accept='image/png,image/jpeg')
     TemporaryFile(filename='test.png', collection=collection).clean()
     TemporaryFile(filename='test.jpg', collection=collection).clean()
     with self.assertRaises(ValidationError):
         TemporaryFile(filename='test.txt', collection=collection).clean()
    def test_uploadform_post_multiple_images(self):
        testuser = create_user('testuser')
        testimage1 = create_image(200, 100)
        testimage2 = create_image(100, 100)
        collection = TemporaryFileCollection.objects.create(user=testuser)
        temporaryfile = TemporaryFile(collection=collection,
                                      filename='testfile1.png')
        temporaryfile.file.save('testfile1.png', ContentFile(testimage1))
        temporaryfile = TemporaryFile(collection=collection,
                                      filename='testfile2.png')
        temporaryfile.file.save('testfile2.png', ContentFile(testimage2))

        testrole = baker.make('cradmin_legacy_testapp.TstRole')
        cradmin_app = mock.MagicMock()
        cradmin_app.reverse_appurl.return_value = '/success'

        self.assertEqual(ArchiveImage.objects.count(), 0)
        mockresponse = self.mock_http302_postrequest(
            requestuser=testuser,
            cradmin_role=testrole,
            cradmin_app=cradmin_app,
            requestkwargs={'data': {
                'filecollectionid': collection.id
            }})
        self.assertEqual(mockresponse.response['Location'], '/success')
        self.assertEqual(ArchiveImage.objects.count(), 2)
        created_images = ArchiveImage.objects.order_by('name')

        self.assertEqual(created_images[0].image.read(), testimage1)
        self.assertEqual(created_images[0].name, 'testfile1.png')
        self.assertEqual(created_images[0].description, '')

        self.assertEqual(created_images[1].image.read(), testimage2)
        self.assertEqual(created_images[1].name, 'testfile2.png')
        self.assertEqual(created_images[1].description, '')
 def test_no_archive_images(self):
     mockresponse = self.mock_http200_getrequest_htmls(
         requestuser=create_user('testuser'),
         cradmin_role=baker.make('cradmin_legacy_testapp.TstRole'))
     self.assertEqual(
         mockresponse.selector.one(
             '#objecttableview-no-items-message').alltext_normalized,
         'No archive images')
Exemplo n.º 10
0
 def __create_token(self, metadata=None, expiration_datetime=None, **kwargs):
     generic_token_with_metadata = GenericTokenWithMetadata(
         created_datetime=timezone.now(),
         expiration_datetime=(expiration_datetime or (timezone.now() + timedelta(days=2))),
         content_object=create_user('invitecontentobject'),
         app='testapp',
         **kwargs)
     generic_token_with_metadata.metadata = metadata or {}
     generic_token_with_metadata.save()
     return generic_token_with_metadata
Exemplo n.º 11
0
 def test_uploadform_post_no_filecollectionid(self):
     self.assertEqual(ArchiveImage.objects.count(), 0)
     testrole = baker.make('cradmin_legacy_testapp.TstRole')
     mockresponse = self.mock_http200_postrequest_htmls(
         requestuser=create_user('testuser'), cradmin_role=testrole)
     self.assertTrue(
         mockresponse.selector.exists('#div_id_filecollectionid.has-error'))
     self.assertIn(
         'You must upload an image.',
         mockresponse.selector.one(
             '#div_id_filecollectionid').alltext_normalized)
Exemplo n.º 12
0
 def setUp(self):
     self.factory = RequestFactory()
     self.role = TstRole.objects.create()
     self.archiveimage = ArchiveImage.objects.create(
         role=self.role,
         name='Original name',
         description='Original description')
     self.testimage = create_image(200, 100)
     self.archiveimage.image.save('testimage.png',
                                  ContentFile(self.testimage))
     self.testuser = create_user('testuser')
Exemplo n.º 13
0
 def test_only_owned_by_role(self):
     testrole = baker.make('cradmin_legacy_testapp.TstRole')
     other_role = baker.make('cradmin_legacy_testapp.TstRole')
     baker.make('cradmin_imagearchive.ArchiveImage',
                name='Test image',
                description='',
                role=other_role)
     mockresponse = self.mock_http200_getrequest_htmls(
         requestuser=create_user('testuser'), cradmin_role=testrole)
     self.assertTrue(
         mockresponse.selector.exists('#objecttableview-no-items-message'))
Exemplo n.º 14
0
 def test_delete_form_collection_not_owned_by_user(self):
     collection = TemporaryFileCollection.objects.create(user=self.testuser)
     request = self.factory.delete('/test', json.dumps({
         'collectionid': collection.id,
         'temporaryfileid': '1'  # NOTE: This is ignored since we never get to the code looking it up
     }))
     request.user = create_user('otheruser')
     response = UploadTemporaryFilesView.as_view()(request)
     self.assertEqual(response.status_code, 404)
     responsedata = json.loads(response.content.decode('utf-8'))
     self.assertEqual(responsedata['collectionid'][0]['code'], 'doesnotexist')
Exemplo n.º 15
0
 def test_clear_collection(self):
     collection = TemporaryFileCollection.objects.create(
         user=create_user('testuser'))
     temporaryfile = TemporaryFile(filename='test.txt',
                                   collection=collection)
     temporaryfile.file.save('test.txt', ContentFile('Testdata'))
     physical_file_path = temporaryfile.file.path
     self.assertTrue(os.path.exists(physical_file_path))
     collection.clear_files()
     self.assertFalse(os.path.exists(physical_file_path))
     self.assertEqual(collection.files.count(), 0)
Exemplo n.º 16
0
 def test_post_form_collection_not_owned_by_user(self):
     collection = TemporaryFileCollection.objects.create(user=self.testuser)
     request = self.factory.post('/test', {
         'file': SimpleUploadedFile('testfile1.txt', b'Test1'),
         'collectionid': collection.id
     })
     request.user = create_user('otheruser')
     response = UploadTemporaryFilesView.as_view()(request)
     self.assertEqual(response.status_code, 404)
     responsedata = json.loads(response.content.decode('utf-8'))
     self.assertEqual(responsedata['collectionid'][0]['code'], 'doesnotexist')
Exemplo n.º 17
0
 def test_delete_removes_physical_file(self):
     collection = TemporaryFileCollection.objects.create(
         user=create_user('testuser'))
     temporaryfile = TemporaryFile(filename='test.txt',
                                   collection=collection)
     temporaryfile.file.save('test.txt', ContentFile('Testdata'))
     physical_file_path = temporaryfile.file.path
     self.assertTrue(os.path.exists(physical_file_path))
     temporaryfile.file.delete()
     temporaryfile.delete()
     self.assertFalse(os.path.exists(physical_file_path))
Exemplo n.º 18
0
 def test_single_archive_image_sanity(self):
     testrole = baker.make('cradmin_legacy_testapp.TstRole')
     baker.make('cradmin_imagearchive.ArchiveImage',
                name='Test image',
                description='',
                role=testrole)
     mockresponse = self.mock_http200_getrequest_htmls(
         requestuser=create_user('testuser'), cradmin_role=testrole)
     self.assertFalse(
         mockresponse.selector.exists('#objecttableview-no-items-message'))
     self.assertEqual(
         mockresponse.selector.count('#objecttableview-table tbody tr'), 1)
Exemplo n.º 19
0
 def test_uploadform_get_render(self):
     testrole = baker.make('cradmin_legacy_testapp.TstRole')
     mockresponse = self.mock_http200_getrequest_htmls(
         requestuser=create_user('testuser'), cradmin_role=testrole)
     self.assertTrue(
         mockresponse.selector.exists(
             '#cradmin_legacy_imagearchive_bulkadd_form'))
     self.assertTrue(
         mockresponse.selector.exists(
             'input[type=hidden][name=filecollectionid]'))
     self.assertTrue(
         mockresponse.selector.exists('#div_id_filecollectionid'))
Exemplo n.º 20
0
 def test_get_filename_set(self):
     collection = TemporaryFileCollection.objects.create(
         user=create_user('testuser'))
     temporaryfile1 = TemporaryFile(filename='test.txt',
                                    collection=collection)
     temporaryfile1.file.save('x', ContentFile('Testdata'))
     temporaryfile2 = TemporaryFile(filename='test2.txt',
                                    collection=collection)
     temporaryfile2.file.save('y', ContentFile('Testdata'))
     temporaryfile3 = TemporaryFile(filename='test.txt',
                                    collection=collection)
     temporaryfile3.file.save('z', ContentFile('Testdata'))
     self.assertEqual(collection.get_filename_set(),
                      {'test.txt', 'test2.txt'})
Exemplo n.º 21
0
    def test_singlemode_keeps_only_last_file_do_not_delete_last(self):
        collection = TemporaryFileCollection.objects.create(
            user=create_user('testuser'), singlemode=True)

        last_added_temporary_file = TemporaryFile(filename='test2.txt',
                                                  collection=collection)
        last_added_temporary_file.file.save('test1.txt',
                                            ContentFile('Testdata'),
                                            save=False)
        last_added_temporary_file.clean()
        last_added_temporary_file.save()
        last_added_temporary_file.clean()
        self.assertEqual(collection.files.count(), 1)
        self.assertEqual(collection.files.first(), last_added_temporary_file)
Exemplo n.º 22
0
 def test_render_single_archive_image_has_description(self):
     testrole = baker.make('cradmin_legacy_testapp.TstRole')
     baker.make('cradmin_imagearchive.ArchiveImage',
                name='Test image',
                description='Test description',
                role=testrole)
     mockresponse = self.mock_http200_getrequest_htmls(
         requestuser=create_user('testuser'), cradmin_role=testrole)
     self.assertEqual(
         mockresponse.selector.one(
             '#objecttableview-table tbody tr td:last-child .objecttable-cellvalue'
         ).alltext_normalized, 'Test description')
     self.assertEqual(
         mockresponse.selector.one(
             '#objecttableview-table tbody tr td:last-child .btn-default').
         alltext_normalized, 'Edit description')
Exemplo n.º 23
0
    def test_uploadform_post_deletes_collection(self):
        testuser = create_user('testuser')
        testimage = create_image(200, 100)
        collection = TemporaryFileCollection.objects.create(user=testuser)
        temporaryfile = TemporaryFile(collection=collection,
                                      filename='testfile.png')
        temporaryfile.file.save('testfile.png', ContentFile(testimage))

        testrole = baker.make('cradmin_legacy_testapp.TstRole')
        self.mock_http302_postrequest(
            requestuser=testuser,
            cradmin_role=testrole,
            requestkwargs={'data': {
                'filecollectionid': collection.id
            }})
        self.assertFalse(
            TemporaryFileCollection.objects.filter(id=collection.id).exists())
Exemplo n.º 24
0
    def test_single_archive_image_sanity(self):
        testrole = baker.make('cradmin_legacy_testapp.TstRole')
        archiveimage = baker.make('cradmin_imagearchive.ArchiveImage',
                                  role=testrole)
        archiveimage.image.save('testimage.png',
                                ContentFile(create_image(200, 100)))

        mockresponse = self.mock_http200_getrequest_htmls(
            requestuser=create_user('testuser'),
            cradmin_role=testrole,
            requestkwargs={'data': {
                'foreignkey_select_fieldid': 'testfield'
            }})
        self.assertFalse(
            mockresponse.selector.exists('#objecttableview-no-items-message'))
        self.assertEqual(
            mockresponse.selector.count('#objecttableview-table tbody tr'), 1)
Exemplo n.º 25
0
    def test_uploadform_post_sets_file_size(self):
        testuser = create_user('testuser')
        testimage = create_image(200, 100)
        collection = TemporaryFileCollection.objects.create(user=testuser)
        temporaryfile = TemporaryFile(collection=collection,
                                      filename='testfile.png')
        temporaryfile.file.save('testfile.png', ContentFile(testimage))

        testrole = baker.make('cradmin_legacy_testapp.TstRole')
        self.mock_http302_postrequest(
            requestuser=testuser,
            cradmin_role=testrole,
            requestkwargs={'data': {
                'filecollectionid': collection.id
            }})
        created_image = ArchiveImage.objects.first()
        self.assertEqual(len(testimage), created_image.file_size)
Exemplo n.º 26
0
    def test_singlemode_keeps_only_last_file_delete_physical_file(self):
        collection = TemporaryFileCollection.objects.create(
            user=create_user('testuser'), singlemode=True)

        first_added_temporary_file = TemporaryFile(filename='test1.txt',
                                                   collection=collection)
        first_added_temporary_file.file.save('test1.txt',
                                             ContentFile('Testdata'),
                                             save=False)
        first_added_temporary_file.clean()
        first_added_temporary_file.save()
        first_added_physical_file_path = first_added_temporary_file.file.path
        self.assertTrue(os.path.exists(first_added_physical_file_path))

        last_added_temporary_file = TemporaryFile(filename='test2.txt',
                                                  collection=collection)
        last_added_temporary_file.file.save('test1.txt',
                                            ContentFile('Testdata'),
                                            save=False)
        last_added_temporary_file.clean()
        self.assertFalse(os.path.exists(first_added_physical_file_path))
Exemplo n.º 27
0
 def test_render_single_archive_image_has_description(self):
     testrole = baker.make('cradmin_legacy_testapp.TstRole')
     archiveimage = baker.make('cradmin_imagearchive.ArchiveImage',
                               name='Test image',
                               description='Test description',
                               role=testrole)
     archiveimage.image.save('testimage.png',
                             ContentFile(create_image(200, 100)))
     mockresponse = self.mock_http200_getrequest_htmls(
         requestuser=create_user('testuser'),
         cradmin_role=testrole,
         requestkwargs={'data': {
             'foreignkey_select_fieldid': 'testfield'
         }})
     self.assertEqual(
         mockresponse.selector.one(
             '#objecttableview-table tbody tr td:last-child .objecttable-cellvalue'
         ).alltext_normalized, 'Test description')
     self.assertEqual(
         mockresponse.selector.one(
             '#objecttableview-table tbody tr td:last-child .btn-default').
         alltext_normalized, 'Use this')
Exemplo n.º 28
0
 def setUp(self):
     self.factory = RequestFactory()
     self.testuser = create_user('testuser')
Exemplo n.º 29
0
    def setUp(self):
        self.testuser = create_user('testuser', email='*****@*****.**')

        # Any object will work as the target of invites.
        # We use a user object since we have that easily available
        self.invite_target = create_user('invitetarget')
Exemplo n.º 30
0
 def setUp(self):
     self.testuser = create_user('testuser', email='*****@*****.**')