File size: 4,131 Bytes
9a92a42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
const API_URL = 'http://localhost:8000/api';
// Mock tokens - in a real test these would be obtained via login
const ADMIN_TOKEN = 'YOUR_ADMIN_TOKEN';
const TRAINER_TOKEN = 'YOUR_TRAINER_TOKEN';
function testLogging() {
    return __awaiter(this, void 0, void 0, function* () {
        var _a, _b, _c, _d;
        try {
            console.log('--- Testing Admin Access ---');
            // 1. Get all logs (Admin)
            console.log('1. Fetching all logs (Admin)...');
            try {
                const allLogs = yield axios_1.default.get(`${API_URL}/activity/timeline`, {
                    headers: { Authorization: `Bearer ${ADMIN_TOKEN}` }
                });
                console.log(`Success: Fetched ${allLogs.data.data.activities.length} logs.`);
            }
            catch (e) {
                console.error('Failed to fetch all logs:', ((_a = e.response) === null || _a === void 0 ? void 0 : _a.data) || e.message);
            }
            // 2. Filter by role (Admin)
            console.log('\n2. Filtering by role "trainer" (Admin)...');
            try {
                const trainerLogs = yield axios_1.default.get(`${API_URL}/activity/timeline?role=trainer`, {
                    headers: { Authorization: `Bearer ${ADMIN_TOKEN}` }
                });
                console.log(`Success: Fetched ${trainerLogs.data.data.activities.length} trainer logs.`);
            }
            catch (e) {
                console.error('Failed to fetch trainer logs:', ((_b = e.response) === null || _b === void 0 ? void 0 : _b.data) || e.message);
            }
            console.log('\n--- Testing User Access ---');
            // 3. Get own logs (Trainer)
            console.log('3. Fetching own logs (Trainer)...');
            try {
                const ownLogs = yield axios_1.default.get(`${API_URL}/activity/timeline`, {
                    headers: { Authorization: `Bearer ${TRAINER_TOKEN}` }
                });
                console.log(`Success: Fetched ${ownLogs.data.data.activities.length} own logs.`);
            }
            catch (e) {
                console.error('Failed to fetch own logs:', ((_c = e.response) === null || _c === void 0 ? void 0 : _c.data) || e.message);
            }
            // 4. Try to access other's logs (Trainer) - Should be ignored and return own logs
            console.log('4. Attempting to fetch other user logs (Trainer)...');
            try {
                const otherLogs = yield axios_1.default.get(`${API_URL}/activity/timeline?userId=SOME_OTHER_ID`, {
                    headers: { Authorization: `Bearer ${TRAINER_TOKEN}` }
                });
                // Verify that it returned own logs, not error, but effectively ignored the param
                console.log(`Success (Expected): Fetched ${otherLogs.data.data.activities.length} logs (should be own logs).`);
            }
            catch (e) {
                console.error('Failed (Unexpected):', ((_d = e.response) === null || _d === void 0 ? void 0 : _d.data) || e.message);
            }
        }
        catch (error) {
            console.error('Test failed:', error);
        }
    });
}
testLogging();