Browse Source

Cypress reset DB action, cypress tests

Vova Tkach 6 years ago
parent
commit
2b3127281f

+ 19 - 17
modules/module_index_act_cypress.go

@@ -1,6 +1,9 @@
 package modules
 
 import (
+	"database/sql"
+	_ "github.com/go-sql-driver/mysql"
+
 	"os"
 
 	"golang-fave/consts"
@@ -9,7 +12,7 @@ import (
 
 func (this *Modules) RegisterAction_IndexCypressReset() *Action {
 	return this.newAction(AInfo{
-		WantDB: true,
+		WantDB: false,
 		Mount:  "index-cypress-reset",
 	}, func(wrap *wrapper.Wrapper) {
 		if !consts.ParamDebug {
@@ -17,31 +20,30 @@ func (this *Modules) RegisterAction_IndexCypressReset() *Action {
 			return
 		}
 
-		_, err := wrap.DB.Query(
-			`DROP TABLE
-				blog_cats,
-				blog_cat_post_rel,
-				blog_posts,
-				pages,
-				users
-			;`,
-		)
+		db, err := sql.Open("mysql", "root:root@tcp(localhost:3306)/fave")
 		if err != nil {
 			wrap.Write(err.Error())
 			return
 		}
-
-		err = os.Remove(wrap.DConfig + string(os.PathSeparator) + ".installed")
+		defer db.Close()
+		err = db.Ping()
 		if err != nil {
 			wrap.Write(err.Error())
 			return
 		}
 
-		err = os.Remove(wrap.DConfig + string(os.PathSeparator) + "mysql.json")
-		if err != nil {
-			wrap.Write(err.Error())
-			return
-		}
+		os.Remove(wrap.DConfig + string(os.PathSeparator) + ".installed")
+		os.Remove(wrap.DConfig + string(os.PathSeparator) + "mysql.json")
+
+		db.Query(
+			`DROP TABLE
+				blog_cats,
+				blog_cat_post_rel,
+				blog_posts,
+				pages,
+				users
+			;`,
+		)
 
 		wrap.Write(`OK`)
 	})

+ 1 - 0
testing/cypress.json

@@ -0,0 +1 @@
+{}

+ 52 - 0
testing/cypress/integration/control-panel/001_module_index/001_install_mysql.spec.js

@@ -0,0 +1,52 @@
+/// <reference types="Cypress" />
+
+context('Install MySQL, create first user and login', () => {
+  it('should do redirect to cp panel', () => {
+    cy.resetCMS();
+    cy.visit('http://localhost:8080/');
+    cy.url().should('eq', 'http://localhost:8080/cp/');
+  });
+
+  it('should configure mysql config', () => {
+    cy.actionStart();
+
+    cy.get('.form-signin input[type=text]').should('have.length', 4);
+    cy.get('.form-signin input[type=password]').should('have.length', 1);
+    cy.get('.form-signin button').should('have.length', 1);
+
+    cy.get('.form-signin input[name=name]').type('fave');
+    cy.get('.form-signin input[name=user]').type('root');
+    cy.get('.form-signin input[name=password]').type('root');
+    cy.get('.form-signin button').click();
+    cy.actionWait();
+  });
+
+  it('should create first user', () => {
+    cy.actionStart();
+
+    cy.get('.form-signin input[type=text]').should('have.length', 2);
+    cy.get('.form-signin input[type=email]').should('have.length', 1);
+    cy.get('.form-signin input[type=password]').should('have.length', 1);
+    cy.get('.form-signin button').should('have.length', 1);
+
+    cy.get('.form-signin input[name=first_name]').type('First');
+    cy.get('.form-signin input[name=last_name]').type('Last');
+    cy.get('.form-signin input[name=email]').type('example@example.com');
+    cy.get('.form-signin input[name=password]').type('example@example.com');
+    cy.get('.form-signin button').click();
+    cy.actionWait();
+  });
+
+  it('should login to control panel', () => {
+    cy.actionStart();
+
+    cy.get('.form-signin input[type=email]').should('have.length', 1);
+    cy.get('.form-signin input[type=password]').should('have.length', 1);
+    cy.get('.form-signin button').should('have.length', 1);
+
+    cy.get('.form-signin input[name=email]').type('example@example.com');
+    cy.get('.form-signin input[name=password]').type('example@example.com');
+    cy.get('.form-signin button').click();
+    cy.actionWait();
+  });
+});

+ 55 - 0
testing/cypress/integration/control-panel/001_module_index/002_top_nav_bar.js

@@ -0,0 +1,55 @@
+/// <reference types="Cypress" />
+
+context('Top navigation bar', () => {
+  it('should reset', () => {
+    cy.installCMS();
+  });
+
+  it('should render top nav bar', () => {
+    cy.loginCMS();
+    cy.get('#navbarCollapse ul.navbar-nav').should('have.length', 2);
+  });
+
+  it('should render modules menu', () => {
+    cy.loginCMS();
+    cy.get('#navbarCollapse ul.navbar-nav:nth-child(1) li.nav-item').should('have.length', 2);
+    cy.get('#navbarCollapse ul.navbar-nav:nth-child(1) li.nav-item:nth-child(1) a.nav-link').should('exist');
+    cy.get('#navbarCollapse ul.navbar-nav:nth-child(1) li.nav-item:nth-child(2) a.nav-link').should('exist');
+  });
+
+  it('should render user menu', () => {
+    cy.loginCMS();
+    cy.get('#navbarCollapse ul.navbar-nav:nth-child(2) li.nav-item').should('have.length', 1);
+    cy.get('#navbarCollapse ul.navbar-nav:nth-child(2) li.nav-item:nth-child(1) a.nav-link').should('contain', 'example@example.com');
+  });
+
+  it('should render user profile modal dialog', () => {
+    cy.loginCMS();
+    cy.get('#navbarCollapse ul.navbar-nav:nth-child(2) li.nav-item:nth-child(1) a.nav-link:first-child').click();
+    cy.get('#navbarCollapse ul.navbar-nav:nth-child(2) li.nav-item:nth-child(1) div.dropdown-menu a.dropdown-item:nth-child(1)').click();
+
+    cy.get('#sys-modal-user-settings').should('exist');
+    cy.get('#sys-modal-user-settings form input[type=text]').should('have.length', 2);
+    cy.get('#sys-modal-user-settings form input[type=email]').should('have.length', 1);
+    cy.get('#sys-modal-user-settings form input[type=password]').should('have.length', 1);
+
+    cy.get('#sys-modal-user-settings form input[name=first_name]').clear().type('FirstNew');
+    cy.get('#sys-modal-user-settings form input[name=last_name]').clear().type('LastNew');
+
+    cy.get('#sys-modal-user-settings form button[type=submit].btn').click();
+    cy.actionWait();
+
+    cy.get('#navbarCollapse ul.navbar-nav:nth-child(2) li.nav-item:nth-child(1) a.nav-link:first-child').click();
+    cy.get('#navbarCollapse ul.navbar-nav:nth-child(2) li.nav-item:nth-child(1) div.dropdown-menu a.dropdown-item:nth-child(1)').click();
+
+    cy.get('#sys-modal-user-settings').should('exist');
+    cy.get('#sys-modal-user-settings form input[name=first_name]').should('have.value', 'FirstNew');
+    cy.get('#sys-modal-user-settings form input[name=last_name]').should('have.value', 'LastNew');
+
+    cy.get('#sys-modal-user-settings form input[name=first_name]').clear().type('First');
+    cy.get('#sys-modal-user-settings form input[name=last_name]').clear().type('Last');
+
+    cy.get('#sys-modal-user-settings form button[type=submit].btn').click();
+    cy.actionWait();
+  });
+});

+ 64 - 0
testing/cypress/integration/control-panel/001_module_index/003_pages.js

@@ -0,0 +1,64 @@
+/// <reference types="Cypress" />
+
+context('Module pages', () => {
+  it('should reset', () => {
+    cy.installCMS();
+  });
+
+  it('should render data table', () => {
+    cy.loginCMS();
+    cy.visit('http://localhost:8080/cp/');
+    cy.get('table#cp-table-pages thead tr').should('have.length', 1);
+    cy.get('table#cp-table-pages thead tr th').should('have.length', 4);
+    cy.get('table#cp-table-pages tbody tr').should('have.length', 3);
+    cy.get('table#cp-table-pages tbody tr:nth-child(1) td').should('have.length', 4);
+  });
+
+  it('should render data form', () => {
+    cy.loginCMS();
+    cy.visit('http://localhost:8080/cp/index/add/');
+    
+    cy.get('.data-form.index-add input[type=text]').should('have.length', 4);
+    cy.get('.data-form.index-add textarea').should('have.length', 2);
+    cy.get('.data-form.index-add input[type=checkbox]').should('have.length', 1);
+  });
+
+  it('should not add new page', () => {
+    cy.loginCMS();
+    cy.visit('http://localhost:8080/cp/index/add/');
+    cy.get('#add-edit-button').click();
+    cy.actionWait();
+
+    cy.get('.data-form.index-add div.sys-messages').should('exist');
+  });
+
+  it('should add new page', () => {
+    cy.loginCMS();
+    cy.visit('http://localhost:8080/cp/index/add/');
+    
+    cy.get('.data-form.index-add input[name=name]').clear().type('Some test page');
+    cy.get('.data-form.index-add textarea[name=content]').clear().type('Some test content');
+    cy.get('.data-form.index-add input[name=meta_title]').clear().type('Page meta title');
+    cy.get('.data-form.index-add input[name=meta_keywords]').clear().type('Page meta keywords');
+    cy.get('.data-form.index-add textarea[name=meta_description]').clear().type('Page meta description');
+    cy.get('.data-form.index-add label[for=lbl_active]').click();
+    cy.get('#add-edit-button').click();
+    cy.actionWait();
+  });
+
+  it('should render added page', () => {
+    cy.loginCMS();
+    cy.visit('http://localhost:8080/cp/');
+    cy.get('table#cp-table-pages tbody tr').should('have.length', 4);
+    cy.get('table#cp-table-pages tbody tr:nth-child(1) td:nth-child(1)').should('contain', 'Some test page');
+    cy.get('table#cp-table-pages tbody tr:nth-child(1) td:nth-child(3) .svg-green').should('exist');
+  });
+
+  it('should delete added page', () => {
+    cy.loginCMS();
+    cy.visit('http://localhost:8080/cp/');
+    cy.get('table#cp-table-pages tbody tr:nth-child(1) td:nth-child(4) a.ico.delete').click();
+    cy.actionWait();
+    cy.get('table#cp-table-pages tbody tr').should('have.length', 3);
+  });
+});

+ 17 - 0
testing/cypress/plugins/index.js

@@ -0,0 +1,17 @@
+// ***********************************************************
+// This example plugins/index.js can be used to load plugins
+//
+// You can change the location of this file or turn off loading
+// the plugins file with the 'pluginsFile' configuration option.
+//
+// You can read more here:
+// https://on.cypress.io/plugins-guide
+// ***********************************************************
+
+// This function is called when a project is opened or re-opened (e.g. due to
+// the project's config changing)
+
+module.exports = (on, config) => {
+  // `on` is used to hook into various events Cypress emits
+  // `config` is the resolved Cypress config
+}

+ 79 - 0
testing/cypress/support/commands.js

@@ -0,0 +1,79 @@
+// ***********************************************
+// This example commands.js shows you how to
+// create various custom commands and overwrite
+// existing commands.
+//
+// For more comprehensive examples of custom
+// commands please read more here:
+// https://on.cypress.io/custom-commands
+// ***********************************************
+//
+//
+// -- This is a parent command --
+// Cypress.Commands.add("login", (email, password) => { ... })
+//
+//
+// -- This is a child command --
+// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
+//
+//
+// -- This is a dual command --
+// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
+//
+//
+// -- This is will overwrite an existing command --
+// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
+
+Cypress.Commands.add('actionStart', () => {
+  cy.server();
+  cy.route({
+    method: 'POST',
+    url: '/*',
+  }).as('formAction');
+});
+
+Cypress.Commands.add('actionWait', () => {
+  cy.wait('@formAction');
+});
+
+Cypress.Commands.add('resetCMS', () => {
+  cy.request({
+    method: 'POST',
+    url: 'http://localhost:8080/',
+    form: true,
+    body: {
+      action: 'index-cypress-reset',
+    }
+  }).then((response) => {
+    expect(response.body).to.eq('OK');
+  });
+});
+
+Cypress.Commands.add('installCMS', () => {
+  cy.actionStart();
+  cy.resetCMS();
+
+  cy.visit('http://localhost:8080/cp/');
+  cy.get('.form-signin input[name=name]').type('fave');
+  cy.get('.form-signin input[name=user]').type('root');
+  cy.get('.form-signin input[name=password]').type('root');
+  cy.get('.form-signin button').click();
+  cy.actionWait();
+
+  cy.visit('http://localhost:8080/cp/');
+  cy.get('.form-signin input[name=first_name]').type('First');
+  cy.get('.form-signin input[name=last_name]').type('Last');
+  cy.get('.form-signin input[name=email]').type('example@example.com');
+  cy.get('.form-signin input[name=password]').type('example@example.com');
+  cy.get('.form-signin button').click();
+  cy.actionWait();
+});
+
+Cypress.Commands.add('loginCMS', () => {
+  cy.actionStart();
+  cy.visit('http://localhost:8080/cp/');
+  cy.get('.form-signin input[name=email]').type('example@example.com');
+  cy.get('.form-signin input[name=password]').type('example@example.com');
+  cy.get('.form-signin button').click();
+  cy.actionWait();
+});

+ 20 - 0
testing/cypress/support/index.js

@@ -0,0 +1,20 @@
+// ***********************************************************
+// This example support/index.js is processed and
+// loaded automatically before your test files.
+//
+// This is a great place to put global configuration and
+// behavior that modifies Cypress.
+//
+// You can change the location of this file or turn off
+// automatically serving support files with the
+// 'supportFile' configuration option.
+//
+// You can read more here:
+// https://on.cypress.io/configuration
+// ***********************************************************
+
+// Import commands.js using ES2015 syntax:
+import './commands'
+
+// Alternatively you can use CommonJS syntax:
+// require('./commands')