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 }]
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)
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 create(self, validated_data): backup = Backup(account=self.context['request'].user, **validated_data) backup.save() return backup