Skip to content

Commit

Permalink
AE-2316: Remove querying the database from e2e tests
Browse files Browse the repository at this point in the history
The expired todistukset are just added to the test data
  • Loading branch information
solita-juhohaa committed Oct 29, 2024
1 parent 4ed9b4f commit bcc4b52
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 243 deletions.
25 changes: 1 addition & 24 deletions e2e-tests/cypress/cypress.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { defineConfig } = require('cypress');
const pg = require('pg');

module.exports = defineConfig({
viewportHeight: 900,
Expand All @@ -10,28 +9,6 @@ module.exports = defineConfig({
backendUrl: 'http://localhost:3444',
publicUrl: 'http://localhost:5059',
baseUrl: 'https://localhost:3009',
setupNodeEvents(on, config) {
on('task', {
// applicationName needs to be parsable by our database audit system. For example -6@something.
executeQuery({ query, applicationName }) {
const client = new pg.Client({
connectionString: `postgresql://etp_app:etp@localhost:5444/etp_dev?search_path=etp&application_name=${applicationName}`
});

return client
.connect()
.then(() => client.query(query))
.then(result => {
return result.rows;
})
.catch(err => {
throw err;
})
.finally(() => {
client.end();
});
}
});
}
setupNodeEvents(on, config) {}
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,27 @@ const backendUrl = Cypress.config('backendUrl');

const expiredEtWithValvontaId = 7;
const expiredEtWithoutValvontaId = 6;
const etToBeMadeExpiredId = 1;
const signedEtThatWillNotBeTouchedId = 4;
const signedEtThatWillNotBeTouched1 = 1;
const signedEtThatWillNotBeTouchedId2 = 4;

const runExpirationOfTodistukset = () => {
cy.request(
'POST',
`${backendUrl}/api/internal/energiatodistukset/anonymize-and-delete-expired`
).then(response => {
expect(response.status).to.eq(200);
});
// There is no way to know when running the expiration is finished.
cy.wait(1000);
};

// Only thing about applicationName that matters in this case is that it is parseable by the audit system.
// Now just put as <expiration-system-user-id>@cypress.
const applicationName = '-6@cypress';

context('Laatija', () => {
// Using just `before` here as we do not want to reset the database in
// between tests.
before(() => {
cy.resetDb();
});

beforeEach(() => {
cy.resetDb();
cy.intercept(/\/api\/private/, req => {
req.headers = { ...req.headers, ...FIXTURES.headers };
});
Expand All @@ -31,12 +37,12 @@ context('Laatija', () => {
cy.visit('/#/energiatodistus/all');

cy.get('[data-cy="energiatodistus-id"]')
.contains(etToBeMadeExpiredId)
.contains(signedEtThatWillNotBeTouched1)
.siblings('[data-cy="energiatodistus-tila"]')
.should('have.text', 'Voimassa');

cy.get('[data-cy="energiatodistus-id"]')
.contains(signedEtThatWillNotBeTouchedId)
.contains(signedEtThatWillNotBeTouchedId2)
.siblings('[data-cy="energiatodistus-tila"]')
.should('have.text', 'Voimassa');

Expand All @@ -51,33 +57,27 @@ context('Laatija', () => {
.should('have.text', 'Voimassa');
});

it('set the et 1 as expired and run expiration', () => {
const query =
"update etp.energiatodistus set voimassaolo_paattymisaika = now() - interval '2 days' where id = 1;";

cy.task('executeQuery', { query, applicationName }).then(() => {
cy.request(
'POST',
`${backendUrl}/api/internal/energiatodistukset/anonymize-and-delete-expired`
).then(response => {
expect(response.status).to.eq(200);
});
it('Should only see non-expired and expired with ongoing valvonta energiatodistukset after running expiration', () => {
cy.request(
'POST',
`${backendUrl}/api/internal/energiatodistukset/anonymize-and-delete-expired`
).then(response => {
expect(response.status).to.eq(200);
});

// There is no way to know when running the expiration is finished.
cy.wait(1000);
});

it('Should only see non-expired and expired with ongoing valvonta energiatodistukset', () => {
cy.visit('/#/energiatodistus/all');

cy.get('[data-cy="energiatodistus-id"]')
.contains(signedEtThatWillNotBeTouchedId)
.contains(signedEtThatWillNotBeTouchedId2)
.siblings('[data-cy="energiatodistus-tila"]')
.should('have.text', 'Voimassa');

cy.get('[data-cy="energiatodistus-id"]')
.contains(etToBeMadeExpiredId)
.should('not.exist');
.contains(signedEtThatWillNotBeTouched1)
.should('exist');

cy.get('[data-cy="energiatodistus-id"]')
.contains(expiredEtWithoutValvontaId)
Expand All @@ -87,26 +87,19 @@ context('Laatija', () => {
.contains(expiredEtWithValvontaId)
.siblings('[data-cy="energiatodistus-tila"]')
.should('have.text', 'Vanhentunut');
});

it('energiatodistus with valvonta can be still accessed', () => {
cy.visit(`/#/energiatodistus/2018/${expiredEtWithValvontaId}`);
cy.contains('Energiatodistus 2018/7 - Vanhentunut').should('exist');
});

it('energiatodistus without valvonta can not be accessed', () => {
cy.visit(`/#/energiatodistus/2018/${expiredEtWithoutValvontaId}`);
cy.contains('Puutteelliset käyttöoikeudet').should('exist');
});
});
});

context('Paakayttaja', () => {
before(() => {
cy.resetDb();
});

beforeEach(() => {
cy.resetDb();
cy.intercept(/\/api\/private/, req => {
req.headers = { ...req.headers, ...paakayttajaHeaders };
});
Expand All @@ -117,7 +110,7 @@ context('Paakayttaja', () => {
cy.visit('/#/energiatodistus/all');

cy.get('[data-cy="energiatodistus-id"]')
.contains(etToBeMadeExpiredId)
.contains(signedEtThatWillNotBeTouched1)
.siblings('[data-cy="energiatodistus-tila"]')
.should('have.text', 'Voimassa');

Expand All @@ -136,30 +129,17 @@ context('Paakayttaja', () => {
.siblings('[data-cy="energiatodistus-tila"]')
.should('have.text', 'Voimassa');
});
it('running the expiration should succeed', () => {
const query =
"update etp.energiatodistus set voimassaolo_paattymisaika = now() - interval '2 days' where id = 1;";
cy.task('executeQuery', { query, applicationName }).then(() => {
cy.request(
'POST',
`${backendUrl}/api/internal/energiatodistukset/anonymize-and-delete-expired`
).then(response => {
expect(response.status).to.eq(200);
});
});
// There is no way to know when running the expiration is finished.
cy.wait(1000);
});
it('should see 1 and 6 as tuhottu, 4 as voimassa and 7 as vahentunut', () => {
it('should see 1 and 6 as tuhottu, 4 as voimassa and 7 as vahentunut after running the expiration', () => {
runExpirationOfTodistukset();
cy.visit('/#/energiatodistus/all');

cy.get('[data-cy="energiatodistus-id"]')
.contains(etToBeMadeExpiredId)
.contains(signedEtThatWillNotBeTouched1)
.siblings('[data-cy="energiatodistus-tila"]')
.should('have.text', 'Tuhottu');
.should('have.text', 'Voimassa');

cy.get('[data-cy="energiatodistus-id"]')
.contains(signedEtThatWillNotBeTouchedId)
.contains(signedEtThatWillNotBeTouchedId2)
.siblings('[data-cy="energiatodistus-tila"]')
.should('have.text', 'Voimassa');

Expand All @@ -172,16 +152,12 @@ context('Paakayttaja', () => {
.contains(expiredEtWithValvontaId)
.siblings('[data-cy="energiatodistus-tila"]')
.should('have.text', 'Vanhentunut');
});
it('energiatodistus without valvonta can not be accessed', () => {
cy.visit('/#/energiatodistus/all');

cy.get('[data-cy="energiatodistus-id"]')
.contains(expiredEtWithoutValvontaId)
.click();
cy.contains('Puutteelliset käyttöoikeudet').should('exist');
});

it('energiatodistus with valvonta can still be accessed', () => {
cy.visit('/#/energiatodistus/all');
cy.get('[data-cy="energiatodistus-id"]')
.contains(expiredEtWithValvontaId)
Expand All @@ -192,7 +168,7 @@ context('Paakayttaja', () => {
});

context('Public', () => {
before(() => {
beforeEach(() => {
cy.resetDb();
});

Expand All @@ -212,28 +188,15 @@ context('Public', () => {
.should('exist');

cy.get('[data-cy="ethaku-tunnus"]')
.contains(signedEtThatWillNotBeTouchedId)
.contains(signedEtThatWillNotBeTouchedId2)
.should('exist');

cy.get('[data-cy="ethaku-tunnus"]')
.contains(etToBeMadeExpiredId)
.contains(signedEtThatWillNotBeTouched1)
.should('exist');
});
it('running the expiration should succeed', () => {
const query =
"update etp.energiatodistus set voimassaolo_paattymisaika = now() - interval '2 days' where id = 1;";
cy.task('executeQuery', { query, applicationName }).then(() => {
cy.request(
'POST',
`${backendUrl}/api/internal/energiatodistukset/anonymize-and-delete-expired`
).then(response => {
expect(response.status).to.eq(200);
});
});
// There is no way to know when running the expiration is finished.
cy.wait(1000);
});
it('should only see the not expired todistus.', () => {
it('should only see the non-expired todistukset after running the expiration.', () => {
runExpirationOfTodistukset();
cy.visit(`${publicUrl}/ethaku`);

cy.get('[data-cy="ethaku-hae"]').click();
Expand All @@ -248,12 +211,12 @@ context('Public', () => {
.should('not.exist');

cy.get('[data-cy="ethaku-tunnus"]')
.contains(signedEtThatWillNotBeTouchedId)
.contains(signedEtThatWillNotBeTouchedId2)
.should('exist');

cy.get('[data-cy="ethaku-tunnus"]')
.contains(etToBeMadeExpiredId)
.should('not.exist');
.contains(signedEtThatWillNotBeTouched1)
.should('exist');

cy.visit(
`${publicUrl}/energiatodistus?id=${expiredEtWithValvontaId}&versio=2018`
Expand All @@ -266,9 +229,10 @@ context('Public', () => {
cy.contains('Energiatodistusta ei löytynyt').should('exist');

cy.visit(
`${publicUrl}/energiatodistus?id=${etToBeMadeExpiredId}&versio=2013`
`${publicUrl}/energiatodistus?id=${signedEtThatWillNotBeTouched1}&versio=2013`
);
cy.contains('Energiatodistusta ei löytynyt').should('exist');
// This is a piece of the todistus' notes.
cy.contains('Seuraavia toimenpiteitä voisi tehdä:').should('exist');
});
});
});
Loading

0 comments on commit bcc4b52

Please sign in to comment.