From f869d05ebee033a9d080bb32d7d202e816f4bca8 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <p.gawron@atcomp.pl>
Date: Thu, 16 May 2024 11:31:38 +0200
Subject: [PATCH] add login to error data

---
 src/utils/error-report/errorReporting.test.ts | 32 +++++++++++++++++++
 src/utils/error-report/errorReporting.ts      |  9 +++++-
 2 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/src/utils/error-report/errorReporting.test.ts b/src/utils/error-report/errorReporting.test.ts
index c782bcf9..8ffa91e7 100644
--- a/src/utils/error-report/errorReporting.test.ts
+++ b/src/utils/error-report/errorReporting.test.ts
@@ -1,13 +1,45 @@
 /* eslint-disable no-magic-numbers */
 import { createErrorData } from '@/utils/error-report/errorReporting';
+import { apiPath } from '@/redux/apiPath';
+import { HttpStatusCode } from 'axios';
+import { loginFixture } from '@/models/fixtures/loginFixture';
+import { userPrivilegesFixture } from '@/models/fixtures/userPrivilegesFixture';
+import { login } from '@/redux/user/user.thunks';
+import { mockNetworkResponse } from '@/utils/mockNetworkResponse';
+import { store } from '@/redux/store';
+
+const mockedAxiosClient = mockNetworkResponse();
+
+const CREDENTIALS = {
+  login: 'test',
+  password: 'password',
+};
 
 describe('createErrorData', () => {
   it('should add stacktrace', () => {
     const error = createErrorData(new Error('hello'));
     expect(error.stacktrace).not.toEqual('');
   });
+
   it('should add url', () => {
     const error = createErrorData(new Error('hello'));
     expect(error.url).not.toBeNull();
   });
+
+  it('should add guest login when not logged', () => {
+    const error = createErrorData(new Error('hello'));
+    expect(error.login).toBe('anonymous');
+  });
+
+  it('should add login when logged', async () => {
+    mockedAxiosClient.onPost(apiPath.postLogin()).reply(HttpStatusCode.Ok, loginFixture);
+    mockedAxiosClient
+      .onGet(apiPath.userPrivileges(loginFixture.login))
+      .reply(HttpStatusCode.Ok, userPrivilegesFixture);
+    await store.dispatch(login(CREDENTIALS));
+
+    const error = createErrorData(new Error('hello'));
+    expect(error.login).not.toBe('anonymous');
+    expect(error.login).toBe(loginFixture.login);
+  });
 });
diff --git a/src/utils/error-report/errorReporting.ts b/src/utils/error-report/errorReporting.ts
index e5992c14..85e5c2f8 100644
--- a/src/utils/error-report/errorReporting.ts
+++ b/src/utils/error-report/errorReporting.ts
@@ -1,6 +1,8 @@
 /* eslint-disable no-console */
 import { ErrorData } from '@/utils/error-report/ErrorData';
 import { SerializedError } from '@reduxjs/toolkit';
+// eslint-disable-next-line import/no-cycle
+import { store } from '@/redux/store';
 
 export const createErrorData = (error: Error | SerializedError | undefined): ErrorData => {
   let stacktrace = '';
@@ -8,9 +10,14 @@ export const createErrorData = (error: Error | SerializedError | undefined): Err
     stacktrace = error.stack !== undefined ? error.stack : '';
   }
 
+  let { login } = store.getState().user;
+  if (!login) {
+    login = 'anonymous';
+  }
+
   const errorData: ErrorData = {
     url: window.location.href,
-    login: null, // TODO provide user login
+    login,
     browser: null, // TODO
     comment: null,
     email: null, // TODO
-- 
GitLab