-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
1a9149d
commit fde9c16
Showing
3 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
from django.test import TestCase | ||
|
||
from core.models import DataSource | ||
from extras.choices import ObjectChangeActionChoices | ||
from netbox.constants import CENSOR_TOKEN, CENSOR_TOKEN_CHANGED | ||
|
||
|
||
class DataSourceChangeLoggingTestCase(TestCase): | ||
|
||
def test_password_added_on_create(self): | ||
datasource = DataSource.objects.create( | ||
name='Data Source 1', | ||
type='git', | ||
source_url='http://localhost/', | ||
parameters={ | ||
'username': 'jeff', | ||
'password': 'foobar123', | ||
} | ||
) | ||
|
||
objectchange = datasource.to_objectchange(ObjectChangeActionChoices.ACTION_CREATE) | ||
self.assertIsNone(objectchange.prechange_data) | ||
self.assertEqual(objectchange.postchange_data['parameters']['username'], 'jeff') | ||
self.assertEqual(objectchange.postchange_data['parameters']['password'], CENSOR_TOKEN_CHANGED) | ||
|
||
def test_password_added_on_update(self): | ||
datasource = DataSource.objects.create( | ||
name='Data Source 1', | ||
type='git', | ||
source_url='http://localhost/' | ||
) | ||
datasource.snapshot() | ||
|
||
# Add a blank password | ||
datasource.parameters = { | ||
'username': 'jeff', | ||
'password': '', | ||
} | ||
|
||
objectchange = datasource.to_objectchange(ObjectChangeActionChoices.ACTION_UPDATE) | ||
self.assertIsNone(objectchange.prechange_data['parameters']) | ||
self.assertEqual(objectchange.postchange_data['parameters']['username'], 'jeff') | ||
self.assertEqual(objectchange.postchange_data['parameters']['password'], '') | ||
|
||
# Add a password | ||
datasource.parameters = { | ||
'username': 'jeff', | ||
'password': 'foobar123', | ||
} | ||
|
||
objectchange = datasource.to_objectchange(ObjectChangeActionChoices.ACTION_UPDATE) | ||
self.assertEqual(objectchange.postchange_data['parameters']['username'], 'jeff') | ||
self.assertEqual(objectchange.postchange_data['parameters']['password'], CENSOR_TOKEN_CHANGED) | ||
|
||
def test_password_changed(self): | ||
datasource = DataSource.objects.create( | ||
name='Data Source 1', | ||
type='git', | ||
source_url='http://localhost/', | ||
parameters={ | ||
'username': 'jeff', | ||
'password': 'password1', | ||
} | ||
) | ||
datasource.snapshot() | ||
|
||
# Change the password | ||
datasource.parameters['password'] = 'password2' | ||
|
||
objectchange = datasource.to_objectchange(ObjectChangeActionChoices.ACTION_UPDATE) | ||
self.assertEqual(objectchange.prechange_data['parameters']['username'], 'jeff') | ||
self.assertEqual(objectchange.prechange_data['parameters']['password'], CENSOR_TOKEN) | ||
self.assertEqual(objectchange.postchange_data['parameters']['username'], 'jeff') | ||
self.assertEqual(objectchange.postchange_data['parameters']['password'], CENSOR_TOKEN_CHANGED) | ||
|
||
def test_password_removed_on_update(self): | ||
datasource = DataSource.objects.create( | ||
name='Data Source 1', | ||
type='git', | ||
source_url='http://localhost/', | ||
parameters={ | ||
'username': 'jeff', | ||
'password': 'foobar123', | ||
} | ||
) | ||
datasource.snapshot() | ||
|
||
objectchange = datasource.to_objectchange(ObjectChangeActionChoices.ACTION_UPDATE) | ||
self.assertEqual(objectchange.prechange_data['parameters']['username'], 'jeff') | ||
self.assertEqual(objectchange.prechange_data['parameters']['password'], CENSOR_TOKEN) | ||
self.assertEqual(objectchange.postchange_data['parameters']['username'], 'jeff') | ||
self.assertEqual(objectchange.postchange_data['parameters']['password'], CENSOR_TOKEN) | ||
|
||
# Remove the password | ||
datasource.parameters['password'] = '' | ||
|
||
objectchange = datasource.to_objectchange(ObjectChangeActionChoices.ACTION_UPDATE) | ||
self.assertEqual(objectchange.prechange_data['parameters']['username'], 'jeff') | ||
self.assertEqual(objectchange.prechange_data['parameters']['password'], CENSOR_TOKEN) | ||
self.assertEqual(objectchange.postchange_data['parameters']['username'], 'jeff') | ||
self.assertEqual(objectchange.postchange_data['parameters']['password'], '') | ||
|
||
def test_password_not_modified(self): | ||
datasource = DataSource.objects.create( | ||
name='Data Source 1', | ||
type='git', | ||
source_url='http://localhost/', | ||
parameters={ | ||
'username': 'username1', | ||
'password': 'foobar123', | ||
} | ||
) | ||
datasource.snapshot() | ||
|
||
# Remove the password | ||
datasource.parameters['username'] = 'username2' | ||
|
||
objectchange = datasource.to_objectchange(ObjectChangeActionChoices.ACTION_UPDATE) | ||
self.assertEqual(objectchange.prechange_data['parameters']['username'], 'username1') | ||
self.assertEqual(objectchange.prechange_data['parameters']['password'], CENSOR_TOKEN) | ||
self.assertEqual(objectchange.postchange_data['parameters']['username'], 'username2') | ||
self.assertEqual(objectchange.postchange_data['parameters']['password'], CENSOR_TOKEN) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters