diff --git a/cypress/integration/api.js b/cypress/integration/api.js new file mode 100644 index 0000000000..2db09aec1b --- /dev/null +++ b/cypress/integration/api.js @@ -0,0 +1,38 @@ +context('API Resources', () => { + before(() => { + cy.visit('/login'); + cy.login(); + cy.visit('/desk'); + }); + + it('Creates two Comments', () => { + cy.create_doc('Comment', {comment_type: 'Comment', content: "hello"}); + cy.create_doc('Comment', {comment_type: 'Comment', content: "world"}); + }); + + it('Lists the Comments', () => { + cy.get_list('Comment') + .its('data') + .then(data => expect(data.length).to.be.at.least(2)); + + cy.get_list('Comment', ['name', 'content'], [['content', '=', 'hello']]) + .then(body => { + expect(body).to.have.property('data'); + expect(body.data).to.have.lengthOf(1); + expect(body.data[0]).to.have.property('content'); + expect(body.data[0]).to.have.property('name'); + }); + }); + + it('Gets each Comment', () => { + cy.get_list('Comment').then(body => body.data.forEach(comment => { + cy.get_doc('Comment', comment.name); + })); + }); + + it('Removes the Comments', () => { + cy.get_list('Comment').then(body => body.data.forEach(comment => { + cy.remove_doc('Comment', comment.name); + })); + }); +}); diff --git a/cypress/support/commands.js b/cypress/support/commands.js index 84d896dbb0..464cbbe1d5 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -59,6 +59,72 @@ Cypress.Commands.add('call', (method, args) => { }); }); +Cypress.Commands.add('get_list', (doctype, fields=[], filters=[]) => { + return cy.window().its('frappe.csrf_token').then(csrf_token => { + return cy.request({ + method: 'GET', + url: `/api/resource/${doctype}?fields=${JSON.stringify(fields)}&filters=${JSON.stringify(filters)}`, + headers: { + 'Accept': 'application/json', + 'X-Frappe-CSRF-Token': csrf_token + } + }).then(res => { + expect(res.status).eq(200); + return res.body; + }); + }); +}); + +Cypress.Commands.add('get_doc', (doctype, name) => { + return cy.window().its('frappe.csrf_token').then(csrf_token => { + return cy.request({ + method: 'GET', + url: `/api/resource/${doctype}/${name}`, + headers: { + 'Accept': 'application/json', + 'X-Frappe-CSRF-Token': csrf_token + } + }).then(res => { + expect(res.status).eq(200); + return res.body; + }); + }); +}); + +Cypress.Commands.add('create_doc', (doctype, args) => { + return cy.window().its('frappe.csrf_token').then(csrf_token => { + return cy.request({ + method: 'POST', + url: `/api/resource/${doctype}`, + body: args, + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + 'X-Frappe-CSRF-Token': csrf_token + } + }).then(res => { + expect(res.status).eq(200); + return res.body; + }); + }); +}); + +Cypress.Commands.add('remove_doc', (doctype, name) => { + return cy.window().its('frappe.csrf_token').then(csrf_token => { + return cy.request({ + method: 'DELETE', + url: `/api/resource/${doctype}/${name}`, + headers: { + 'Accept': 'application/json', + 'X-Frappe-CSRF-Token': csrf_token + } + }).then(res => { + expect(res.status).eq(202); + return res.body; + }); + }); +}); + Cypress.Commands.add('create_records', (doc) => { return cy.call('frappe.tests.ui_test_helpers.create_if_not_exists', { doc }) .then(r => r.message);