Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.97 KB | None | 0 0
  1. (function(){
  2. 'use strict';
  3.  
  4. angular
  5. .module('portal')
  6. .factory('AWSService',AWSService);
  7.  
  8. AWSService.$inject = ['$rootScope'];
  9.  
  10. function AWSService($rootScope){
  11. var service = {};
  12.  
  13. service.Login = Login;
  14. service.SignOut = SignOut;
  15. service.IsSessionValid = IsSessionValid;
  16. service.Register = Register;
  17. service.ConfirmReg = ConfirmReg;
  18. service.GetUserDetails = GetUserDetails;
  19.  
  20. initService();
  21.  
  22. return service;
  23.  
  24.  
  25. function initService(){
  26.  
  27. // Cognito Identity Pool Id
  28. AWS.config.region = 'XXXXXXX'; // Region
  29. AWS.config.credentials = new AWS.CognitoIdentityCredentials({
  30. IdentityPoolId: 'XXXXXXXXX'
  31. });
  32.  
  33. // Cognito User Pool Id
  34. AWSCognito.config.region = 'XXXXXXXX';
  35. AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials({
  36. IdentityPoolId: 'XXXXXXXX'
  37. });
  38.  
  39. }
  40.  
  41. function getUserPool(){
  42. var poolData = { UserPoolId : 'XXXXXXX',
  43. ClientId : 'XXXXXXXXXXXX'
  44. };
  45.  
  46.  
  47. var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
  48. return userPool;
  49. }
  50.  
  51. function Login(authDetails, callback) {
  52. console.log('In AWS service login');
  53. if (authDetails == null || authDetails.UserName == null || authDetails.Password == null) {
  54. throw new Error('Username and password information are required.');
  55. }
  56.  
  57. try{
  58. var authenticationData = {
  59. Username : authDetails.UserName,
  60. Password : authDetails.Password,
  61. };
  62.  
  63. var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider
  64. .AuthenticationDetails(authenticationData);
  65.  
  66. var userData = {
  67. Username : authDetails.UserName,
  68. Pool : getUserPool()
  69. };
  70.  
  71. var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
  72. cognitoUser.authenticateUser(authenticationDetails, callback);
  73.  
  74. } catch(e){
  75. alert(e.name+" "+e.message)
  76. }
  77.  
  78.  
  79. }
  80.  
  81. function IsSessionValid(callback) {
  82.  
  83. var cognitoUser = getUserPool().getCurrentUser();
  84.  
  85. if (cognitoUser != null) {
  86. cognitoUser.getSession(function(err, session) {
  87. if (err) {
  88. alert(err);
  89. callback({ success: false, message: 'Session is invalid'});
  90. }
  91. console.log('session validity: ' + session.isValid());
  92. callback({ success: true, message: 'Session is valid'});
  93. });
  94. }
  95.  
  96. }
  97.  
  98. function SignOut(){
  99. var cognitoUser = getUserPool().getCurrentUser();
  100.  
  101. if (cognitoUser != null) {
  102. console.log('session is valid, signing out');
  103. cognitoUser.signOut();
  104. }else {
  105. console.log('session already expired');
  106. }
  107.  
  108. }
  109.  
  110. function Register(userInfo, callback){
  111. var attributeList = [];
  112.  
  113. var dataEmail = {
  114. Name : 'email',
  115. Value : userInfo.email
  116. };
  117. var dataFirstName = {
  118. Name : 'given_name',
  119. Value : userInfo.firstName
  120. };
  121. var dataLastName = {
  122. Name : 'family_name',
  123. Value : userInfo.lastName
  124. };
  125.  
  126. var attributeEmail = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail);
  127. var attributeFirstName = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataFirstName);
  128. var attributeLastName = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataLastName);
  129.  
  130. attributeList.push(attributeEmail);
  131. attributeList.push(attributeFirstName);
  132. attributeList.push(attributeLastName);
  133.  
  134.  
  135. getUserPool().signUp(userInfo.email, userInfo.password, attributeList, null, callback);
  136. }
  137.  
  138. function GetUserDetails(callback){
  139. IsSessionValid(function (response) {
  140. if (response.success) {
  141. var cognitoUser = getUserPool().getCurrentUser();
  142. cognitoUser.getUserAttributes(function(err, result) {
  143. if (err) {
  144. alert(err);
  145. return;
  146. }
  147. for (i = 0; i < result.length; i++) {
  148. console.log('attribute ' + result[i].getName() + ' has value ' + result[i].getValue());
  149. }
  150. })
  151.  
  152. }
  153. else{
  154. console.log('invalid session');
  155. }
  156. })
  157.  
  158.  
  159. }
  160.  
  161. function ConfirmReg(confirmCode, callback){
  162.  
  163. }
  164.  
  165.  
  166. }
  167.  
  168.  
  169. })();
  170.  
  171. 'use strict'
  172.  
  173. describe('AWSService', function(){
  174. var AWSService, myRootscope;
  175. console.log('In service describe');
  176. beforeEach(angular.mock.module("portal"))
  177.  
  178. it("Should have created AWS service", inject(function($rootScope, _AWSService_){
  179. console.log('Service injection start');
  180. AWSService = _AWSService_;
  181. myRootscope = $rootScope;
  182. console.log('Service injection done');
  183. }));
  184.  
  185. it('Test AWS service - login', function(){
  186. try{
  187.  
  188. console.log('Calling login function');
  189. AWSService.Login(null, null);
  190. }catch(e){
  191. console.log(e);
  192. expect(e.message).toEqual('Username and password information are required.');
  193. }
  194. });
  195. });
  196.  
  197. module.exports = function(config) {
  198. 'use strict';
  199.  
  200. config.set({
  201. // enable / disable watching file and executing tests whenever any file changes
  202. autoWatch: true,
  203.  
  204. // base path, that will be used to resolve files and exclude
  205. basePath: '../',
  206.  
  207. // testing framework to use (jasmine/mocha/qunit/...)
  208. // as well as any additional frameworks (requirejs/chai/sinon/...)
  209. frameworks: [
  210. 'jasmine',
  211. 'browserify'
  212. ],
  213.  
  214.  
  215. // list of files / patterns to load in the browser
  216. files: [
  217. // bower:js
  218. 'bower_components/jquery/dist/jquery.js',
  219. 'bower_components/angular/angular.js',
  220. 'bower_components/bootstrap/dist/js/bootstrap.js',
  221. 'bower_components/angular-animate/angular-animate.js',
  222. 'bower_components/angular-cookies/angular-cookies.js',
  223. 'bower_components/angular-resource/angular-resource.js',
  224. 'bower_components/angular-route/angular-route.js',
  225. 'bower_components/angular-sanitize/angular-sanitize.js',
  226. 'bower_components/angular-touch/angular-touch.js',
  227. 'bower_components/aws-sdk-js/dist/aws-sdk.js',
  228. 'bower_components/moment/moment.js',
  229. 'bower_components/sjcl/sjcl.js',
  230. 'bower_components/jsbn/jsbn.js',
  231. 'bower_components/angular-mocks/angular-mocks.js',
  232. // endbower
  233.  
  234. 'app/*.js',
  235. 'app/app-services/*.js',
  236. 'app/**/*.js',
  237.  
  238. 'test/mock/**/*.js',
  239. 'test/spec/**/*.js'
  240. ],
  241.  
  242. // Browserify config
  243. browserify: {
  244. watch: true,
  245. debug: true
  246. },
  247.  
  248. preprocessors: {
  249. 'app/*.js' : ['browserify'],
  250. 'test/spec/services/aws.services.test.js': [ 'browserify' ]
  251. },
  252. // list of files / patterns to exclude
  253. exclude: ['karma.conf.js', 'protractor-conf.js'],
  254.  
  255.  
  256. // web server port
  257. port: 8080,
  258.  
  259. // Start these browsers, currently available:
  260. // - Chrome
  261. // - ChromeCanary
  262. // - Firefox
  263. // - Opera
  264. // - Safari (only Mac)
  265. // - PhantomJS
  266. // - IE (only Windows)
  267. browsers: [
  268. 'PhantomJS'
  269. ],
  270.  
  271. // Which plugins to enable
  272. plugins: [
  273. 'karma-phantomjs-launcher',
  274. 'karma-jasmine',
  275. 'karma-chrome-launcher',
  276. 'karma-browserify',
  277. ],
  278.  
  279. // Continuous Integration mode
  280. // if true, it capture browsers, run tests and exit
  281. singleRun: false,
  282.  
  283. colors: true,
  284.  
  285. // level of logging
  286. // possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
  287. logLevel: config.LOG_DEBUG,
  288.  
  289. // Uncomment the following lines if you are using grunt's server to run the tests
  290. // proxies: {
  291. // '/': 'http://localhost:9000/'
  292. // },
  293. // URL root prevent conflicts with the site root
  294. // urlRoot: '_karma_'
  295. });
  296. };
  297.  
  298. karma start karma.conf.js --browsers=Chrome --single-run=false
  299.  
  300. Chrome 53.0.2785 (Windows 10 0.0.0) ERROR
  301. Uncaught TypeError: Cannot read property 'CognitoIdentityServiceProvider' of undefined
  302. at app/third-party/amazon-cognito-identity.min.js:19
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement