123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- const apiUrl = Cypress.env('apiUrl');
- describe('Subscribers', () => {
- it('Opens subscribers page', () => {
- cy.resetDB();
- cy.loginAndVisit('/subscribers');
- });
- it('Counts subscribers', () => {
- cy.get('tbody td[data-label=Status]').its('length').should('eq', 2);
- });
- it('Searches subscribers', () => {
- const cases = [
- { value: 'john{enter}', count: 1, contains: 'john@example.com' },
- { value: 'anon{enter}', count: 1, contains: 'anon@example.com' },
- { value: '{enter}', count: 2, contains: null },
- ];
- cases.forEach((c) => {
- cy.get('[data-cy=search]').clear().type(c.value);
- cy.get('tbody td[data-label=Status]').its('length').should('eq', c.count);
- if (c.contains) {
- cy.get('tbody td[data-label=E-mail]').contains(c.contains);
- }
- });
- });
- it('Advanced searches subscribers', () => {
- cy.get('[data-cy=btn-advanced-search]').click();
- const cases = [
- { value: 'subscribers.attribs->>\'city\'=\'Bengaluru\'', count: 2 },
- { value: 'subscribers.attribs->>\'city\'=\'Bengaluru\' AND id=1', count: 1 },
- { value: '(subscribers.attribs->>\'good\')::BOOLEAN = true AND name like \'Anon%\'', count: 1 },
- ];
- cases.forEach((c) => {
- cy.get('[data-cy=query]').clear().type(c.value);
- cy.get('[data-cy=btn-query]').click();
- cy.get('tbody td[data-label=Status]').its('length').should('eq', c.count);
- });
- cy.get('[data-cy=btn-query-reset]').click();
- cy.wait(1000);
- cy.get('tbody td[data-label=Status]').its('length').should('eq', 2);
- });
- it('Does bulk subscriber list add and remove', () => {
- const cases = [
- // radio: action to perform, rows: table rows to select and perform on: [expected statuses of those rows after thea action]
- { radio: 'check-list-add', lists: [0, 1], rows: { 0: ['unconfirmed', 'unconfirmed'] } },
- { radio: 'check-list-unsubscribe', lists: [0, 1], rows: { 0: ['unsubscribed', 'unsubscribed'], 1: ['unsubscribed'] } },
- { radio: 'check-list-remove', lists: [0, 1], rows: { 1: [] } },
- { radio: 'check-list-add', lists: [0, 1], rows: { 0: ['unsubscribed', 'unsubscribed'], 1: ['unconfirmed', 'unconfirmed'] } },
- { radio: 'check-list-remove', lists: [0], rows: { 0: ['unsubscribed'] } },
- { radio: 'check-list-add', lists: [0], rows: { 0: ['unconfirmed', 'unsubscribed'] } },
- ];
- cases.forEach((c, n) => {
- // Select one of the 2 subscribers in the table.
- Object.keys(c.rows).forEach((r) => {
- cy.get('tbody td.checkbox-cell .checkbox').eq(r).click();
- });
- // Open the 'manage lists' modal.
- cy.get('[data-cy=btn-manage-lists]').click();
- // Check both lists in the modal.
- c.lists.forEach((l) => {
- cy.get('.list-selector input').click();
- cy.get('.list-selector .autocomplete a').first().click();
- });
- // Select the radio option in the modal.
- cy.get(`[data-cy=${c.radio}] .check`).click();
- // Save.
- cy.get('.modal button.is-primary').click();
- // Check the status of the lists on the subscriber.
- Object.keys(c.rows).forEach((r) => {
- cy.get('tbody td[data-label=E-mail]').eq(r).find('.tags').then(($el) => {
- cy.wrap($el).find('.tag').should('have.length', c.rows[r].length);
- c.rows[r].forEach((status, n) => {
- // eg: .tag(n).unconfirmed
- cy.wrap($el).find('.tag').eq(n).should('have.class', status);
- });
- });
- });
- });
- });
- it('Resets subscribers page', () => {
- cy.resetDB();
- cy.loginAndVisit('/subscribers');
- });
- it('Edits subscribers', () => {
- const status = ['enabled', 'blocklisted'];
- const json = '{"string": "hello", "ints": [1,2,3], "null": null, "sub": {"bool": true}}';
- // Collect values being edited on each sub to confirm the changes in the next step
- // index by their ID shown in the modal.
- const rows = {};
- // Open the edit popup and edit the default lists.
- cy.get('[data-cy=btn-edit]').each(($el, n) => {
- const email = `email-${n}@EMAIL.com`;
- const name = `name-${n}`;
- // Open the edit modal.
- cy.wrap($el).click();
- // Get the ID from the header and proceed to fill the form.
- let id = 0;
- cy.get('[data-cy=id]').then(($el) => {
- id = $el.text();
- cy.get('input[name=email]').clear().type(email);
- cy.get('input[name=name]').clear().type(name);
- cy.get('select[name=status]').select(status[n]);
- cy.get('.list-selector input').click();
- cy.get('.list-selector .autocomplete a').first().click();
- cy.get('textarea[name=attribs]').clear().type(json, { parseSpecialCharSequences: false, delay: 0 });
- cy.get('.modal-card-foot button[type=submit]').click();
- rows[id] = { email, name, status: status[n] };
- });
- });
- // Confirm the edits on the table.
- cy.wait(250);
- cy.get('tbody tr').each(($el) => {
- cy.wrap($el).find('td[data-id]').invoke('attr', 'data-id').then((id) => {
- cy.wrap($el).find('td[data-label=E-mail]').contains(rows[id].email.toLowerCase());
- cy.wrap($el).find('td[data-label=Name]').contains(rows[id].name);
- cy.wrap($el).find('td[data-label=Status]').contains(rows[id].status, { matchCase: false });
- // Both lists on the enabled sub should be 'unconfirmed' and the blocklisted one, 'unsubscribed.'
- cy.wrap($el).find(`.tags .${rows[id].status === 'enabled' ? 'unconfirmed' : 'unsubscribed'}`)
- .its('length').should('eq', 2);
- cy.wrap($el).find('td[data-label=Lists]').then((l) => {
- cy.expect(parseInt(l.text().trim())).to.equal(rows[id].status === 'blocklisted' ? 0 : 2);
- });
- });
- });
- });
- it('Deletes subscribers', () => {
- // Delete all visible lists.
- cy.get('tbody tr').each(() => {
- cy.get('tbody a[data-cy=btn-delete]').first().click();
- cy.get('.modal button.is-primary').click();
- });
- // Confirm deletion.
- cy.get('table tr.is-empty');
- });
- it('Creates new subscribers', () => {
- const statuses = ['enabled', 'blocklisted'];
- const lists = [[1], [2], [1, 2]];
- const json = '{"string": "hello", "ints": [1,2,3], "null": null, "sub": {"bool": true}}';
- // Cycle through each status and each list ID combination and create subscribers.
- const n = 0;
- for (let n = 0; n < 6; n++) {
- const email = `email-${n}@EMAIL.com`;
- const name = `name-${n}`;
- const status = statuses[(n + 1) % statuses.length];
- const list = lists[(n + 1) % lists.length];
- cy.get('[data-cy=btn-new]').click();
- cy.get('input[name=email]').type(email);
- cy.get('input[name=name]').type(name);
- cy.get('select[name=status]').select(status);
- list.forEach((l) => {
- cy.get('.list-selector input').click();
- cy.get('.list-selector .autocomplete a').first().click();
- });
- cy.get('textarea[name=attribs]').clear().type(json, { parseSpecialCharSequences: false, delay: 0 });
- cy.get('.modal-card-foot button[type=submit]').click();
- // Confirm the addition by inspecting the newly created list row,
- // which is always the first row in the table.
- cy.wait(250);
- const tr = cy.get('tbody tr:nth-child(1)').then(($el) => {
- cy.wrap($el).find('td[data-label=E-mail]').contains(email.toLowerCase());
- cy.wrap($el).find('td[data-label=Name]').contains(name);
- cy.wrap($el).find('td[data-label=Status]').contains(status, { matchCase: false });
- cy.wrap($el).find(`.tags .${status === 'enabled' ? 'unconfirmed' : 'unsubscribed'}`)
- .its('length').should('eq', list.length);
- cy.wrap($el).find('td[data-label=Lists]').then((l) => {
- cy.expect(parseInt(l.text().trim())).to.equal(status === 'blocklisted' ? 0 : list.length);
- });
- });
- }
- });
- it('Sorts subscribers', () => {
- const asc = [3, 4, 5, 6, 7, 8];
- const desc = [8, 7, 6, 5, 4, 3];
- const cases = ['cy-status', 'cy-email', 'cy-name', 'cy-created_at', 'cy-updated_at'];
- cases.forEach((c) => {
- cy.sortTable(`thead th.${c}`, asc);
- cy.wait(250);
- cy.sortTable(`thead th.${c}`, desc);
- cy.wait(250);
- });
- });
- });
- describe('Domain blocklist', () => {
- it('Opens settings page', () => {
- cy.resetDB();
- });
- it('Add domains to blocklist', () => {
- cy.loginAndVisit('/settings');
- cy.get('.b-tabs nav a').eq(2).click();
- cy.get('textarea[name="privacy.domain_blocklist"]').clear().type('ban.net\n\nBaN.OrG\n\nban.com\n\n');
- cy.get('[data-cy=btn-save]').click();
- });
- it('Try subscribing via public page', () => {
- cy.visit(`${apiUrl}/subscription/form`);
- cy.get('input[name=email]').clear().type('test@noban.net');
- cy.get('button[type=submit]').click();
- cy.get('h2').contains('Subscribe');
- cy.visit(`${apiUrl}/subscription/form`);
- cy.get('input[name=email]').clear().type('test@ban.net');
- cy.get('button[type=submit]').click();
- cy.get('h2').contains('Error');
- });
- // Post to the admin API.
- it('Try via admin API', () => {
- cy.wait(1000);
- // Add non-banned domain.
- cy.request({
- method: 'POST', url: `${apiUrl}/api/subscribers`, failOnStatusCode: true,
- body: { email: 'test1@noban.net', 'name': 'test', 'lists': [1], 'status': 'enabled' }
- }).should((response) => {
- expect(response.status).to.equal(200);
- });
- // Add banned domain.
- cy.request({
- method: 'POST', url: `${apiUrl}/api/subscribers`, failOnStatusCode: false,
- body: { email: 'test1@ban.com', 'name': 'test', 'lists': [1], 'status': 'enabled' }
- }).should((response) => {
- expect(response.status).to.equal(400);
- });
- // Modify an existinb subscriber to a banned domain.
- cy.request({
- method: 'PUT', url: `${apiUrl}/api/subscribers/1`, failOnStatusCode: false,
- body: { email: 'test3@ban.org', 'name': 'test', 'lists': [1], 'status': 'enabled' }
- }).should((response) => {
- expect(response.status).to.equal(400);
- });
- });
- it('Try via import', () => {
- cy.loginAndVisit('/subscribers/import');
- cy.get('.list-selector input').click();
- cy.get('.list-selector .autocomplete a').first().click();
- cy.fixture('subs-domain-blocklist.csv').then((data) => {
- cy.get('input[type="file"]').attachFile({
- fileContent: data.toString(),
- fileName: 'subs.csv',
- mimeType: 'text/csv',
- });
- });
- cy.get('button.is-primary').click();
- cy.get('section.wrap .has-text-success');
- // cy.get('button.is-primary').click();
- cy.get('.log-view').should('contain', 'ban1-import@BAN.net').and('contain', 'ban2-import@ban.ORG');
- cy.wait(100);
- });
- it('Clear blocklist and try', () => {
- cy.loginAndVisit('/settings');
- cy.get('.b-tabs nav a').eq(2).click();
- cy.get('textarea[name="privacy.domain_blocklist"]').clear();
- cy.get('[data-cy=btn-save]').click();
- cy.wait(1000);
- // Add banned domain.
- cy.request({
- method: 'POST', url: `${apiUrl}/api/subscribers`, failOnStatusCode: true,
- body: { email: 'test4@BAN.com', 'name': 'test', 'lists': [1], 'status': 'enabled' }
- }).should((response) => {
- expect(response.status).to.equal(200);
- });
- // Modify an existinb subscriber to a banned domain.
- cy.request({
- method: 'PUT', url: `${apiUrl}/api/subscribers/1`, failOnStatusCode: true,
- body: { email: 'test4@BAN.org', 'name': 'test', 'lists': [1], 'status': 'enabled' }
- }).should((response) => {
- expect(response.status).to.equal(200);
- });
- });
- });
|