router_test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. require('router');
  20. describe('App.Router', function () {
  21. var router = App.Router.create();
  22. describe('#loginSuccessCallback()', function() {
  23. beforeEach(function () {
  24. sinon.stub(App.usersMapper, 'map');
  25. sinon.stub(router, 'setUserLoggedIn');
  26. sinon.stub(App.ajax, 'send');
  27. });
  28. afterEach(function() {
  29. App.usersMapper.map.restore();
  30. router.setUserLoggedIn.restore();
  31. App.ajax.send.restore();
  32. });
  33. it('should log in user and load views', function () {
  34. var userName = 'test';
  35. router.loginSuccessCallback({},{},{loginName: userName});
  36. expect(router.setUserLoggedIn.calledOnce).to.be.true;
  37. expect(router.setUserLoggedIn.calledWith(userName)).to.be.true;
  38. })
  39. });
  40. describe('#initAdmin()', function () {
  41. var cases = [
  42. {
  43. user: {
  44. admin: true
  45. },
  46. isAdmin: true,
  47. isOperator: false,
  48. isPermissionDataLoaded: true,
  49. title: 'admin'
  50. },
  51. {
  52. user: {
  53. operator: true
  54. },
  55. isAdmin: false,
  56. isOperator: true,
  57. isPermissionDataLoaded: true,
  58. title: 'operator'
  59. },
  60. {
  61. user: {},
  62. isAdmin: false,
  63. isOperator: false,
  64. isPermissionDataLoaded: true,
  65. title: 'read only access'
  66. },
  67. {
  68. user: null,
  69. isAdmin: false,
  70. isOperator: false,
  71. isPermissionDataLoaded: false,
  72. title: 'no user'
  73. }
  74. ];
  75. beforeEach(function () {
  76. App.setProperties({
  77. isAdmin: false,
  78. isOperator: false,
  79. isPermissionDataLoaded: false
  80. });
  81. });
  82. afterEach(function () {
  83. App.db.getUser.restore();
  84. });
  85. cases.forEach(function (item) {
  86. it(item.title, function () {
  87. sinon.stub(App.db, 'getUser').returns(item.user);
  88. router.initAdmin();
  89. expect(App.get('isAdmin')).to.equal(item.isAdmin);
  90. expect(App.get('isOperator')).to.equal(item.isOperator);
  91. expect(App.get('isPermissionDataLoaded')).to.equal(item.isPermissionDataLoaded);
  92. });
  93. });
  94. });
  95. describe('#adminViewInfoSuccessCallback', function() {
  96. beforeEach(function() {
  97. sinon.stub(window.location, 'replace', Em.K);
  98. });
  99. afterEach(function() {
  100. window.location.replace.restore();
  101. });
  102. it('should redirect to the latest version of admin view', function() {
  103. var tests = [{
  104. mockData: {
  105. components: [{
  106. 'RootServiceComponents': {
  107. 'component_version': '1.9.0'
  108. }
  109. }, {
  110. 'RootServiceComponents': {
  111. 'component_version': '2.0.0'
  112. }
  113. }]
  114. },
  115. expected: '/views/ADMIN_VIEW/2.0.0/INSTANCE/#/'
  116. }, {
  117. mockData: {
  118. components: [{
  119. 'RootServiceComponents': {
  120. 'component_version': '1.9.0'
  121. }
  122. }, {
  123. 'RootServiceComponents': {
  124. 'component_version': '2.1.0'
  125. }
  126. }, {
  127. 'RootServiceComponents': {
  128. 'component_version': '2.0.0'
  129. }
  130. }]
  131. },
  132. expected: '/views/ADMIN_VIEW/2.1.0/INSTANCE/#/'
  133. }, {
  134. mockData: {
  135. versions: [{
  136. 'RootServiceComponents': {
  137. version: '2.1.0'
  138. }
  139. }]
  140. },
  141. expected: '/views/ADMIN_VIEW/2.1.0/INSTANCE/#/'
  142. }];
  143. tests.forEach(function(data) {
  144. router.adminViewInfoSuccessCallback(data.mockData);
  145. expect(window.location.replace.calledWith(data.expected)).to.be.true;
  146. });
  147. });
  148. });
  149. });