This repository has been archived by the owner on May 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from kpn/feature/credentialsecrets-model
NEW - Added credential secret
- Loading branch information
Showing
9 changed files
with
298 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,30 @@ | ||
from django.contrib import admin | ||
|
||
from katka.fields import username_on_model | ||
from katka.models import Team | ||
from katka.models import Credential, CredentialSecret, Project, Team | ||
|
||
|
||
@admin.register(Team) | ||
class TeamAdmin(admin.ModelAdmin): | ||
fields = ('name', 'group') | ||
|
||
class WithUsernameAdminModel(admin.ModelAdmin): | ||
def save_model(self, request, obj, form, change): | ||
with username_on_model(self.model, request.user.username): | ||
super().save_model(request, obj, form, change) | ||
|
||
|
||
@admin.register(Team) | ||
class TeamAdmin(WithUsernameAdminModel): | ||
fields = ('name', 'group') | ||
|
||
|
||
@admin.register(Project) | ||
class ProjectAdmin(WithUsernameAdminModel): | ||
fields = ('name', 'slug', 'team') | ||
|
||
|
||
@admin.register(Credential) | ||
class CredentialAdmin(WithUsernameAdminModel): | ||
fields = ('name', 'slug', 'credential_type', 'team') | ||
|
||
|
||
@admin.register(CredentialSecret) | ||
class CredentialSecretAdmin(WithUsernameAdminModel): | ||
fields = ('key', 'value', 'credential') |
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,35 @@ | ||
# Generated by Django 2.1.5 on 2019-02-12 02:12 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
import encrypted_model_fields.fields | ||
import katka.fields | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('katka', '0003_credential_and_unique_slugs'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='CredentialSecret', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('created', models.DateTimeField(auto_now_add=True)), | ||
('created_username', katka.fields.AutoUsernameField(max_length=50)), | ||
('modified', models.DateTimeField(auto_now=True)), | ||
('modified_username', katka.fields.AutoUsernameField(max_length=50)), | ||
('status', models.CharField(choices=[('active', 'active'), ('inactive', 'inactive')], default='active', max_length=50)), | ||
('key', models.CharField(max_length=50)), | ||
('value', encrypted_model_fields.fields.EncryptedCharField()), | ||
('credential', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='katka.Credential')), | ||
], | ||
), | ||
migrations.AlterUniqueTogether( | ||
name='credentialsecret', | ||
unique_together={('credential', 'key')}, | ||
), | ||
] |
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
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
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 uuid import UUID | ||
|
||
import pytest | ||
from katka import models | ||
from katka.constants import STATUS_INACTIVE | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestCredentialSecretViewSetUnauthenticated: | ||
""" | ||
When a user is not logged in, no group information is available, so nothing is returned. | ||
For listing, that would be an empty list for other operations, an error like the object could | ||
not be found, except on create (you need to be part of a group and anonymous users do not have any) | ||
""" | ||
|
||
def test_list(self, client, team, credential, secret): | ||
url = f'/team/{team.public_identifier}/credential/{credential.public_identifier}/secrets/' | ||
response = client.get(url) | ||
assert response.status_code == 200 | ||
parsed = response.json() | ||
assert len(parsed) == 0 | ||
|
||
def test_list_unknown_team(self, client, team, credential, secret): | ||
unknown_pui = '00000000-0000-0000-0000-000000000000' | ||
url = f'/team/{unknown_pui}/credential/{credential.public_identifier}/secrets/' | ||
response = client.get(url) | ||
assert response.status_code == 200 | ||
parsed = response.json() | ||
assert len(parsed) == 0 | ||
|
||
def test_get(self, client, team, credential, secret): | ||
url = f'/team/{team.public_identifier}/credential/{credential.public_identifier}/secrets/{secret.key}/' | ||
response = client.get(url) | ||
assert response.status_code == 404 | ||
|
||
def test_delete(self, client, team, credential, secret): | ||
url = f'/team/{team.public_identifier}/credential/{credential.public_identifier}/secrets/{secret.key}/' | ||
response = client.delete(url) | ||
assert response.status_code == 404 | ||
|
||
def test_update(self, client, team, credential, secret): | ||
url = f'/team/{team.public_identifier}/credential/{credential.public_identifier}/secrets/{secret.key}/' | ||
data = {'name': 'B-Team', 'group': 'group1'} | ||
response = client.put(url, data, content_type='application/json') | ||
assert response.status_code == 404 | ||
|
||
def test_partial_update(self, client, team, credential, secret): | ||
url = f'/team/{team.public_identifier}/credential/{credential.public_identifier}/secrets/{secret.key}/' | ||
response = client.patch(url, {'name': 'B-Team'}, content_type='application/json') | ||
assert response.status_code == 404 | ||
|
||
def test_create(self, client, team, credential): | ||
url = f'/team/{team.public_identifier}/credential/{credential.public_identifier}/secrets/' | ||
data = {'key': 'access_token', 'value': 'my_secret_access_token'} | ||
response = client.post(url, data, content_type='application/json') | ||
assert response.status_code == 403 | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestCredentialViewSet: | ||
def test_list(self, client, logged_in_user, team, credential, secret): | ||
response = client.get(f'/team/{team.public_identifier}/credential/{credential.public_identifier}/secrets/') | ||
assert response.status_code == 200 | ||
parsed = response.json() | ||
assert len(parsed) == 1 | ||
secret = parsed[0] | ||
assert secret['key'] == 'access_token' | ||
assert secret['value'] == 'full_access_value' | ||
assert UUID(secret['credential']) == credential.public_identifier | ||
|
||
def test_get(self, client, logged_in_user, team, credential, secret): | ||
url = f'/team/{team.public_identifier}/credential/{credential.public_identifier}/secrets/{secret.key}/' | ||
response = client.get(url) | ||
assert response.status_code == 200 | ||
secret = response.json() | ||
assert secret['key'] == 'access_token' | ||
assert secret['value'] == 'full_access_value' | ||
assert UUID(secret['credential']) == credential.public_identifier | ||
|
||
def test_get_excludes_inactive(self, client, logged_in_user, team, credential, deactivated_secret): | ||
url = f'/team/{team.public_identifier}/credential/{credential.public_identifier}/' + \ | ||
f'secrets/{deactivated_secret.key}/' | ||
response = client.get(url) | ||
assert response.status_code == 404 | ||
|
||
def test_delete(self, client, logged_in_user, team, credential, secret): | ||
url = f'/team/{team.public_identifier}/credential/{credential.public_identifier}/secrets/{secret.key}/' | ||
response = client.delete(url) | ||
assert response.status_code == 204 | ||
s = models.CredentialSecret.objects.get(key=secret.key, credential=credential) | ||
assert s.status == STATUS_INACTIVE | ||
|
||
def test_update(self, client, logged_in_user, team, credential, secret): | ||
url = f'/team/{team.public_identifier}/credential/{credential.public_identifier}/secrets/{secret.key}/' | ||
data = {'key': 'access_token', 'value': 'new value'} | ||
response = client.put(url, data, content_type='application/json') | ||
assert response.status_code == 200 | ||
s = models.CredentialSecret.objects.get(key=secret.key, credential=credential) | ||
assert s.value == 'new value' | ||
|
||
def test_update_nonexistent_team(self, client, logged_in_user, team, credential, secret): | ||
unknown_pui = '00000000-0000-0000-0000-000000000000' | ||
url = f'/team/{unknown_pui}/credential/{credential.public_identifier}/secrets/{secret.key}/' | ||
data = {'key': 'access_token', 'value': 'new value'} | ||
response = client.put(url, data, content_type='application/json') | ||
assert response.status_code == 404 | ||
|
||
def test_partial_update(self, client, logged_in_user, team, credential, secret): | ||
url = f'/team/{team.public_identifier}/credential/{credential.public_identifier}/secrets/{secret.key}/' | ||
data = {'value': 'new value'} | ||
response = client.patch(url, data, content_type='application/json') | ||
assert response.status_code == 200 | ||
s = models.CredentialSecret.objects.get(key=secret.key, credential=credential) | ||
assert s.value == 'new value' | ||
|
||
def test_create(self, client, logged_in_user, team, credential, secret): | ||
url = f'/team/{team.public_identifier}/credential/{credential.public_identifier}/secrets/' | ||
response = client.post(url, {'key': 'password', 'value': 'new value'}, content_type='application/json') | ||
assert response.status_code == 201 | ||
models.CredentialSecret.objects.get(key='access_token', credential=credential) | ||
assert models.CredentialSecret.objects.count() == 2 |
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