role.spec.jsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import Cookies from 'js-cookie';
  2. import {mountWithTheme} from 'sentry-test/enzyme';
  3. import Role from 'app/components/acl/role';
  4. import ConfigStore from 'app/stores/configStore';
  5. describe('Role', function () {
  6. const organization = TestStubs.Organization({
  7. role: 'admin',
  8. availableRoles: [
  9. {
  10. id: 'member',
  11. name: 'Member',
  12. },
  13. {
  14. id: 'admin',
  15. name: 'Admin',
  16. },
  17. {
  18. id: 'manager',
  19. name: 'Manager',
  20. },
  21. {
  22. id: 'owner',
  23. name: 'Owner',
  24. },
  25. ],
  26. });
  27. const routerContext = TestStubs.routerContext([
  28. {
  29. organization,
  30. },
  31. ]);
  32. describe('as render prop', function () {
  33. const childrenMock = jest.fn().mockReturnValue(null);
  34. beforeEach(function () {
  35. childrenMock.mockClear();
  36. });
  37. it('has a sufficient role', function () {
  38. mountWithTheme(<Role role="admin">{childrenMock}</Role>, routerContext);
  39. expect(childrenMock).toHaveBeenCalledWith({
  40. hasRole: true,
  41. });
  42. });
  43. it('has an unsufficient role', function () {
  44. mountWithTheme(<Role role="manager">{childrenMock}</Role>, routerContext);
  45. expect(childrenMock).toHaveBeenCalledWith({
  46. hasRole: false,
  47. });
  48. });
  49. it('gives access to a superuser with unsufficient role', function () {
  50. ConfigStore.config.user = {isSuperuser: true};
  51. Cookies.set = jest.fn();
  52. mountWithTheme(<Role role="owner">{childrenMock}</Role>, routerContext);
  53. expect(childrenMock).toHaveBeenCalledWith({
  54. hasRole: true,
  55. });
  56. expect(Cookies.set).toHaveBeenCalledWith('su', 'test');
  57. ConfigStore.config.user = {isSuperuser: false};
  58. });
  59. it('does not give access to a made up role', function () {
  60. mountWithTheme(<Role role="abcdefg">{childrenMock}</Role>, routerContext);
  61. expect(childrenMock).toHaveBeenCalledWith({
  62. hasRole: false,
  63. });
  64. });
  65. it('handles no user', function () {
  66. const user = {...ConfigStore.config.user};
  67. ConfigStore.config.user = undefined;
  68. mountWithTheme(<Role role="member">{childrenMock}</Role>, routerContext);
  69. expect(childrenMock).toHaveBeenCalledWith({
  70. hasRole: false,
  71. });
  72. ConfigStore.config.user = user;
  73. });
  74. it('handles no availableRoles', function () {
  75. mountWithTheme(
  76. <Role role="member" organization={{...organization, availableRoles: undefined}}>
  77. {childrenMock}
  78. </Role>,
  79. routerContext
  80. );
  81. expect(childrenMock).toHaveBeenCalledWith({
  82. hasRole: false,
  83. });
  84. });
  85. });
  86. describe('as React node', function () {
  87. it('has a sufficient role', function () {
  88. const wrapper = mountWithTheme(
  89. <Role role="member">
  90. <div>The Child</div>
  91. </Role>,
  92. routerContext
  93. );
  94. expect(wrapper.find('Role div').exists()).toBeTruthy();
  95. });
  96. it('has an unsufficient role', function () {
  97. const wrapper = mountWithTheme(
  98. <Role role="owner">
  99. <div>The Child</div>
  100. </Role>,
  101. routerContext
  102. );
  103. expect(wrapper.find('Role div').exists()).toBeFalsy();
  104. });
  105. });
  106. });