Added authentication for test-requests and test agains current nginx-server
Sonarqube Scanner / Build and analyze (push) Failing after 20s
Sonarqube Scanner / Build and analyze (push) Failing after 20s
This commit is contained in:
@@ -31,7 +31,10 @@ test('Python API tools are syntax-valid and contain reusable 401 authentication
|
||||
'create_route.py', 'delete_audio.py', 'delete_picture.py', 'delete_poi.py', 'delete_route.py',
|
||||
'restore_route.py', 'update_audio.py', 'update_picture.py', 'update_poi.py', 'update_route.py'
|
||||
];
|
||||
assert.deepEqual(names.sort(), expected.sort());
|
||||
assert.deepEqual(
|
||||
names.toSorted((left, right) => left.localeCompare(right)),
|
||||
expected.toSorted((left, right) => left.localeCompare(right))
|
||||
);
|
||||
|
||||
const compile = spawnSync('python3', ['-m', 'py_compile', ...names.map(name => path.join(toolsDir, name))], {
|
||||
cwd: root,
|
||||
@@ -45,17 +48,22 @@ test('Python API tools are syntax-valid and contain reusable 401 authentication
|
||||
assert.match(common, /error\.code == 401/);
|
||||
assert.match(common, /self\.prompt_credentials\(\)/);
|
||||
assert.match(common, /getpass\.getpass/);
|
||||
assert.match(common, /os\.getenv\(API_USER_ENV/);
|
||||
assert.match(common, /os\.getenv\(API_PASSWORD_ENV/);
|
||||
assert.match(common, /def encode_multipart/);
|
||||
assert.doesNotMatch(common, /Content-Type:.*application\/octet-stream/);
|
||||
});
|
||||
|
||||
|
||||
test('Python client retries the same request with credentials after HTTP 401', async t => {
|
||||
const expected = `Basic ${Buffer.from('tester:secret').toString('base64')}`;
|
||||
let requests = 0;
|
||||
test('Python client retries the same request with API_USER and API_PASSWORD after HTTP 401', async t => {
|
||||
const apiUser = process.env.API_USER || 'tester';
|
||||
const apiPassword = process.env.API_PASSWORD || 'secret';
|
||||
const expectedAuthorization = `Basic ${Buffer.from(`${apiUser}:${apiPassword}`).toString('base64')}`;
|
||||
const receivedAuthorization = [];
|
||||
|
||||
const server = http.createServer((req, res) => {
|
||||
requests += 1;
|
||||
if (req.headers.authorization !== expected) {
|
||||
receivedAuthorization.push(req.headers.authorization || null);
|
||||
if (req.headers.authorization !== expectedAuthorization) {
|
||||
res.writeHead(401, { 'Content-Type': 'application/json', 'WWW-Authenticate': 'Basic realm="test"' });
|
||||
res.end(JSON.stringify({ message: 'Zugangsdaten erforderlich.' }));
|
||||
return;
|
||||
@@ -68,20 +76,22 @@ test('Python client retries the same request with credentials after HTTP 401', a
|
||||
const { port } = server.address();
|
||||
|
||||
const script = `
|
||||
from common import ApiClient, Credentials
|
||||
class TestClient(ApiClient):
|
||||
def prompt_credentials(self):
|
||||
self.credentials = Credentials('tester', 'secret')
|
||||
result = TestClient('http://127.0.0.1:${port}').request('GET', '/protected')
|
||||
from common import ApiClient
|
||||
result = ApiClient('http://127.0.0.1:${port}').request('GET', '/protected')
|
||||
assert result == {'ok': True}
|
||||
`;
|
||||
const child = spawn('python3', ['-c', script], {
|
||||
env: { ...process.env, PYTHONPATH: path.join(root, 'tools/python') },
|
||||
env: {
|
||||
...process.env,
|
||||
API_USER: apiUser,
|
||||
API_PASSWORD: apiPassword,
|
||||
PYTHONPATH: path.join(root, 'tools/python')
|
||||
},
|
||||
stdio: ['ignore', 'pipe', 'pipe']
|
||||
});
|
||||
const stderr = [];
|
||||
child.stderr.on('data', chunk => stderr.push(chunk.toString()));
|
||||
const status = await new Promise(resolve => child.once('exit', resolve));
|
||||
assert.equal(status, 0, stderr.join(''));
|
||||
assert.equal(requests, 2);
|
||||
assert.deepEqual(receivedAuthorization, [null, expectedAuthorization]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user