mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Add a test framework script to pull down rendered javascript files
- Use the testing framework so we don't need to spin up a server
This commit is contained in:
parent
880a701881
commit
f57a31c9b5
3
.gitignore
vendored
3
.gitignore
vendored
@ -67,6 +67,9 @@ secret_key.txt
|
||||
.coverage
|
||||
htmlcov/
|
||||
|
||||
# Temporary javascript files (used for testing)
|
||||
js_tmp/
|
||||
|
||||
# Development files
|
||||
dev/
|
||||
|
||||
|
93
InvenTree/InvenTree/ci_render_js.py
Normal file
93
InvenTree/InvenTree/ci_render_js.py
Normal file
@ -0,0 +1,93 @@
|
||||
"""
|
||||
Pull rendered copies of the templated
|
||||
"""
|
||||
|
||||
from django.http import response
|
||||
from django.test import TestCase, testcases
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
import os
|
||||
import pathlib
|
||||
|
||||
|
||||
class RenderJavascriptFiles(TestCase):
|
||||
"""
|
||||
A unit test to "render" javascript files.
|
||||
|
||||
The server renders templated javascript files,
|
||||
we need the fully-rendered files for linting and static tests.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
|
||||
user = get_user_model()
|
||||
|
||||
self.user = user.objects.create_user(
|
||||
username='testuser',
|
||||
password='testpassword',
|
||||
email='user@gmail.com',
|
||||
)
|
||||
|
||||
self.client.login(username='testuser', password='testpassword')
|
||||
|
||||
def download_file(self, filename, prefix):
|
||||
|
||||
url = os.path.join(prefix, filename)
|
||||
|
||||
response = self.client.get(url)
|
||||
|
||||
here = os.path.abspath(os.path.dirname(__file__))
|
||||
|
||||
output_dir = os.path.join(
|
||||
here,
|
||||
'..',
|
||||
'..',
|
||||
'js_tmp',
|
||||
)
|
||||
|
||||
output_dir = os.path.abspath(output_dir)
|
||||
|
||||
if not os.path.exists(output_dir):
|
||||
os.mkdir(output_dir)
|
||||
|
||||
output_file = os.path.join(
|
||||
output_dir,
|
||||
filename,
|
||||
)
|
||||
|
||||
with open(output_file, 'wb') as output:
|
||||
output.write(response.content)
|
||||
|
||||
def download_files(self, subdir, prefix):
|
||||
here = os.path.abspath(os.path.dirname(__file__))
|
||||
|
||||
js_template_dir = os.path.join(
|
||||
here,
|
||||
'..',
|
||||
'templates',
|
||||
'js',
|
||||
)
|
||||
|
||||
directory = os.path.join(js_template_dir, subdir)
|
||||
|
||||
directory = os.path.abspath(directory)
|
||||
|
||||
js_files = pathlib.Path(directory).rglob('*.js')
|
||||
|
||||
for f in js_files:
|
||||
js = os.path.basename(f)
|
||||
|
||||
self.download_file(js, prefix)
|
||||
|
||||
def test_render_files(self):
|
||||
"""
|
||||
Look for all javascript files
|
||||
"""
|
||||
|
||||
self.download_files('translated', '/js/i18n')
|
||||
self.download_files('dynamic', '/js/dynamic')
|
||||
|
||||
|
||||
|
||||
|
||||
|
1
ci/.gitignore
vendored
1
ci/.gitignore
vendored
@ -1 +0,0 @@
|
||||
js_tmp/
|
@ -1,111 +0,0 @@
|
||||
"""
|
||||
Pull 'rendered' copies of the templated JS files down from the InvenTree server.
|
||||
|
||||
These files can then be used for linting and unit testing
|
||||
"""
|
||||
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import sys
|
||||
import re
|
||||
import os
|
||||
import json
|
||||
import pathlib
|
||||
import argparse
|
||||
import requests
|
||||
from requests.auth import HTTPBasicAuth
|
||||
|
||||
|
||||
here = os.path.abspath(os.path.dirname(__file__))
|
||||
js_template_dir = os.path.abspath(os.path.join(here, '..', 'InvenTree', 'templates', 'js'))
|
||||
|
||||
js_tmp_dir = os.path.join(here, 'js_tmp')
|
||||
|
||||
def get_token(server, username, password):
|
||||
|
||||
url = os.path.join(
|
||||
server,
|
||||
'api',
|
||||
'user',
|
||||
'token',
|
||||
)
|
||||
|
||||
auth = HTTPBasicAuth(username, password)
|
||||
|
||||
response = requests.get(url, auth=auth, allow_redirects=False)
|
||||
|
||||
data = json.loads(response.text)
|
||||
|
||||
return data['token']
|
||||
|
||||
|
||||
def download_file(url, filename, token):
|
||||
"""
|
||||
Download a single javascript file
|
||||
"""
|
||||
|
||||
print(f"Downloading '{url}'")
|
||||
|
||||
headers = {
|
||||
'AUTHORIZATION': f'Token {token}'
|
||||
}
|
||||
|
||||
response = requests.get(
|
||||
url,
|
||||
allow_redirects=False,
|
||||
headers=headers
|
||||
)
|
||||
|
||||
output_file = os.path.join(
|
||||
js_tmp_dir,
|
||||
filename,
|
||||
)
|
||||
|
||||
with open(output_file, 'wb') as output:
|
||||
output.write(response.content)
|
||||
|
||||
|
||||
def download_js_files(subdir, url, token):
|
||||
"""
|
||||
Returns a flattened list of all javascript files
|
||||
"""
|
||||
|
||||
d = os.path.join(js_template_dir, subdir)
|
||||
|
||||
files = pathlib.Path(d).rglob('*.js')
|
||||
|
||||
for filename in files:
|
||||
js = os.path.basename(filename)
|
||||
|
||||
js_url = os.path.join(url, js)
|
||||
|
||||
download_file(js_url, js, token)
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
parser = argparse.ArgumentParser("Download JavaScript files")
|
||||
|
||||
parser.add_argument('-s', '--server', help='InvenTree server', action='store')
|
||||
parser.add_argument('-u', '--username', help='Username', action='store')
|
||||
parser.add_argument('-p', '--password', help='password', action='store')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if not os.path.exists(js_tmp_dir):
|
||||
os.mkdir(js_tmp_dir)
|
||||
|
||||
auth = HTTPBasicAuth(args.username, args.password)
|
||||
|
||||
# Get an auth token from the server
|
||||
token = get_token(args.server, args.username, args.password)
|
||||
|
||||
# Dynamic javascript files
|
||||
dynamic_url = os.path.join(args.server, 'js', 'dynamic')
|
||||
|
||||
download_js_files('dynamic', dynamic_url, token)
|
||||
|
||||
# Translated JS files
|
||||
i18n_url = os.path.join(args.server, 'js', 'i18n')
|
||||
|
||||
download_js_files("translated", i18n_url, token)
|
Loading…
Reference in New Issue
Block a user