Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add session_id to headers for all HTTP requests to sphinx-tribes backend #820

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
17 changes: 17 additions & 0 deletions cypress/support/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,27 @@
import './commands';
import nodes from '../fixtures/nodes.json';
import nodesv2 from '../fixtures/v2nodes.json';
import { randomString } from '../../src/helpers/helpers-extended';

// Alternatively you can use CommonJS syntax:
// require('./commands')

const sessionId = randomString(32);

before(() => {
sessionStorage.setItem('sphinx_session_id', sessionId);
});

beforeEach(() => {
if (!sessionStorage.getItem('sphinx_session_id')) {
sessionStorage.setItem('sphinx_session_id', sessionId);
}

cy.intercept('**/*', (req: any) => {
req.headers['x-session-id'] = sessionStorage.getItem('sphinx_session_id');
});
});

async function postAllUsersToTribe() {
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
Expand Down
37 changes: 34 additions & 3 deletions src/bounties/__tests__/bountyTest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ const mockedApi = api as jest.Mocked<typeof api>;
describe('Bounty Tests', () => {
beforeAll(() => {
Object.defineProperty(window, 'localStorage', { value: localStorageMock });
const Crypto = {
getRandomValues: (arr: Uint8Array) => {
for (let i = 0; i < arr.length; i++) {
arr[i] = Math.floor(Math.random() * 256);
}
return arr;
}
};
global.crypto = Crypto as any;
});

beforeEach(() => {
Expand All @@ -37,6 +46,10 @@ describe('Bounty Tests', () => {
it('should save a new bounty and persist to localStorage', async () => {
global.fetch = jest.fn();
mockedApi.post = jest.fn().mockResolvedValue(newBounty);

mainStore.initializeSessionId();
const { sessionId } = mainStore;

await mainStore.saveBounty(newBounty);
expect(mockedApi.post).toHaveBeenCalledTimes(0);
expect(global.fetch).toHaveBeenCalledTimes(1);
Expand All @@ -45,7 +58,11 @@ describe('Bounty Tests', () => {
'http://localhost:5002/gobounties?token=undefined',
{
body: JSON.stringify(newBounty),
headers: { 'Content-Type': 'application/json', 'x-jwt': undefined },
headers: {
'Content-Type': 'application/json',
'x-jwt': undefined,
'x-session-id': sessionId
},
method: 'POST',
mode: 'cors'
}
Expand All @@ -68,11 +85,18 @@ describe('Bounty Tests', () => {
mockedApi.del = jest.fn().mockResolvedValue({});
await mainStore.deleteBounty(bountyIdToDelete, publicKeyToDelete);

mainStore.initializeSessionId();
const { sessionId } = mainStore;

const deleteRequestContent = [
'http://localhost:5002/gobounties?token=undefined',
{
body: JSON.stringify(newBounty),
headers: { 'Content-Type': 'application/json', 'x-jwt': undefined },
headers: {
'Content-Type': 'application/json',
'x-jwt': undefined,
'x-session-id': sessionId
},
method: 'POST',
mode: 'cors'
}
Expand All @@ -89,11 +113,18 @@ describe('Bounty Tests', () => {
it('should fetch and persist people bounties to localStorage', async () => {
await mainStore.getPeopleBounties();

mainStore.initializeSessionId();
const { sessionId } = mainStore;

const peopleRequestContent = [
'http://localhost:5002/gobounties?token=undefined',
{
body: JSON.stringify(newBounty),
headers: { 'Content-Type': 'application/json', 'x-jwt': undefined },
headers: {
'Content-Type': 'application/json',
'x-jwt': undefined,
'x-session-id': sessionId
},
method: 'POST',
mode: 'cors'
}
Expand Down
72 changes: 48 additions & 24 deletions src/store/__test__/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ let mockApiResponseData: any[];

const origFetch = global.fetch;

const Crypto = {
getRandomValues: (arr: Uint8Array) => {
for (let i = 0; i < arr.length; i++) {
arr[i] = Math.floor(Math.random() * 256);
}
return arr;
}
};

beforeAll(() => {
fetchStub = sinon.stub(global, 'fetch');
fetchStub.returns(Promise.resolve({ status: 200, json: () => Promise.resolve({}) })); // Mock a default behavior
Expand All @@ -26,6 +35,7 @@ beforeAll(() => {
{ uuid: 'cldl1g04nncmf23du7kg' },
{ orgUUID: 'cmas9gatu2rvqiev4ur0' }
];
global.crypto = Crypto as any;
});

afterAll(() => {
Expand Down Expand Up @@ -548,19 +558,26 @@ describe('Main store', () => {
it('should send request delete request with correct body and url', async () => {
const url = `${TribesURL}/gobounties/pub_key/1111`;
const allBountiesUrl = `http://${getHost()}/gobounties/all?limit=10&sortBy=created&search=&page=1&resetPage=true&Open=true&Assigned=false&Paid=false`;

const store = new MainStore();
store.initializeSessionId();

const expectedRequestOptions: RequestInit = {
method: 'DELETE',
mode: 'cors',
headers: {
'x-jwt': user.tribe_jwt,
'Content-Type': 'application/json'
'Content-Type': 'application/json',
'x-session-id': store.sessionId
}
};

fetchStub.withArgs(url, expectedRequestOptions).returns(
Promise.resolve({
status: 200
}) as any
);

fetchStub.withArgs(allBountiesUrl, sinon.match.any).returns(
Promise.resolve({
status: 200,
Expand All @@ -569,7 +586,6 @@ describe('Main store', () => {
}) as any
);

const store = new MainStore();
await store.deleteBounty(1111, 'pub_key');

expect(fetchStub.withArgs(url, expectedRequestOptions).calledOnce).toEqual(true);
Expand All @@ -579,17 +595,21 @@ describe('Main store', () => {

it('should not panic if failed to delete bounty', async () => {
const url = `${TribesURL}/gobounties/pub_key/1111`;

const store = new MainStore();
store.initializeSessionId();

const expectedRequestOptions: RequestInit = {
method: 'DELETE',
mode: 'cors',
headers: {
'x-jwt': user.tribe_jwt,
'Content-Type': 'application/json'
'Content-Type': 'application/json',
'x-session-id': store.sessionId
}
};
fetchStub.withArgs(url, expectedRequestOptions).throwsException();

const store = new MainStore();
await store.deleteBounty(1111, 'pub_key');

expect(fetchStub.withArgs(url, expectedRequestOptions).calledOnce).toEqual(true);
Expand All @@ -598,6 +618,10 @@ describe('Main store', () => {

it('should not return false if asignee removed successfully', async () => {
const url = `${TribesURL}/gobounties/assignee`;

const store = new MainStore();
store.initializeSessionId();

const expectedRequestOptions: RequestInit = {
method: 'DELETE',
mode: 'cors',
Expand All @@ -607,7 +631,8 @@ describe('Main store', () => {
}),
headers: {
'x-jwt': user.tribe_jwt,
'Content-Type': 'application/json'
'Content-Type': 'application/json',
'x-session-id': store.sessionId
}
};
fetchStub.withArgs(url, expectedRequestOptions).returns(
Expand All @@ -616,7 +641,6 @@ describe('Main store', () => {
}) as any
);

const store = new MainStore();
const res = await store.deleteBountyAssignee({ owner_pubkey: 'pub_key', created: '1111' });

expect(fetchStub.withArgs(url, expectedRequestOptions).calledOnce).toEqual(true);
Expand All @@ -625,6 +649,10 @@ describe('Main store', () => {

it('should return false if failed to remove asignee ', async () => {
const url = `${TribesURL}/gobounties/assignee`;

const store = new MainStore();
store.initializeSessionId();

const expectedRequestOptions: RequestInit = {
method: 'DELETE',
mode: 'cors',
Expand All @@ -634,12 +662,12 @@ describe('Main store', () => {
}),
headers: {
'x-jwt': user.tribe_jwt,
'Content-Type': 'application/json'
'Content-Type': 'application/json',
'x-session-id': store.sessionId
}
};
fetchStub.withArgs(url, expectedRequestOptions).throwsException();

const store = new MainStore();
const res = await store.deleteBountyAssignee({ owner_pubkey: 'pub_key', created: '1111' });

expect(fetchStub.withArgs(url, expectedRequestOptions).calledOnce).toEqual(true);
Expand All @@ -648,12 +676,17 @@ describe('Main store', () => {

it('should successfully update bounty payment status', async () => {
const url = `${TribesURL}/gobounties/paymentstatus/1111`;

const store = new MainStore();
store.initializeSessionId();

const expectedRequestOptions: RequestInit = {
method: 'POST',
mode: 'cors',
headers: {
'x-jwt': user.tribe_jwt,
'Content-Type': 'application/json'
'Content-Type': 'application/json',
'x-session-id': store.sessionId
}
};
fetchStub.withArgs(url, expectedRequestOptions).returns(
Expand All @@ -662,7 +695,6 @@ describe('Main store', () => {
}) as any
);

const store = new MainStore();
const res = await store.updateBountyPaymentStatus(1111);

expect(fetchStub.withArgs(url, expectedRequestOptions).calledOnce).toEqual(true);
Expand All @@ -671,17 +703,21 @@ describe('Main store', () => {

it('should return false if failed to update bounty status', async () => {
const url = `${TribesURL}/gobounties/paymentstatus/1111`;

const store = new MainStore();
store.initializeSessionId();

const expectedRequestOptions: RequestInit = {
method: 'POST',
mode: 'cors',
headers: {
'x-jwt': user.tribe_jwt,
'Content-Type': 'application/json'
'Content-Type': 'application/json',
'x-session-id': store.sessionId
}
};
fetchStub.withArgs(url, expectedRequestOptions).throwsException();

const store = new MainStore();
const res = await store.updateBountyPaymentStatus(1111);

expect(fetchStub.withArgs(url, expectedRequestOptions).calledOnce).toEqual(true);
Expand Down Expand Up @@ -758,18 +794,6 @@ describe('Main store', () => {
// Arrange: Set user as logged out
uiStore.setMeInfo(emptyMeInfo);

// Define the expected query parameters
const queryParams = new URLSearchParams({
limit: '10',
sortBy: 'updatedat',
search: 'random',
page: '1',
resetPage: 'true'
// Add languages if applicable, e.g., languages: 'javascript,typescript'
});

const allBountiesUrl = `http://${getHost()}/gobounties/all?${queryParams.toString()}`;

// Stub the fetch with a flexible matcher
fetchStub
.withArgs(
Expand Down
Loading
Loading