Browse Source

Prepare for the removal of Profile.api_key

pull/214/head
Pēteris Caune 6 years ago
parent
commit
c16e9dc4fe
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
4 changed files with 25 additions and 32 deletions
  1. +0
    -20
      hc/accounts/management/commands/createreadonlykeys.py
  2. +13
    -0
      hc/accounts/tests/test_profile.py
  3. +8
    -8
      hc/accounts/views.py
  4. +4
    -4
      templates/accounts/profile.html

+ 0
- 20
hc/accounts/management/commands/createreadonlykeys.py View File

@ -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

+ 13
- 0
hc/accounts/tests/test_profile.py View File

@ -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="[email protected]", 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="[email protected]", password="password") self.client.login(username="[email protected]", password="password")


+ 8
- 8
hc/accounts/views.py View File

@ -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,10 +211,9 @@ 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_readonly = profile.api_key_readonly
project.save()
project.api_key = profile.api_key
project.api_key_readonly = profile.api_key_readonly
project.save()
ctx["show_api_keys"] = True ctx["show_api_keys"] = True
ctx["api_keys_created"] = True ctx["api_keys_created"] = True
@ -223,10 +224,9 @@ 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_readonly = ""
project.save()
project.api_key = ""
project.api_key_readonly = ""
project.save()
ctx["api_keys_revoked"] = True ctx["api_keys_revoked"] = True
ctx["api_status"] = "info" ctx["api_status"] = "info"


+ 4
- 4
templates/accounts/profile.html View File

@ -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…
Cancel
Save