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 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)
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 }]
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()
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)
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)
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)
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)
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'))
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)
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)
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)
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()
def create(self, validated_data): backup = Backup(account=self.context['request'].user, **validated_data) backup.save() return backup
def test_remove_does_not_exist(self): backup = Backup.remove('super_hash1') self.assertIsNone(backup)
def test_remove_exists(self): backup = Backup.remove('super_hash') self.assertIsNotNone(backup)
def test_get_by_pk_does_not_exist(self): self.assertIsNone(Backup.get_by_pk('super_hash1'))
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