Created
August 1, 2017 20:22
-
-
Save zamirkhan/e5e6284ceb522edf073b0439b5350a30 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AWS from 'aws-sdk'; | |
import config from './config.js'; | |
import sigV4Client from './sigV4Client'; | |
export function getAwsCredentials(userToken) { | |
const authenticator = `cognito-idp.${config.cognito.REGION}.amazonaws.com/${config.cognito.USER_POOL_ID}`; | |
AWS.config.update({ region: config.cognito.REGION }); | |
AWS.config.credentials = new AWS.CognitoIdentityCredentials({ | |
IdentityPoolId: config.cognito.IDENTITY_POOL_ID, | |
Logins: { | |
[authenticator]: userToken | |
} | |
}); | |
return AWS.config.credentials.getPromise(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const chakram = require('chakram'), | |
expect = chakram.expect; | |
const util = require('util'); | |
const aws = require('aws-sdk'); | |
const amazonCognitoIdentity = require('amazon-cognito-identity-js'); | |
const config = require('../secrets.json'); | |
const myconfig = require('./config.js'); | |
const awsLib = require('./awsLib.js'); // provided in gist | |
import sigV4Client from './sigV4Client.js'; // identical to the sigV4Client from the tutorial | |
const cognitoUserPool = amazonCognitoIdentity.CognitoUserPool; | |
// Set the AWS region | |
aws.config.update({ region: config.AwsRegion }); | |
// The API portion being tested | |
const card_route = "/cards" // my modification - I'm creating "cards" instead of "notes" | |
const cisp = new aws.CognitoIdentityServiceProvider({ apiVersion: '2016-04-18' }); | |
const params = { | |
AuthFlow: 'ADMIN_NO_SRP_AUTH', | |
ClientId: config.AwsAppClientId, | |
UserPoolId: config.CognitoUserPoolId, | |
AuthParameters: { | |
USERNAME: config.CognitoTestUsername, | |
PASSWORD: config.CognitoTestPassword | |
} | |
}; | |
var adminInitiateAuthPromise = cisp.adminInitiateAuth(params).promise(); | |
const getSignedRequest = function (method, path, headers, queryParams, body) { | |
const signedRequest = sigV4Client | |
.newClient({ | |
accessKey: aws.config.credentials.accessKeyId, | |
secretKey: aws.config.credentials.secretAccessKey, | |
sessionToken: aws.config.credentials.sessionToken, | |
region: myconfig.default.apiGateway.REGION, | |
endpoint: myconfig.default.apiGateway.URL, | |
}) | |
.signRequest({ | |
method, | |
path, | |
headers, | |
queryParams, | |
body | |
}); | |
return signedRequest; | |
} | |
describe("cards API", function () { | |
var opts = {}; | |
before("test setup", function () { | |
return adminInitiateAuthPromise | |
.then(function (auth) { | |
const idToken = auth.AuthenticationResult.IdToken; | |
return awsLib.getAwsCredentials(idToken); | |
}); | |
}); | |
it("should support a put (create) and delete endpoints", function () { | |
const body = JSON.stringify({ | |
'url': 'my_new_url', | |
'title': 'My new title', | |
'description': 'My new description' | |
}); | |
const method = 'POST'; | |
const signedRequest = getSignedRequest(method, card_route, {}, {}, body); | |
const opts = { | |
'headers': signedRequest.headers, | |
'body': signedRequest.body | |
}; | |
console.log(signedRequest); | |
return chakram.request(signedRequest.method, signedRequest.url, opts).then(function (postResponse) { | |
// ... the test should continue here. but this request returns the 403 error I described. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment