Skip to content

Commit

Permalink
lint: Format javascript test code
Browse files Browse the repository at this point in the history
Signed-off-by: aeneasr <aeneas@ory.sh>
  • Loading branch information
aeneasr committed Apr 27, 2019
1 parent 9851f9b commit 9e829a9
Show file tree
Hide file tree
Showing 6 changed files with 342 additions and 324 deletions.
49 changes: 28 additions & 21 deletions cypress/helpers/index.js
Expand Up @@ -3,23 +3,31 @@ export const prng = () =>
.toString(36)
.substring(2)}${Math.random()
.toString(36)
.substring(2)}`
.substring(2)}`;

const isStatusOk = res =>
res.ok
? Promise.resolve(res)
: Promise.reject(
new Error(`Received unexpected status code ${res.statusCode}`)
)
new Error(`Received unexpected status code ${res.statusCode}`)
);

export const findEndUserAuthorization = subject =>
fetch(Cypress.env('admin_url') + '/oauth2/auth/sessions/consent?subject='+subject)
fetch(
Cypress.env('admin_url') +
'/oauth2/auth/sessions/consent?subject=' +
subject
)
.then(isStatusOk)
.then((res) => res.json())
.then(res => res.json());

export const revokeEndUserAuthorization = subject =>
fetch(Cypress.env('admin_url') + '/oauth2/auth/sessions/consent?subject='+subject, { method: 'DELETE' })
.then(isStatusOk)
fetch(
Cypress.env('admin_url') +
'/oauth2/auth/sessions/consent?subject=' +
subject,
{ method: 'DELETE' }
).then(isStatusOk);

export const createClient = client =>
fetch(Cypress.env('admin_url') + '/clients', {
Expand All @@ -29,7 +37,7 @@ export const createClient = client =>
})
.then(isStatusOk)
.then(res => {
return res.json()
return res.json();
})
.then(body =>
getClient(client.client_id).then(actual => {
Expand All @@ -38,32 +46,31 @@ export const createClient = client =>
new Error(
`Expected client_id's to match: ${actual.client_id} !== ${
body.client
}`
}`
)
)
);
}

return Promise.resolve(body)
return Promise.resolve(body);
})
)
);

export const deleteClients = () =>
fetch(Cypress.env('admin_url') + '/clients', {
method: 'GET',
method: 'GET'
})
.then(isStatusOk)
.then((res) => res.json())
.then((body) => {
body.forEach(({ client_id }) => deleteClient(client_id))
})
.then(res => res.json())
.then(body => {
body.forEach(({ client_id }) => deleteClient(client_id));
});

const deleteClient = client_id =>
fetch(Cypress.env('admin_url') + '/clients/' + client_id, {
method: 'DELETE',
})
.then(isStatusOk)
method: 'DELETE'
}).then(isStatusOk);

const getClient = id =>
fetch(Cypress.env('admin_url') + '/clients/' + id)
.then(isStatusOk)
.then((res) => res.json())
.then(res => res.json());
88 changes: 44 additions & 44 deletions cypress/integration/oauth2/authorize_error.js
@@ -1,5 +1,5 @@
import { createClient, prng } from '../../helpers'
import qs from 'querystring'
import { createClient, prng } from '../../helpers';
import qs from 'querystring';

describe('OAuth 2.0 Authorization Endpoint Error Handling', () => {
it('should return an error when an OAuth 2.0 Client ID is used that does not exist', () => {
Expand All @@ -8,16 +8,16 @@ describe('OAuth 2.0 Authorization Endpoint Error Handling', () => {
'client_url'
)}/oauth2/code?client_id=i-do-not-exist&client_secret=i-am-not-correct}`,
{ failOnStatusCode: false }
)
);

cy.location().should(({ search, port }) => {
const query = qs.parse(search.substr(1))
expect(query.error).to.equal('invalid_client')
const query = qs.parse(search.substr(1));
expect(query.error).to.equal('invalid_client');

// Should show ORY Hydra's Error URL because a redirect URL could not be determined
expect(port).to.equal(Cypress.env('public_port'))
})
})
expect(port).to.equal(Cypress.env('public_port'));
});
});

it('should return an error when an OAuth 2.0 Client requests a scope that is not allowed to be requested', () => {
const c = {
Expand All @@ -26,91 +26,91 @@ describe('OAuth 2.0 Authorization Endpoint Error Handling', () => {
scope: 'foo',
redirect_uris: [`${Cypress.env('client_url')}/oauth2/callback`],
grant_types: ['authorization_code']
}
cy.wrap(createClient(c))
};
cy.wrap(createClient(c));

cy.visit(
`${Cypress.env('client_url')}/oauth2/code?client_id=${
c.client_id
}&client_secret=${c.client_secret}&scope=bar`,
}&client_secret=${c.client_secret}&scope=bar`,
{ failOnStatusCode: false }
)
);

cy.location().should(({ search, port }) => {
const query = qs.parse(search.substr(1))
expect(query.error).to.equal('invalid_scope')
const query = qs.parse(search.substr(1));
expect(query.error).to.equal('invalid_scope');

// This is a client error so we expect the client app to show the error
expect(port).to.equal(Cypress.env('client_port'))
})
})
expect(port).to.equal(Cypress.env('client_port'));
});
});

it('should return an error when an OAuth 2.0 Client requests a response type it is not allowed to call', () => {
const c = {
client_id: prng(),
client_secret: prng(),
redirect_uris: [`${Cypress.env('client_url')}/oauth2/callback`],
response_types: ['token'] // disallows Authorization Code Grant
}
cy.wrap(createClient(c))
};
cy.wrap(createClient(c));

cy.visit(
`${Cypress.env('client_url')}/oauth2/code?client_id=${
c.client_id
}&client_secret=${c.client_secret}`,
}&client_secret=${c.client_secret}`,
{ failOnStatusCode: false }
)
);

cy.get('body').should('contain', 'unsupported_response_type')
})
cy.get('body').should('contain', 'unsupported_response_type');
});

it('should return an error when an OAuth 2.0 Client requests a grant type it is not allowed to call', () => {
const c = {
client_id: prng(),
client_secret: prng(),
redirect_uris: [`${Cypress.env('client_url')}/oauth2/callback`],
grant_types: ['client_credentials']
}
cy.wrap(createClient(c))
};
cy.wrap(createClient(c));

cy.visit(
`${Cypress.env('client_url')}/oauth2/code?client_id=${
c.client_id
}&client_secret=${c.client_secret}&scope=`,
}&client_secret=${c.client_secret}&scope=`,
{ failOnStatusCode: false }
)
);

cy.get('#email').type('foo@bar.com', { delay: 1 })
cy.get('#password').type('foobar', { delay: 1 })
cy.get('#accept').click()
cy.get('#accept').click()
cy.get('#email').type('foo@bar.com', { delay: 1 });
cy.get('#password').type('foobar', { delay: 1 });
cy.get('#accept').click();
cy.get('#accept').click();

cy.get('body').should('contain', 'invalid_grant')
})
cy.get('body').should('contain', 'invalid_grant');
});

it('should return an error when an OAuth 2.0 Client requests a redirect_uri that is not preregistered', () => {
const c = {
client_id: prng(),
client_secret: prng(),
redirect_uris: ['http://some-other-domain/not-callback'],
grant_types: ['client_credentials']
}
cy.wrap(createClient(c))
};
cy.wrap(createClient(c));

cy.visit(
`${Cypress.env('client_url')}/oauth2/code?client_id=${
c.client_id
}&client_secret=${c.client_secret}&scope=`,
}&client_secret=${c.client_secret}&scope=`,
{ failOnStatusCode: false }
)
);

cy.location().should(({ search, port }) => {
const query = qs.parse(search.substr(1))
expect(query.error).to.equal('invalid_request')
expect(query.error_hint).to.contain('redirect_uri')
const query = qs.parse(search.substr(1));
expect(query.error).to.equal('invalid_request');
expect(query.error_hint).to.contain('redirect_uri');

// Should show ORY Hydra's Error URL because a redirect URL could not be determined
expect(port).to.equal(Cypress.env('public_port'))
})
})
})
expect(port).to.equal(Cypress.env('public_port'));
});
});
});
54 changes: 32 additions & 22 deletions cypress/integration/oauth2/consent.js
@@ -1,4 +1,4 @@
import { prng } from '../../helpers'
import { prng } from '../../helpers';

describe('OAuth 2.0 End-User Authorization', () => {
const nc = () => ({
Expand All @@ -7,42 +7,52 @@ describe('OAuth 2.0 End-User Authorization', () => {
scope: 'offline_access',
redirect_uris: [`${Cypress.env('client_url')}/oauth2/callback`],
grant_types: ['authorization_code', 'refresh_token']
})
});

const hasConsent = (client, body) => {
let found = false
let found = false;
body.forEach(({ consent_request: { client: { client_id } } }) => {
if (client_id === client.client_id) {
found = true
found = true;
}
})
return found
}
});
return found;
};

it('should check if end user authorization exists', () => {
const client = nc()
const client = nc();
cy.authCodeFlow(client, {
consent: {
scope: ['offline_access'],
remember: true
}
})
});

cy.request(Cypress.env('admin_url') + '/oauth2/auth/sessions/consent?subject=foo@bar.com')
cy.request(
Cypress.env('admin_url') +
'/oauth2/auth/sessions/consent?subject=foo@bar.com'
)
.its('body')
.then((body) => {
expect(body.length).to.be.greaterThan(0)
expect(hasConsent(client, body)).to.be.true
})
.then(body => {
expect(body.length).to.be.greaterThan(0);
expect(hasConsent(client, body)).to.be.true;
});

cy.request('DELETE', Cypress.env('admin_url') + '/oauth2/auth/sessions/consent?subject=foo@bar.com')
cy.request(
'DELETE',
Cypress.env('admin_url') +
'/oauth2/auth/sessions/consent?subject=foo@bar.com'
);

cy.request(Cypress.env('admin_url') + '/oauth2/auth/sessions/consent?subject=foo@bar.com')
cy.request(
Cypress.env('admin_url') +
'/oauth2/auth/sessions/consent?subject=foo@bar.com'
)
.its('body')
.then((body) => {
expect(body.length).to.eq(0)
expect(hasConsent(client, body)).to.be.false
})
.then(body => {
expect(body.length).to.eq(0);
expect(hasConsent(client, body)).to.be.false;
});

cy.request(`${Cypress.env('client_url')}/oauth2/introspect/at`)
.its('body')
Expand All @@ -57,5 +67,5 @@ describe('OAuth 2.0 End-User Authorization', () => {
expect(body.result).to.equal('success');
expect(body.body.active).to.be.false;
});
})
})
});
});

0 comments on commit 9e829a9

Please sign in to comment.