Skip to content

Commit 77eaff0

Browse files
committed
test: update local test env
1 parent 9fd5b0d commit 77eaff0

16 files changed

+141
-17
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ database=# select * from testmodel where id = 1;
4848

4949
```python
5050
from mirage.crypto import Crypto
51-
c = Crypto(key="") # key is optional, default will use settings.SECRET_KEY
51+
c = Crypto() # key is optional, default will use settings.SECRET_KEY
5252
c.encrypt('some_address') # -bYijegsEDrmS1s7ilnspA==
5353
c.decrypt('-bYijegsEDrmS1s7ilnspA==') # some_address
5454
```

mirage/crypto.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -27,46 +27,53 @@ def decrypt(self, encrypted) -> str:
2727
class ECBCipher(BaseCipher):
2828

2929
def encrypt(self, text):
30-
encryptor = Cipher(algorithms.AES(self.key), modes.ECB(), default_backend()).encryptor()
30+
encryptor = Cipher(algorithms.AES(self.key),
31+
modes.ECB(), default_backend()).encryptor()
3132
padder = padding.PKCS7(algorithms.AES(self.key).block_size).padder()
3233
padded_data = padder.update(force_bytes(text)) + padder.finalize()
3334
encrypted_text = encryptor.update(padded_data) + encryptor.finalize()
3435
return force_str(base64.urlsafe_b64encode(encrypted_text))
3536

3637
def decrypt(self, encrypted):
37-
decryptor = Cipher(algorithms.AES(self.key), modes.ECB(), default_backend()).decryptor()
38+
decryptor = Cipher(algorithms.AES(self.key),
39+
modes.ECB(), default_backend()).decryptor()
3840
padder = padding.PKCS7(algorithms.AES(self.key).block_size).unpadder()
3941
decrypted_text = decryptor.update(base64.urlsafe_b64decode(encrypted))
4042
unpadded_text = padder.update(decrypted_text) + padder.finalize()
4143
return force_str(unpadded_text)
4244

45+
4346
class CBCCipher(BaseCipher):
4447

4548
def encrypt(self, text) -> str:
46-
encryptor = Cipher(algorithms.AES(self.key), modes.CBC(self.iv), default_backend()).encryptor()
49+
encryptor = Cipher(algorithms.AES(self.key), modes.CBC(
50+
self.iv), default_backend()).encryptor()
4751
padder = padding.PKCS7(algorithms.AES(self.key).block_size).padder()
4852
padded_data = padder.update(force_bytes(text)) + padder.finalize()
4953
encrypted_text = encryptor.update(padded_data) + encryptor.finalize()
5054
return force_str(base64.urlsafe_b64encode(encrypted_text))
5155

5256
def decrypt(self, encrypted) -> str:
53-
decryptor = Cipher(algorithms.AES(self.key), modes.CBC(self.iv), default_backend()).decryptor()
57+
decryptor = Cipher(algorithms.AES(self.key), modes.CBC(
58+
self.iv), default_backend()).decryptor()
5459
padder = padding.PKCS7(algorithms.AES(self.key).block_size).unpadder()
5560
decrypted_text = decryptor.update(base64.urlsafe_b64decode(encrypted))
5661
unpadded_text = padder.update(decrypted_text) + padder.finalize()
5762
return force_str(unpadded_text)
5863

64+
5965
class Crypto:
6066

6167
def __init__(self, key=None, mode=None, iv=None):
62-
if key is None:
63-
key = getattr(settings, "MIRAGE_SECRET_KEY", None) or getattr(settings, "SECRET_KEY")
68+
if not key:
69+
key = getattr(settings, "MIRAGE_SECRET_KEY",
70+
None) or getattr(settings, "SECRET_KEY")
6471
assert len(key) >= 32, SHORT_SECRET_KEY
6572
key = base64.urlsafe_b64encode(force_bytes(key))[:32]
6673
if mode is None:
6774
mode = getattr(settings, "MIRAGE_CIPHER_MODE", "ECB")
6875
if iv is None:
69-
iv=getattr(settings, "MIRAGE_CIPHER_IV", "1234567890abcdef")
76+
iv = getattr(settings, "MIRAGE_CIPHER_IV", "1234567890abcdef")
7077
self.cipher = eval(f"{mode}Cipher")(key=key, iv=force_bytes(iv))
7178

7279
def encrypt(self, text):

tests/apps.py

-7
This file was deleted.

tests/apps/__init__.py

Whitespace-only changes.

tests/apps/admin.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from django.contrib import admin
2+
from django.contrib.admin.models import LogEntry
3+
from .models import TestModel
4+
5+
6+
@admin.register(LogEntry)
7+
class DjangoLogAdmin(admin.ModelAdmin):
8+
list_display = ['id', 'content_type', 'user',
9+
'object_repr', 'change_message', 'action_time']
10+
search_fields = ['user_id']
11+
12+
13+
@admin.register(TestModel)
14+
class DjangoLogAdmin(admin.ModelAdmin):
15+
list_display = ['id', 'char', 'text', 'textraw', 'integer', 'email']
16+
search_fields = ['id', 'char', 'text', 'textraw', 'integer', 'email']

tests/apps/apps.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class AppsConfig(AppConfig):
5+
name = 'apps'

tests/apps/migrations/0001_initial.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Generated by Django 3.0.5 on 2021-11-23 08:43
2+
3+
from django.db import migrations, models
4+
import mirage.fields
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
initial = True
10+
11+
dependencies = [
12+
]
13+
14+
operations = [
15+
migrations.CreateModel(
16+
name='TestModel',
17+
fields=[
18+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
19+
('char', mirage.fields.EncryptedCharField(blank=True, max_length=255, null=True)),
20+
('text', mirage.fields.EncryptedTextField(blank=True, null=True)),
21+
('integer', mirage.fields.EncryptedIntegerField(blank=True, max_length=64, null=True)),
22+
('email', mirage.fields.EncryptedEmailField(blank=True, max_length=254, null=True)),
23+
],
24+
),
25+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 3.0.5 on 2021-11-23 09:07
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('apps', '0001_initial'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='testmodel',
15+
name='textraw',
16+
field=models.TextField(blank=True, null=True),
17+
),
18+
]

tests/apps/migrations/__init__.py

Whitespace-only changes.

tests/models.py renamed to tests/apps/models.py

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
class TestModel(models.Model):
66
char = fields.EncryptedCharField(blank=True, null=True)
77
text = fields.EncryptedTextField(blank=True, null=True)
8+
textraw = models.TextField(blank=True, null=True)
89
integer = fields.EncryptedIntegerField(blank=True, null=True)
910
email = fields.EncryptedEmailField(blank=True, null=True)

tests/apps/tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

tests/apps/views.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.shortcuts import render
2+
3+
# Create your views here.

tests_manage.py renamed to tests/manage.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44

55
if __name__ == '__main__':
6-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tests.settings.pg')
6+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings.pg')
77
try:
88
from django.core.management import execute_from_command_line
99
except ImportError as exc:

tests/mirage

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../mirage

tests/settings/base.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,46 @@
11
# base64.urlsafe_encode('django-mirage-field have fun')
22
SECRET_KEY = "ZGphbmdvLW1pcmFnZS1maWVsZCBoYXZlIGZ1bg=="
33

4+
DEBUG = True
5+
STATIC_URL = "/static/"
6+
ROOT_URLCONF = "urls"
7+
48
MIRAGE_SECRET_KEY = "MIRAGE_SECRET_KEYMIRAGE_SECRET_KEY"
59
MIRAGE_CIPHER_MODE = "CBC"
610
MIRAGE_CIPHER_IV = "1234567890abcdef"
711

812
INSTALLED_APPS = [
13+
'django.contrib.admin',
914
'django.contrib.auth',
1015
'django.contrib.contenttypes',
1116
'django.contrib.sessions',
1217
'django.contrib.messages',
1318
'django.contrib.staticfiles',
14-
'tests',
19+
'apps',
20+
]
21+
22+
MIDDLEWARE = [
23+
'django.middleware.security.SecurityMiddleware',
24+
'django.contrib.sessions.middleware.SessionMiddleware',
25+
'django.middleware.common.CommonMiddleware',
26+
'django.middleware.csrf.CsrfViewMiddleware',
27+
'django.contrib.auth.middleware.AuthenticationMiddleware',
28+
'django.contrib.messages.middleware.MessageMiddleware',
29+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
30+
]
31+
32+
TEMPLATES = [
33+
{
34+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
35+
'DIRS': [],
36+
'APP_DIRS': True,
37+
'OPTIONS': {
38+
'context_processors': [
39+
'django.template.context_processors.debug',
40+
'django.template.context_processors.request',
41+
'django.contrib.auth.context_processors.auth',
42+
'django.contrib.messages.context_processors.messages',
43+
],
44+
},
45+
},
1546
]

tests/urls.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""mirage_demo URL Configuration
2+
3+
The `urlpatterns` list routes URLs to views. For more information please see:
4+
https://docs.djangoproject.com/en/3.0/topics/http/urls/
5+
Examples:
6+
Function views
7+
1. Add an import: from my_app import views
8+
2. Add a URL to urlpatterns: path('', views.home, name='home')
9+
Class-based views
10+
1. Add an import: from other_app.views import Home
11+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12+
Including another URLconf
13+
1. Import the include() function: from django.urls import include, path
14+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15+
"""
16+
from django.contrib import admin
17+
from django.urls import path
18+
19+
urlpatterns = [
20+
path('admin/', admin.site.urls),
21+
]

0 commit comments

Comments
 (0)