Exemplo n.º 1
0
 def setUp(self):
     account = Account.create(
         **{
             'username': '******',
             'email': '*****@*****.**',
             'password': '******'
         })
     account.save()
     response = self.client.post('/api/v1/login',
                                 data={
                                     'username': '******',
                                     'password': '******'
                                 })
     self.auth_token_header = {
         'HTTP_AUTHORIZATION': 'Token {}'.format(response.json().get('key'))
     }
     self.backup = Backup(
         **{
             'account': account,
             'digest':
             'somedigestsomedigestsomedigestsomedigestsomedigestsomedigestsomedigestsomedigestsomedigestsomedigestsomedigestsomedigestmedigest',
             'timestamp': self.timestamp,
             'backup': 'encoded backup data'
         })
     self.backup.save()
Exemplo n.º 2
0
 def post(request):
     serializer = BackupSerializer(data=request.data,
                                   context={'request': request})
     if serializer.is_valid():
         serializer.save()
         account = Account.get_by_pk(request.user.username)
         backups = Backup.objects.all().order_by('timestamp')
         if len(backups) > account.max_backups:
             Backup.remove(backups[0].digest)
         return Response({'detail': 'backup has been created'},
                         status=status.HTTP_201_CREATED)
     else:
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 3
0
 def setUp(self):
     account = Account.create(
         **{
             'username': '******',
             'email': '*****@*****.**',
             'password': '******'
         })
     account.save()
     response = self.client.post('/api/v1/login',
                                 data={
                                     'username': '******',
                                     'password': '******'
                                 })
     self.auth_token_header = {
         'HTTP_AUTHORIZATION': 'Token {}'.format(response.json().get('key'))
     }
     account_1 = Account.create(
         **{
             'username': '******',
             'email': '*****@*****.**',
             'password': '******'
         })
     account_1.save()
     backup = Backup(
         **{
             'account': account,
             'digest': 'somedigest',
             'timestamp': self.timestamp,
             'backup': 'encoded backup data'
         })
     backup.save()
     backup_1 = Backup(
         **{
             'account': account,
             'digest': 'somedigest_1',
             'timestamp': self.timestamp,
             'backup': 'encoded backup data 1'
         })
     backup_1.save()
     Backup(
         **{
             'account': account_1,
             'digest': 'somedigest_2',
             'timestamp': self.timestamp,
             'backup': 'encoded backup data 2'
         }).save()
     self.expected_list = [{
         'digest': backup_1.digest,
         'timestamp': backup_1.timestamp
     }, {
         'digest': backup.digest,
         'timestamp': backup.timestamp
     }]
Exemplo n.º 4
0
 def setUp(self):
     self.account = Account(
         **{
             'username': '******',
             'email': '*****@*****.**',
             'password': '******'
         })
     self.account.save()
     self.expected_backup = Backup(
         **{
             'digest': 'super_hash',
             'timestamp': self.timestamp,
             'backup': 'backup content',
             'account': self.account
         })
     self.expected_backup.save()
Exemplo n.º 5
0
 async def get(self):
     try:
         backups = await self.get_objects()
     except Exception as e:
         data = {'error': str(e)}
     else:
         data = {'data': Backup.dump_many(backups).data}
     self.write(data)
Exemplo n.º 6
0
 def test_get_by_pk_exists(self):
     actual_backup = Backup.get_by_pk('super_hash')
     self.assertIsNotNone(actual_backup)
     self.assertEqual(self.expected_backup.account, actual_backup.account)
     self.assertEqual(self.expected_backup.digest, actual_backup.digest)
     self.assertEqual(self.expected_backup.timestamp,
                      actual_backup.timestamp)
     self.assertEqual(self.expected_backup.backup, actual_backup.backup)
Exemplo n.º 7
0
class BackupDetailsViewTestCase(TestCase):
    timestamp = now()

    def setUp(self):
        account = Account.create(
            **{
                'username': '******',
                'email': '*****@*****.**',
                'password': '******'
            })
        account.save()
        response = self.client.post('/api/v1/login',
                                    data={
                                        'username': '******',
                                        'password': '******'
                                    })
        self.auth_token_header = {
            'HTTP_AUTHORIZATION': 'Token {}'.format(response.json().get('key'))
        }
        self.backup = Backup(
            **{
                'account': account,
                'digest':
                'somedigestsomedigestsomedigestsomedigestsomedigestsomedigestsomedigestsomedigestsomedigestsomedigestsomedigestsomedigestmedigest',
                'timestamp': self.timestamp,
                'backup': 'encoded backup data'
            })
        self.backup.save()

    def test_get_200_ok(self):
        response = self.client.get(
            '/api/v1/backups/details/{}'.format(self.backup.digest),
            **self.auth_token_header)
        self.assertEqual(response.status_code, 200)
        content = response.json()
        self.assertEqual(content['digest'], self.backup.digest)
        self.assertEqual(content['backup'], self.backup.backup)
        self.assertEqual(content['timestamp'],
                         self.backup.timestamp.strftime('%Y-%m-%d %H:%M:%S'))

    def test_get_404_not_found(self):
        response = self.client.get(
            '/api/v1/backups/details/{}'.format('f' + self.backup.digest[1:]),
            **self.auth_token_header)
        self.assertEqual(response.status_code, 404)
Exemplo n.º 8
0
 def get(request, pk_hash):
     if pk_hash is None:
         return Response({'details': 'primary key is not provided'},
                         status=status.HTTP_400_BAD_REQUEST)
     backup = Backup.get_by_pk(pk_hash)
     if backup is None:
         return Response(status=status.HTTP_404_NOT_FOUND)
     serializer = BackupSerializer(backup)
     return Response(serializer.data, status=status.HTTP_200_OK)
Exemplo n.º 9
0
 def test_post_201_created_max_backups_reached(self):
     Backup(
         **{
             'account': self.account,
             'digest': 'some_digest_4',
             'timestamp': now(),
             'backup': 'encoded backup data 4'
         }).save()
     self.assertIsNotNone(Backup.get_by_pk('some_digest_0'))
     response = self.client.post('/api/v1/backups/create',
                                 data={
                                     'account': self.account,
                                     'digest': 'some_digest',
                                     'timestamp': now(),
                                     'backup': 'encoded backup data'
                                 },
                                 **self.auth_token_header)
     self.assertEqual(response.status_code, 201)
     self.assertIsNone(Backup.get_by_pk('some_digest_0'))
Exemplo n.º 10
0
 def post(request, pk_hash):
     if pk_hash is None:
         return Response({'details': 'primary key is not provided'},
                         status=status.HTTP_400_BAD_REQUEST)
     backup = Backup.remove(pk_hash)
     if backup is None:
         return Response(status=status.HTTP_404_NOT_FOUND)
     return Response(
         {'detail': 'backup {} has been deleted'.format(backup.digest)},
         status=status.HTTP_201_CREATED)
Exemplo n.º 11
0
 def create_object(self):
     group_id = self.get_argument('group_id', None)
     now = timezone.now()
     file = self.create_backup(now)
     kwargs = {
         'group_id': group_id,
         'created': now,
         'file': file,
     }
     return Backup.create(**kwargs)
Exemplo n.º 12
0
class BackupSerializerTestCase(TestCase):

    timestamp = now()

    def setUp(self):
        self.account = Account(
            **{
                'username': '******',
                'email': '*****@*****.**',
                'password': '******'
            })
        self.account.save()
        self.expected_backup = Backup(
            **{
                'digest': 'super_hash',
                'timestamp': self.timestamp,
                'backup': 'backup content',
                'account': self.account
            })
        self.expected_backup.save()

    def test_get_by_pk_exists(self):
        actual_backup = Backup.get_by_pk('super_hash')
        self.assertIsNotNone(actual_backup)
        self.assertEqual(self.expected_backup.account, actual_backup.account)
        self.assertEqual(self.expected_backup.digest, actual_backup.digest)
        self.assertEqual(self.expected_backup.timestamp,
                         actual_backup.timestamp)
        self.assertEqual(self.expected_backup.backup, actual_backup.backup)

    def test_get_by_pk_does_not_exist(self):
        self.assertIsNone(Backup.get_by_pk('super_hash1'))

    def test_remove_exists(self):
        backup = Backup.remove('super_hash')
        self.assertIsNotNone(backup)

    def test_remove_does_not_exist(self):
        backup = Backup.remove('super_hash1')
        self.assertIsNone(backup)
Exemplo n.º 13
0
 def setUp(self):
     self.account = Account.create(
         **{
             'username': '******',
             'email': '*****@*****.**',
             'password': '******'
         })
     self.account.save()
     response = self.client.post('/api/v1/login',
                                 data={
                                     'username': '******',
                                     'password': '******'
                                 })
     self.auth_token_header = {
         'HTTP_AUTHORIZATION': 'Token {}'.format(response.json().get('key'))
     }
     for i in range(4):
         Backup(
             **{
                 'account': self.account,
                 'digest': 'some_digest_{}'.format(i),
                 'timestamp': now(),
                 'backup': 'encoded backup data {}'.format(i)
             }).save()
Exemplo n.º 14
0
 def create(self, validated_data):
     backup = Backup(account=self.context['request'].user, **validated_data)
     backup.save()
     return backup
Exemplo n.º 15
0
 def test_remove_does_not_exist(self):
     backup = Backup.remove('super_hash1')
     self.assertIsNone(backup)
Exemplo n.º 16
0
 def test_remove_exists(self):
     backup = Backup.remove('super_hash')
     self.assertIsNotNone(backup)
Exemplo n.º 17
0
 def test_get_by_pk_does_not_exist(self):
     self.assertIsNone(Backup.get_by_pk('super_hash1'))
Exemplo n.º 18
0
 def validate(self, data):
     digest = data.get('digest')
     if Backup.get_by_pk(digest) is not None:
         raise serializers.ValidationError(
             'backup {} already exists'.format(digest))
     return data