close

Endpoint Security Pembroke

1 Flares Twitter 0 Facebook 1 Filament.io 1 Flares

Below is a test script which will:

    Load a page with a form Confirm the form exists Capture CSRF token Build Ajax request Send form to REST endpoint

Forms on these pages use Entities and Form classes and are rendered using Twig, however, due to design requirement they are loaded in modals and must send their data using XHR request. Below test attempt to mimic user behaviour by loading the page and sending form data in a post the way user would.

casper.test.begin('Test Registration', 4, function suite(test) {    casper.start("function() {        test.assertExists('form[id="register_form"]', "registration form exists");		test.assertExists('#registration__token', "registration form CSRF token exists");	});	casper.then(function() {		var endPoint = '		//CAPTURE CSFR TOKEN                var formToken = this.evaluate(function() {			return document.querySelector('#registration__token').getAttribute('value');		});		var ts = new Date().getTime();		var data = 'registration[email][first]=casper'  ts &';		data = 'registration[email][second]=casper'  ts &';		data = 'registration[firstname]=Casper&';		data = 'registration[lastname]=TheGhost&';		data = 'registration[phone]=4165558899&';		data = 'registration[password][first]=1234&';		data = 'registration[password][second]=1234&';		data = 'registration[_token]='  formToken;		resp = this.evaluate(function(wsurl, data) {			try {				//returnJSON.parse(utils.sendAJAX(wsurl, 'POST', null, false));				return __utils__.sendAJAX(wsurl, 'POST', data, false);			} catch (e) {				this.echo(e);			}		}, {wsurl: endPoint, data: data});		this.echo(resp);		this.echo("API STATUS: "  JSON.parse(resp).status);		this.echo("REGISTRATION STATUS: "  JSON.parse(resp).data.registration);		this.echo("REGISTRATION ERROR: "  JSON.parse(resp).data.errortype);		test.assertEquals(JSON.parse(resp).status, "ok", "API status");		test.assertEquals(JSON.parse(resp).data.registration, 1, "Registration status");	});    casper.run(function() {        test.done();    });});

For your reference the response object looks like this:

{"status":"ok","info":"","data":{"registration":1,"validationErrorCount":0,"errors":[]}}

endpoint security download checkpoint     endpoint security client

TAGS

CATEGORIES