v0.5.8: distinct MOCK_USERS (WZ88888=АЛЬФА, WZ77777=БЕТА)
This commit is contained in:
@@ -0,0 +1,61 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# API-тесты: все комбинации (без UI)
|
||||||
|
# Запуск: bash docs/test-api.sh
|
||||||
|
|
||||||
|
BASE="https://white.nodejsk8s.dev.nubes.ru"
|
||||||
|
PASS=0
|
||||||
|
FAIL=0
|
||||||
|
|
||||||
|
green() { echo -e "\033[32m$1\033[0m"; }
|
||||||
|
red() { echo -e "\033[31m$1\033[0m"; }
|
||||||
|
|
||||||
|
check() { # check "описание" expected actual
|
||||||
|
if [ "$2" = "$3" ]; then
|
||||||
|
green " ✓ $1"
|
||||||
|
PASS=$((PASS+1))
|
||||||
|
else
|
||||||
|
red " ✗ $1 (ожидалось: $2, получено: $3)"
|
||||||
|
FAIL=$((FAIL+1))
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# ── Получить mock JWT для заданного clientId ───────────────────
|
||||||
|
get_token() {
|
||||||
|
local CLIENT_ID="$1"
|
||||||
|
# POST /dev-login для получения токена через API
|
||||||
|
# Возвращает access_token из ответа
|
||||||
|
local RESP=$(curl -s -c /tmp/jar-test.txt "$BASE/login" > /dev/null && \
|
||||||
|
curl -s -b /tmp/jar-test.txt -c /tmp/jar-test.txt \
|
||||||
|
-X POST "$BASE/login" \
|
||||||
|
-d "clientId=$CLIENT_ID" \
|
||||||
|
-D - 2>&1)
|
||||||
|
# Извлечь токен из редиректа (он в сессии, получаем через API)
|
||||||
|
# Используем /dev-login для прямого получения токена
|
||||||
|
local CSRF=$(curl -s -b /tmp/jar-test.txt "$BASE/dev-login" | grep -oP 'name="_csrf" value="\K[^"]+')
|
||||||
|
curl -s -b /tmp/jar-test.txt -c /tmp/jar-test.txt \
|
||||||
|
-X POST "$BASE/dev-login" \
|
||||||
|
-d "clientId=$CLIENT_ID&_csrf=$CSRF&email=test@test.local&companyName=TestCo" \
|
||||||
|
> /dev/null
|
||||||
|
# Получить api-токен через сессию
|
||||||
|
# Пробуем получить через /login page (там токен генерится в сессию)
|
||||||
|
# Возвращаем токен из cookie
|
||||||
|
echo "TODO"
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "========================================="
|
||||||
|
echo " API Tests v0.5.4"
|
||||||
|
echo "========================================="
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
# ── 1. Без токена → 401 ────────────────────────────────────────
|
||||||
|
echo "--- 1. No token → 401 ---"
|
||||||
|
CODE=$(curl -s -o /dev/null -w '%{http_code}' "$BASE/api/v1/entries")
|
||||||
|
check "GET /entries без токена" "401" "$CODE"
|
||||||
|
|
||||||
|
CODE=$(curl -s -o /dev/null -w '%{http_code}' -X POST "$BASE/api/v1/entries" -H 'Content-Type: application/json' -d '{"value":"1.1.1.1/32"}')
|
||||||
|
check "POST /entries без токена" "401" "$CODE"
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "========================================="
|
||||||
|
echo " Результат: $PASS/$((PASS+FAIL))"
|
||||||
|
echo "========================================="
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ipwhitelist",
|
"name": "ipwhitelist",
|
||||||
"version": "0.5.7",
|
"version": "0.5.8",
|
||||||
"description": "IP WhiteList microservice for cloud provider",
|
"description": "IP WhiteList microservice for cloud provider",
|
||||||
"main": "server.js",
|
"main": "server.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
+18
-16
@@ -17,7 +17,7 @@
|
|||||||
const MOCK_USERS = [
|
const MOCK_USERS = [
|
||||||
{
|
{
|
||||||
id: 'admin',
|
id: 'admin',
|
||||||
label: 'Администратор (WZ01112)',
|
label: 'АДМИН (WZ01112)',
|
||||||
role: 'admin',
|
role: 'admin',
|
||||||
isAdmin: true,
|
isAdmin: true,
|
||||||
clientId: 'WZ01112',
|
clientId: 'WZ01112',
|
||||||
@@ -26,33 +26,35 @@ const MOCK_USERS = [
|
|||||||
email: 'admin@nubes.ru',
|
email: 'admin@nubes.ru',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'test',
|
id: 'alfa',
|
||||||
label: 'Тест (WZ01325)',
|
label: 'АЛЬФА (WZ88888)',
|
||||||
role: 'user',
|
role: 'user',
|
||||||
isAdmin: false,
|
isAdmin: false,
|
||||||
clientId: 'WZ01325',
|
clientId: 'WZ88888',
|
||||||
companyId: '3e64aac6-dcfc-4082-88dc-da19c86555a5',
|
companyId: '11111111-aaaa-aaaa-aaaa-111111111111',
|
||||||
companyName: 'Компания Тест',
|
companyName: 'АЛЬФА',
|
||||||
email: 'tazet@narod.ru',
|
email: 'alfa@example.com',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'multi',
|
id: 'multi',
|
||||||
label: 'Мульти (WZ01325 + WZ02001)',
|
label: 'АЛЬФА+БЕТА (WZ88888, WZ77777)',
|
||||||
role: 'user',
|
role: 'user',
|
||||||
isAdmin: false,
|
isAdmin: false,
|
||||||
clientId: 'WZ01325, WZ02001',
|
clientId: 'WZ88888, WZ77777',
|
||||||
companyId: '3e64aac6-dcfc-4082-88dc-da19c86555a5',
|
companyId: '11111111-aaaa-aaaa-aaaa-111111111111',
|
||||||
companyName: 'Мульти-компания',
|
companyName: 'АЛЬФА+БЕТА',
|
||||||
email: 'multi@example.com',
|
email: 'multi@example.com',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'client2',
|
id: 'beta',
|
||||||
label: 'Вторая (WZ02001)',
|
label: 'БЕТА (WZ77777)',
|
||||||
role: 'user',
|
role: 'user',
|
||||||
isAdmin: false,
|
isAdmin: false,
|
||||||
clientId: 'WZ02001',
|
clientId: 'WZ77777',
|
||||||
companyId: '11111111-2222-3333-4444-555555555555',
|
companyId: '22222222-bbbb-bbbb-bbbb-222222222222',
|
||||||
companyName: 'Вторая компания',
|
companyName: 'БЕТА',
|
||||||
|
email: 'beta@example.com',
|
||||||
|
},
|
||||||
email: 'user2@example.com',
|
email: 'user2@example.com',
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|||||||
+4
-4
@@ -146,8 +146,8 @@ async function cleanup() {
|
|||||||
// ── Токены для трёх пользователей ───────────────────────────────────────────
|
// ── Токены для трёх пользователей ───────────────────────────────────────────
|
||||||
const { MOCK_USERS } = require('../src/config');
|
const { MOCK_USERS } = require('../src/config');
|
||||||
const uAdmin = MOCK_USERS.find(u => u.id === 'admin');
|
const uAdmin = MOCK_USERS.find(u => u.id === 'admin');
|
||||||
const uUser = MOCK_USERS.find(u => u.id === 'test');
|
const uUser = MOCK_USERS.find(u => u.id === 'alfa');
|
||||||
const uUser2 = MOCK_USERS.find(u => u.id === 'client2');
|
const uUser2 = MOCK_USERS.find(u => u.id === 'beta');
|
||||||
|
|
||||||
const tokenAdmin = auth.issueMockToken({ clientId: uAdmin.clientId, companyId: uAdmin.companyId, companyName: uAdmin.companyName, email: uAdmin.email });
|
const tokenAdmin = auth.issueMockToken({ clientId: uAdmin.clientId, companyId: uAdmin.companyId, companyName: uAdmin.companyName, email: uAdmin.email });
|
||||||
const tokenUser = auth.issueMockToken({ clientId: uUser.clientId, companyId: uUser.companyId, companyName: uUser.companyName, email: uUser.email });
|
const tokenUser = auth.issueMockToken({ clientId: uUser.clientId, companyId: uUser.companyId, companyName: uUser.companyName, email: uUser.email });
|
||||||
@@ -983,7 +983,7 @@ async function cleanup() {
|
|||||||
|
|
||||||
// O8: admin передаёт ?client_id= → использует свою компанию (игнор)
|
// O8: admin передаёт ?client_id= → использует свою компанию (игнор)
|
||||||
{
|
{
|
||||||
const r = await req.get('/api/v1/entries?client_id=WZ02001').set(bearer(tokenAdmin));
|
const r = await req.get('/api/v1/entries?client_id=WZ77777').set(bearer(tokenAdmin));
|
||||||
log(r.status === 200, 'O8: admin GET ?client_id= → 200 (игнорирует)', `status=${r.status}`);
|
log(r.status === 200, 'O8: admin GET ?client_id= → 200 (игнорирует)', `status=${r.status}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -992,7 +992,7 @@ async function cleanup() {
|
|||||||
await req.delete('/api/v1/entries/' + multiEntry1).set(bearer(tokenMulti));
|
await req.delete('/api/v1/entries/' + multiEntry1).set(bearer(tokenMulti));
|
||||||
}
|
}
|
||||||
if (multiEntry2) {
|
if (multiEntry2) {
|
||||||
await req.delete('/api/v1/entries/' + multiEntry2 + '?client_id=WZ02001').set(bearer(tokenMulti));
|
await req.delete('/api/v1/entries/' + multiEntry2 + '?client_id=WZ77777').set(bearer(tokenMulti));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ function cidr(ip) { return ip.includes('/') ? ip : ip + '/32'; }
|
|||||||
const { MOCK_USERS } = require('../src/config');
|
const { MOCK_USERS } = require('../src/config');
|
||||||
|
|
||||||
const admin = MOCK_USERS.find(u => u.id === 'admin');
|
const admin = MOCK_USERS.find(u => u.id === 'admin');
|
||||||
const user = MOCK_USERS.find(u => u.id === 'test');
|
const user = MOCK_USERS.find(u => u.id === 'alfa');
|
||||||
const tokAdmin = auth.issueMockToken({ clientId: admin.clientId, companyId: admin.companyId, companyName: admin.companyName, email: admin.email });
|
const tokAdmin = auth.issueMockToken({ clientId: admin.clientId, companyId: admin.companyId, companyName: admin.companyName, email: admin.email });
|
||||||
const tokUser = auth.issueMockToken({ clientId: user.clientId, companyId: user.companyId, companyName: user.companyName, email: user.email });
|
const tokUser = auth.issueMockToken({ clientId: user.clientId, companyId: user.companyId, companyName: user.companyName, email: user.email });
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user