From cd02c1bad4b61225159c2023a1a7fff309c5cc7e Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Wed, 17 Aug 2016 22:35:25 +0200 Subject: [PATCH] improve tests --- public/app/app.js | 3 +- public/app/index.js | 5 ++- public/app/main.jade | 22 ++++++---- public/app/main.js | 4 +- public/app/settings.jade | 12 ++--- public/app/settings.js | 19 +++++--- public/components/alert.js | 13 ++++++ public/components/config.js | 2 +- public/components/session.js | 1 - public/index.html | 3 +- tests/index.coffee | 85 +++++++++++++++++++++++++----------- 11 files changed, 118 insertions(+), 51 deletions(-) create mode 100644 public/components/alert.js diff --git a/public/app/app.js b/public/app/app.js index 7f3c7d3..f855c98 100644 --- a/public/app/app.js +++ b/public/app/app.js @@ -4,7 +4,8 @@ angular.module('warehost', [ 'ui.router', 'ui.bootstrap', 'config', - 'session' + 'session', + 'alert' ]) .config(['$urlRouterProvider','$httpProvider',function($urlRouterProvider,$httpProvider){ $urlRouterProvider.otherwise('/'); diff --git a/public/app/index.js b/public/app/index.js index 37a49e6..81d884e 100644 --- a/public/app/index.js +++ b/public/app/index.js @@ -8,7 +8,10 @@ angular.module('warehost') }) .state('app.index', { url:'/', - templateUrl: 'app/home.html' + templateUrl: 'app/home.html', + controller:function(alert){ + alert.set({}); + } }) .state('app.L', { templateUrl: 'app/loggedin.html' diff --git a/public/app/main.jade b/public/app/main.jade index 0bbc02f..7a4df80 100644 --- a/public/app/main.jade +++ b/public/app/main.jade @@ -31,11 +31,17 @@ span.glyphicon.glyphicon-user .navbar-text span.glyphicon.glyphicon-log-out(ng-click="logout()") - form.navbar-form.navbar-right(ng-if="!session.login.active",ng-class="{'has-warning':globals.currentUser.authdata}",ng-submit="login()") - input.form-control(ng-model="obj.username",placeholder="Username") - .input-group - input.form-control(type="password",ng-model="obj.password",placeholder="Password") - .input-group-btn - button.btn.btn-default(type="submit") - span.glyphicon.glyphicon-log-in -div(ui-view="",style="margin-top:53px;") + form.navbar-form.navbar-right(ng-if="!session.login.active",ng-submit="login()") + .form-group(ng-class="{'has-error': (error.fields.indexOf('username') >= 0)}") + input.form-control(ng-model="obj.username",placeholder="Username",pattern=".{3,}") + .form-group(ng-class="{'has-error': (error.fields.indexOf('password') >= 0)}") + .input-group + input.form-control(type="password",ng-model="obj.password",placeholder="Password") + .input-group-btn + button.btn.btn-default(type="submit") + span.glyphicon.glyphicon-log-in +div(style="margin-top:53px;") + .container(ng-if="error.msg") + .alert.alert-danger(style="margin-top:20px;") + {{error.msg}} + div(ui-view="") diff --git a/public/app/main.js b/public/app/main.js index a31b761..5755c87 100644 --- a/public/app/main.js +++ b/public/app/main.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('warehost') - .controller('MainCtrl',function($scope,$http,$state,config,session){ + .controller('MainCtrl',function($scope,alert,$http,$state,config,session){ $scope.isOpen = false; $scope.obj = {}; $scope.$state = $state; @@ -12,11 +12,13 @@ angular.module('warehost') $scope.logout = function(){ $http.get(config.api+'/logout').then(function(res){ session.set(res); + alert.set(res); }); }; $scope.login = function(){ $http.post(config.api+'/login',$scope.obj).then(function(res){ session.set(res); + alert.set(res); $scope.obj = {}; }); }; diff --git a/public/app/settings.jade b/public/app/settings.jade index 47fc63b..4470fd1 100644 --- a/public/app/settings.jade +++ b/public/app/settings.jade @@ -3,19 +3,19 @@ .row .col-md-6 h2 Global - form.well.well-sm(ng-submit="submit()") + form.well.well-sm(ng-submit="submit()",name="myform") .form-group label(for="globalUsername") Username input.form-control(id="globalUsername",ng-model="session.login.username",readonly) - .form-group + .form-group(ng-class="{'has-error': (error.fields.indexOf('currentpassword') >= 0)}") label(for="currentPassword") Current Password input.form-control(id="currentPassword",type="password",ng-model="obj.currentPassword") - .form-group + .form-group(ng-class="{'has-error': (error.fields.indexOf('newpassword') >= 0 || !myform.newPassword.$valid)}") label(for="newPassword") New Password - input.form-control(id="newPassword",type="password",ng-model="obj.newPassword") - .form-group + input.form-control(id="newPassword",type="password",name="newPassword",ng-model="obj.newPassword",ng-minlength="3") + .form-group(ng-class="{'has-error': (obj.newPassword != obj.repeatPassword && myform.repeatPassword.$dirty)}") label(for="repeatPassword") Again Password - input.form-control(id="repeatPassword",type="password",ng-model="obj.repeatPassword") + input.form-control(id="repeatPassword",type="password",name="repeatPassword",ng-model="obj.repeatPassword") .row .col-xs-6 button.btn.btn-primary(type="submit") diff --git a/public/app/settings.js b/public/app/settings.js index 9f9d041..b83c14c 100644 --- a/public/app/settings.js +++ b/public/app/settings.js @@ -1,19 +1,28 @@ 'use strict'; angular.module('warehost') - .controller('SettingsCtrl',function(session,config,$scope,$http){ + .controller('SettingsCtrl',function(session,config,alert,$scope,$http){ $scope.obj = {}; + alert.set({}); $scope.submit = function(){ - $http.post(config.api+'/password',$scope.obj).then(function(res){ - session.set(res); - $scope.obj = {}; - }); + if($scope.obj.newPassword === $scope.obj.repeatPassword){ + $http.post(config.api+'/password',$scope.obj).then(function(res){ + session.set(res); + alert.set(res); + $scope.obj = {}; + }); + }else{ + alert.set({msg:'Not equal Passwords'}); + } }; $scope.delete = function(){ if(window.confirm('realy delete your login and everything else on warehost?')){ $http.get(config.api+'/delete').then(function(res){ session.set(res); + alert.set(res); }); + }else{ + alert.set({msg:'Account not deleted'}); } }; }); diff --git a/public/components/alert.js b/public/components/alert.js new file mode 100644 index 0000000..59135db --- /dev/null +++ b/public/components/alert.js @@ -0,0 +1,13 @@ +'use strict'; +angular.module('alert', []) + .factory('alert', ['$rootScope',function($rootScope) { + return { + set: function (res){ + if(res.data){ + $rootScope.error = res.data.error; + }else{ + $rootScope.error = res; + } + } + }; + }]); diff --git a/public/components/config.js b/public/components/config.js index 6112470..30635e5 100644 --- a/public/components/config.js +++ b/public/components/config.js @@ -2,6 +2,6 @@ angular.module('config', []) .factory('config', function() { return { - api: 'http://[::1]:8080', + api: 'http://[::1]:8080' }; }); diff --git a/public/components/session.js b/public/components/session.js index 4c199be..7484f70 100644 --- a/public/components/session.js +++ b/public/components/session.js @@ -7,7 +7,6 @@ angular.module('session', []) }, set: function (res){ $rootScope.session = res.data.session; - console.log(res,$rootScope.session); } }; }]); diff --git a/public/index.html b/public/index.html index 0a2c646..da12357 100644 --- a/public/index.html +++ b/public/index.html @@ -17,7 +17,7 @@ - + @@ -48,6 +48,7 @@ + diff --git a/tests/index.coffee b/tests/index.coffee index 46d8549..9812758 100644 --- a/tests/index.coffee +++ b/tests/index.coffee @@ -10,8 +10,10 @@ describe('API',-> request({method: 'GET',uri:config.ADDRESS+"/status"},(err,res,body)-> expect(err).to.be.null expect(res.statusCode).to.be.equal(200) - x = JSON.parse(body) - expect(x.data).to.be.true + if(typeof body == "string") + body = JSON.parse(body) + expect(body.data).to.be.true + expect(body.error).to.be.null done() ) ) @@ -23,7 +25,11 @@ describe('API',-> }},(err,res,body)-> expect(err).to.be.null expect(res.statusCode).to.be.equal(200) + if(typeof body == "string") + body = JSON.parse(body) expect(body.data).to.be.false + expect(body.error.msg).to.not.be.null + expect(body.error.fields).to.include('username') done() ) ) @@ -34,7 +40,11 @@ describe('API',-> }},(err,res,body)-> expect(err).to.be.null expect(res.statusCode).to.be.equal(200) + if(typeof body == "string") + body = JSON.parse(body) expect(body.data).to.be.false + expect(body.error.msg).to.not.be.null + expect(body.error.fields).to.include('password') done() ) ) @@ -45,7 +55,10 @@ describe('API',-> }},(err,res,body)-> expect(err).to.be.null expect(res.statusCode).to.be.equal(200) + if(typeof body == "string") + body = JSON.parse(body) expect(body.data).to.be.true + expect(body.error).to.be.null expect(body.session.login.active).to.be.true done() ) @@ -56,9 +69,11 @@ describe('API',-> request({method: 'GET',uri:config.ADDRESS+"/logout"},(err,res,body)-> expect(err).to.be.null expect(res.statusCode).to.be.equal(200) - x = JSON.parse(body) - expect(x.data).to.be.true - expect(x.session.login).to.be.null + if(typeof body == "string") + body = JSON.parse(body) + expect(body.data).to.be.false + expect(body.error.msg).to.not.be.null + expect(body.error.fields).to.include('session') done() ) ) @@ -67,9 +82,11 @@ describe('API',-> request({method: 'GET',uri:config.ADDRESS+"/logout",jar:j},(err,res,body)-> expect(err).to.be.null expect(res.statusCode).to.be.equal(200) - x = JSON.parse(body) - expect(x.data).to.be.true - expect(x.session.login).to.be.null + if(typeof body == "string") + body = JSON.parse(body) + expect(body.data).to.be.true + expect(body.error).to.be.null + expect(body.session.login).to.be.null done() ) ) @@ -83,9 +100,12 @@ describe('API',-> }},(err,res,body)-> expect(err).to.be.null expect(res.statusCode).to.be.equal(200) - x = JSON.parse(body) - expect(x.data).to.be.true - expect(x.session.login).to.be.null + if(typeof body == "string") + body = JSON.parse(body) + expect(body.data).to.be.false + expect(body.error.msg).to.not.be.null + expect(body.error.fields).to.include('session') + expect(body.session.login).to.be.null done() ) ) @@ -97,9 +117,12 @@ describe('API',-> },jar:j},(err,res,body)-> expect(err).to.be.null expect(res.statusCode).to.be.equal(200) - x = JSON.parse(body) - expect(x.data).to.be.false - expect(x.session.login).to.be.null + if(typeof body == "string") + body = JSON.parse(body) + expect(body.data).to.be.false + expect(body.error.msg).to.not.be.null + expect(body.error.fields).to.include('currentpassword') + expect(body.session.login).to.not.be.null done() ) ) @@ -112,9 +135,12 @@ describe('API',-> },jar:j},(err,res,body)-> expect(err).to.be.null expect(res.statusCode).to.be.equal(200) - x = JSON.parse(body) - expect(x.data).to.be.false - expect(x.session.login).to.be.null + if(typeof body == "string") + body = JSON.parse(body) + expect(body.data).to.be.false + expect(body.error.msg).to.not.be.null + expect(body.error.fields).to.include('newpassword') + expect(body.session.login).to.not.be.null done() ) ) @@ -127,13 +153,15 @@ describe('API',-> },jar:j},(err,res,body)-> expect(err).to.be.null expect(res.statusCode).to.be.equal(200) - x = JSON.parse(body) - expect(x.data).to.be.true - expect(x.session.login).to.be.null + if(typeof body == "string") + body = JSON.parse(body) + expect(body.data).to.be.true + expect(body.error).to.be.null request({method: 'POST',uri: config.ADDRESS+"/password",json:{ currentpassword: config.testdata.password+"f", newpassword: config.testdata.password, },jar:j},(err,res,body)-> + expect(body.data).to.be.true done() ) ) @@ -145,9 +173,12 @@ describe('API',-> request({method: 'GET',uri:config.ADDRESS+"/delete"},(err,res,body)-> expect(err).to.be.null expect(res.statusCode).to.be.equal(200) - x = JSON.parse(body) - expect(x.data).to.be.true - expect(x.session.login).to.be.null + if(typeof body == "string") + body = JSON.parse(body) + expect(body.data).to.be.false + expect(body.error.msg).to.not.be.null + expect(body.error.fields).to.include('session') + expect(body.session.login).to.be.null done() ) ) @@ -156,9 +187,11 @@ describe('API',-> request({method: 'GET',uri:config.ADDRESS+"/delete",jar:j},(err,res,body)-> expect(err).to.be.null expect(res.statusCode).to.be.equal(200) - x = JSON.parse(body) - expect(x.data).to.be.true - expect(x.session.login).to.be.null + if(typeof body == "string") + body = JSON.parse(body) + expect(body.data).to.be.true + expect(body.error).to.be.null + expect(body.session.login).to.be.null done() ) )