forked from GithubBackups/healthchecks
Prepare for the removal of Profile.api_key
This commit is contained in:
parent
fc18652afa
commit
c16e9dc4fe
@ -1,20 +0,0 @@
|
|||||||
from base64 import urlsafe_b64encode
|
|
||||||
import os
|
|
||||||
|
|
||||||
from django.core.management.base import BaseCommand
|
|
||||||
|
|
||||||
from hc.accounts.models import Profile
|
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
|
||||||
help = """Create read-only API keys."""
|
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
|
||||||
c = 0
|
|
||||||
q = Profile.objects.filter(api_key_readonly="").exclude(api_key="")
|
|
||||||
for profile in q:
|
|
||||||
profile.api_key_readonly = urlsafe_b64encode(os.urandom(24)).decode()
|
|
||||||
profile.save()
|
|
||||||
c += 1
|
|
||||||
|
|
||||||
return "Done! Generated %d readonly keys." % c
|
|
@ -27,6 +27,19 @@ class ProfileTestCase(BaseTestCase):
|
|||||||
expected_subject = "Set password on %s" % settings.SITE_NAME
|
expected_subject = "Set password on %s" % settings.SITE_NAME
|
||||||
self.assertEqual(mail.outbox[0].subject, expected_subject)
|
self.assertEqual(mail.outbox[0].subject, expected_subject)
|
||||||
|
|
||||||
|
def test_it_shows_api_keys(self):
|
||||||
|
self.project.api_key_readonly = "R" * 32
|
||||||
|
self.project.save()
|
||||||
|
|
||||||
|
self.client.login(username="alice@example.org", password="password")
|
||||||
|
|
||||||
|
form = {"show_api_keys": "1"}
|
||||||
|
r = self.client.post("/accounts/profile/", form)
|
||||||
|
self.assertEqual(r.status_code, 200)
|
||||||
|
|
||||||
|
self.assertContains(r, "X" * 32)
|
||||||
|
self.assertContains(r, "R" * 32)
|
||||||
|
|
||||||
def test_it_creates_api_key(self):
|
def test_it_creates_api_key(self):
|
||||||
self.client.login(username="alice@example.org", password="password")
|
self.client.login(username="alice@example.org", password="password")
|
||||||
|
|
||||||
|
@ -190,10 +190,12 @@ def check_token(request, username, token):
|
|||||||
def profile(request):
|
def profile(request):
|
||||||
_ensure_own_team(request)
|
_ensure_own_team(request)
|
||||||
profile = request.profile
|
profile = request.profile
|
||||||
|
project = profile.get_own_project()
|
||||||
|
|
||||||
ctx = {
|
ctx = {
|
||||||
"page": "profile",
|
"page": "profile",
|
||||||
"profile": profile,
|
"profile": profile,
|
||||||
|
"project": project,
|
||||||
"show_api_keys": False,
|
"show_api_keys": False,
|
||||||
"api_status": "default",
|
"api_status": "default",
|
||||||
"team_status": "default"
|
"team_status": "default"
|
||||||
@ -209,7 +211,6 @@ def profile(request):
|
|||||||
elif "create_api_keys" in request.POST:
|
elif "create_api_keys" in request.POST:
|
||||||
profile.set_api_keys()
|
profile.set_api_keys()
|
||||||
|
|
||||||
for project in request.user.project_set.all():
|
|
||||||
project.api_key = profile.api_key
|
project.api_key = profile.api_key
|
||||||
project.api_key_readonly = profile.api_key_readonly
|
project.api_key_readonly = profile.api_key_readonly
|
||||||
project.save()
|
project.save()
|
||||||
@ -223,7 +224,6 @@ def profile(request):
|
|||||||
profile.api_key_readonly = ""
|
profile.api_key_readonly = ""
|
||||||
profile.save()
|
profile.save()
|
||||||
|
|
||||||
for project in request.user.project_set.all():
|
|
||||||
project.api_key = ""
|
project.api_key = ""
|
||||||
project.api_key_readonly = ""
|
project.api_key_readonly = ""
|
||||||
project.save()
|
project.save()
|
||||||
|
@ -60,16 +60,16 @@
|
|||||||
<div class="panel panel-{{ api_status }}">
|
<div class="panel panel-{{ api_status }}">
|
||||||
<div class="panel-body settings-block">
|
<div class="panel-body settings-block">
|
||||||
<h2>API Access</h2>
|
<h2>API Access</h2>
|
||||||
{% if profile.api_key %}
|
{% if project.api_key %}
|
||||||
{% if show_api_keys %}
|
{% if show_api_keys %}
|
||||||
<p>
|
<p>
|
||||||
API key: <br />
|
API key: <br />
|
||||||
<code>{{ profile.api_key }}</code>
|
<code>{{ project.api_key }}</code>
|
||||||
</p>
|
</p>
|
||||||
{% if profile.api_key_readonly %}
|
{% if project.api_key_readonly %}
|
||||||
<p>
|
<p>
|
||||||
API key (read-only): <br />
|
API key (read-only): <br />
|
||||||
<code>{{ profile.api_key_readonly }}</code>
|
<code>{{ project.api_key_readonly }}</code>
|
||||||
</p>
|
</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<button
|
<button
|
||||||
|
Loading…
x
Reference in New Issue
Block a user