From f57a31c9b512e85cb513e6aeff7381d396722f6e Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 28 Aug 2021 20:46:51 +1000 Subject: [PATCH] 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 --- ci/.eslintrc.yml => .eslintrc.yml | 0 .gitignore | 3 + InvenTree/InvenTree/ci_render_js.py | 93 +++++++++++++++++++++++ ci/.gitignore | 1 - ci/pull_js_files.py | 111 ---------------------------- 5 files changed, 96 insertions(+), 112 deletions(-) rename ci/.eslintrc.yml => .eslintrc.yml (100%) create mode 100644 InvenTree/InvenTree/ci_render_js.py delete mode 100644 ci/.gitignore delete mode 100644 ci/pull_js_files.py diff --git a/ci/.eslintrc.yml b/.eslintrc.yml similarity index 100% rename from ci/.eslintrc.yml rename to .eslintrc.yml diff --git a/.gitignore b/.gitignore index f3fa0ac8c1..6f253ee8ea 100644 --- a/.gitignore +++ b/.gitignore @@ -67,6 +67,9 @@ secret_key.txt .coverage htmlcov/ +# Temporary javascript files (used for testing) +js_tmp/ + # Development files dev/ diff --git a/InvenTree/InvenTree/ci_render_js.py b/InvenTree/InvenTree/ci_render_js.py new file mode 100644 index 0000000000..c45caf604d --- /dev/null +++ b/InvenTree/InvenTree/ci_render_js.py @@ -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') + + + + + diff --git a/ci/.gitignore b/ci/.gitignore deleted file mode 100644 index b8ba69cb91..0000000000 --- a/ci/.gitignore +++ /dev/null @@ -1 +0,0 @@ -js_tmp/ \ No newline at end of file diff --git a/ci/pull_js_files.py b/ci/pull_js_files.py deleted file mode 100644 index b69268e0e2..0000000000 --- a/ci/pull_js_files.py +++ /dev/null @@ -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) \ No newline at end of file