Skip to content

Commit

Permalink
Add browser tests for existing user booking flow
Browse files Browse the repository at this point in the history
  • Loading branch information
bfirsh committed Feb 27, 2019
1 parent 012b254 commit 7ab41fd
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
2 changes: 1 addition & 1 deletion core/factory_apps/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Meta:
first_name = factory.Faker('first_name')
last_name = factory.Faker('last_name')
email = factory.Faker('email')
is_staff = factory.Faker('pybool')
is_staff = False
is_active = True
is_superuser = False

Expand Down
5 changes: 4 additions & 1 deletion core/management/commands/generate_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from core.factory_apps.communication import EmailtemplateFactory
from core.factory_apps.location import LocationFactory, ResourceFactory
from core.factory_apps.user import SuperUserFactory
from core.factory_apps.user import SuperUserFactory, UserFactory


class Command(BaseCommand):
Expand All @@ -24,6 +24,9 @@ def handle(self, *args, **options):
# all locations.
SuperUserFactory()

# known normal users
UserFactory(username="pixel", first_name="Pixel", last_name="McPixelston")

# communication. these emails will also generate users.
EmailtemplateFactory()

Expand Down
50 changes: 49 additions & 1 deletion cypress/integration/booking_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const randomstring = require("randomstring");

describe("Booking a room", function() {
it("New user flow", function() {
it("works for a new user", function() {
cy.visit("/");
cy.contains("Embassy SF").click();
cy.contains("View all rooms").click();
Expand Down Expand Up @@ -38,4 +38,52 @@ describe("Booking a room", function() {

cy.contains("Your booking has been submitted.");
});

it("works for an existing logged in user", function() {
cy.login("pixel", "password")
cy.visit("/");
cy.contains("Embassy SF").click();
cy.contains("View all rooms").click();
cy.contains("Ada Lovelace").click({ force: true });

cy.get("input[name=arrive]").focus();
// Pick next month so all the dates are available
cy.get(".react-datepicker__navigation--next").click({ force: true });
cy.get("[aria-label=day-13]").click({ force: true });
// Force de-select first date picker. For some reason immediately selecting second date
// picker causes first not to close.
cy.get("#network-footer").click("topLeft");
cy.get("input[name=depart]").focus();
cy.get("[aria-label=day-16]").click({ force: true });
cy.get("[name=purpose]").type("Work.");
cy.contains("Request to Book").click();

cy.contains("Your booking was submitted.");
});

it("works for an existing logged out user", function() {
cy.visit("/");
cy.contains("Embassy SF").click();
cy.contains("View all rooms").click();
cy.contains("Ada Lovelace").click({ force: true });

cy.get("input[name=arrive]").focus();
// Pick next month so all the dates are available
cy.get(".react-datepicker__navigation--next").click({ force: true });
cy.get("[aria-label=day-13]").click({ force: true });
// Force de-select first date picker. For some reason immediately selecting second date
// picker causes first not to close.
cy.get("#network-footer").click("topLeft");
cy.get("input[name=depart]").focus();
cy.get("[aria-label=day-16]").click({ force: true });
cy.get("[name=purpose]").type("Work.");
cy.contains("Request to Book").click();

cy.contains("log in").click();
cy.get("[name=username]").type("pixel");
cy.get("[name=password]").type("password");
cy.get("button[type=submit]").click();

cy.contains("Your booking has been submitted.");
});
});
29 changes: 29 additions & 0 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,32 @@ Cypress.Commands.add("uploadFile", (fileName, selector) => {
});
});
});

// https://docs.cypress.io/guides/getting-started/testing-your-app.html#Logging-in
// https://github.com/cypress-io/cypress-example-recipes/blob/master/examples/logging-in__csrf-tokens/cypress/integration/logging-in-csrf-tokens-spec.js
Cypress.Commands.add("login", (username, password) => {
return cy
.request("/people/login/")
.its("body")
.then(body => {
const $html = Cypress.$(body);
const csrf = $html.find("input[name=csrfmiddlewaretoken]").val();

return cy
.request({
method: "POST",
url: "/people/login/",
form: true,
followRedirect: false,
body: {
username: username,
password: password,
csrfmiddlewaretoken: csrf
}
})
.then(resp => {
// Redirect to home page = success
expect(resp.status).to.eq(302);
});
});
});

0 comments on commit 7ab41fd

Please sign in to comment.