managed-group-subscriptions.test.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. import { expect } from 'chai'
  2. import { screen } from '@testing-library/react'
  3. import ManagedGroupSubscriptions from '../../../../../../frontend/js/features/subscription/components/dashboard/managed-group-subscriptions'
  4. import { ManagedGroupSubscription } from '../../../../../../types/subscription/dashboard/subscription'
  5. import {
  6. cleanUpContext,
  7. renderWithSubscriptionDashContext,
  8. } from '../../helpers/render-with-subscription-dash-context'
  9. function getManagedGroupSubscriptions(
  10. groupSSO: boolean | null,
  11. managedUsers: boolean | null
  12. ): ManagedGroupSubscription[] {
  13. const subscriptionOne = {
  14. _id: 'bcd567',
  15. userIsGroupMember: true,
  16. planLevelName: 'Professional',
  17. admin_id: {
  18. email: 'you@example.com',
  19. },
  20. features: {
  21. groupSSO,
  22. managedUsers,
  23. },
  24. teamName: 'GAS',
  25. }
  26. const subscriptionTwo = {
  27. _id: 'def456',
  28. userIsGroupMember: false,
  29. planLevelName: 'Collaborator',
  30. admin_id: {
  31. email: 'someone@example.com',
  32. },
  33. features: {
  34. groupSSO,
  35. managedUsers,
  36. },
  37. teamName: 'GASWPLC',
  38. }
  39. const subscriptionMemberAndAdmin = {
  40. _id: 'group2abc',
  41. userIsGroupMember: true,
  42. planLevelName: 'Collaborator',
  43. admin_id: {
  44. email: 'admin@example.com',
  45. },
  46. features: {
  47. groupSSO,
  48. managedUsers,
  49. },
  50. teamName: 'Testing',
  51. }
  52. const subscriptionAdmin = {
  53. _id: 'group123abc',
  54. userIsGroupMember: false,
  55. planLevelName: 'Collaborator',
  56. admin_id: {
  57. email: 'admin@example.com',
  58. },
  59. features: {
  60. groupSSO,
  61. managedUsers,
  62. },
  63. teamName: 'Testing Another',
  64. }
  65. return [
  66. subscriptionOne,
  67. subscriptionTwo,
  68. subscriptionMemberAndAdmin,
  69. subscriptionAdmin,
  70. ]
  71. }
  72. const managedGroupSubscriptions: ManagedGroupSubscription[] =
  73. getManagedGroupSubscriptions(false, false)
  74. const managedGroupSubscriptions2: ManagedGroupSubscription[] =
  75. getManagedGroupSubscriptions(true, true)
  76. const managedGroupSubscriptions3: ManagedGroupSubscription[] =
  77. getManagedGroupSubscriptions(true, false)
  78. const managedGroupSubscriptions4: ManagedGroupSubscription[] =
  79. getManagedGroupSubscriptions(false, true)
  80. const managedGroupSubscriptions5: ManagedGroupSubscription[] =
  81. getManagedGroupSubscriptions(null, true)
  82. describe('<ManagedGroupSubscriptions />', function () {
  83. afterEach(function () {
  84. cleanUpContext()
  85. })
  86. it('renders all managed group subscriptions', async function () {
  87. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  88. metaTags: [
  89. {
  90. name: 'ol-managedGroupSubscriptions',
  91. value: managedGroupSubscriptions,
  92. },
  93. { name: 'ol-usersEmail', value: 'admin@example.com' },
  94. ],
  95. })
  96. const elements = screen.getAllByText('You are a', {
  97. exact: false,
  98. })
  99. expect(elements.length).to.equal(4)
  100. expect(elements[0].textContent).to.equal(
  101. 'You are a manager and member of the Professional group subscription GAS administered by you@example.com.'
  102. )
  103. expect(elements[1].textContent).to.equal(
  104. 'You are a manager of the Collaborator group subscription GASWPLC administered by someone@example.com.'
  105. )
  106. expect(elements[2].textContent).to.equal(
  107. 'You are a manager and member of the Collaborator group subscription Testing administered by you (admin@example.com).'
  108. )
  109. expect(elements[3].textContent).to.equal(
  110. 'You are a manager of the Collaborator group subscription Testing Another administered by you (admin@example.com).'
  111. )
  112. const links = screen.getAllByRole('link')
  113. expect(links[1].getAttribute('href')).to.equal(
  114. '/manage/groups/bcd567/members'
  115. )
  116. expect(links[2].getAttribute('href')).to.equal(
  117. '/manage/groups/bcd567/managers'
  118. )
  119. expect(links[3].getAttribute('href')).to.equal('/metrics/groups/bcd567')
  120. expect(links[5].getAttribute('href')).to.equal(
  121. '/manage/groups/def456/members'
  122. )
  123. expect(links[6].getAttribute('href')).to.equal(
  124. '/manage/groups/def456/managers'
  125. )
  126. expect(links[7].getAttribute('href')).to.equal('/metrics/groups/def456')
  127. expect(links[9].getAttribute('href')).to.equal(
  128. '/manage/groups/group2abc/members'
  129. )
  130. expect(links[10].getAttribute('href')).to.equal(
  131. '/manage/groups/group2abc/managers'
  132. )
  133. expect(links[11].getAttribute('href')).to.equal('/metrics/groups/group2abc')
  134. expect(links[13].getAttribute('href')).to.equal(
  135. '/manage/groups/group123abc/members'
  136. )
  137. expect(links[14].getAttribute('href')).to.equal(
  138. '/manage/groups/group123abc/managers'
  139. )
  140. expect(links[15].getAttribute('href')).to.equal(
  141. '/metrics/groups/group123abc'
  142. )
  143. })
  144. it('renders nothing when there are no group memberships', function () {
  145. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />)
  146. const elements = screen.queryAllByText('You are a', {
  147. exact: false,
  148. })
  149. expect(elements.length).to.equal(0)
  150. })
  151. it('does not render the Manage group settings row when the user is not the group admin', function () {
  152. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  153. metaTags: [
  154. {
  155. name: 'ol-managedGroupSubscriptions',
  156. value: managedGroupSubscriptions2,
  157. },
  158. {
  159. name: 'ol-groupSettingsEnabledFor',
  160. value: [],
  161. },
  162. ],
  163. })
  164. expect(screen.queryByText('Manage group settings')).to.be.null
  165. expect(screen.queryByText('Configure and manage SSO and Managed Users')).to
  166. .be.null
  167. })
  168. describe('with group SSO off by default', function () {
  169. beforeEach(function () {
  170. window.metaAttributesCache.set('ol-ExposedSettings', {
  171. groupSSOEnabled: false,
  172. })
  173. })
  174. it('renders the Manage group settings row when feature is turned on', async function () {
  175. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  176. metaTags: [
  177. {
  178. name: 'ol-managedGroupSubscriptions',
  179. value: managedGroupSubscriptions2,
  180. },
  181. {
  182. name: 'ol-groupSettingsEnabledFor',
  183. value: [managedGroupSubscriptions2[0]._id],
  184. },
  185. ],
  186. })
  187. await screen.findAllByText('Manage group settings')
  188. await screen.findAllByText('Configure and manage SSO and Managed Users')
  189. })
  190. describe('renders the the correct subText for Manage Group settings row', async function () {
  191. it('with managedUsers.enabled=true and groupSSO.enabled=true', async function () {
  192. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  193. metaTags: [
  194. {
  195. name: 'ol-managedGroupSubscriptions',
  196. value: managedGroupSubscriptions2,
  197. },
  198. {
  199. name: 'ol-groupSettingsEnabledFor',
  200. value: [managedGroupSubscriptions2[0]._id],
  201. },
  202. ],
  203. })
  204. await screen.findAllByText('Configure and manage SSO and Managed Users')
  205. })
  206. it('with managedUsers.enabled=false and groupSSO.enabled=true', async function () {
  207. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  208. metaTags: [
  209. {
  210. name: 'ol-managedGroupSubscriptions',
  211. value: managedGroupSubscriptions3,
  212. },
  213. {
  214. name: 'ol-groupSettingsEnabledFor',
  215. value: [managedGroupSubscriptions3[0]._id],
  216. },
  217. ],
  218. })
  219. await screen.findAllByText('Configure and manage SSO')
  220. })
  221. it('with managedUsers.enabled=true and groupSSO.enabled=false', async function () {
  222. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  223. metaTags: [
  224. {
  225. name: 'ol-managedGroupSubscriptions',
  226. value: managedGroupSubscriptions4,
  227. },
  228. {
  229. name: 'ol-groupSettingsEnabledFor',
  230. value: [managedGroupSubscriptions4[0]._id],
  231. },
  232. ],
  233. })
  234. await screen.findAllByText('Turn on Managed Users')
  235. })
  236. it('with managedUsers.enabled=true and groupSSO.enabled=null', async function () {
  237. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  238. metaTags: [
  239. {
  240. name: 'ol-managedGroupSubscriptions',
  241. value: managedGroupSubscriptions5,
  242. },
  243. {
  244. name: 'ol-groupSettingsEnabledFor',
  245. value: [managedGroupSubscriptions5[0]._id],
  246. },
  247. ],
  248. })
  249. await screen.findAllByText('Turn on Managed Users')
  250. })
  251. })
  252. })
  253. describe('with group SSO on by default', function () {
  254. it('renders the Manage group settings row when features are turned on', async function () {
  255. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  256. metaTags: [
  257. {
  258. name: 'ol-managedGroupSubscriptions',
  259. value: managedGroupSubscriptions2,
  260. },
  261. {
  262. name: 'ol-groupSettingsEnabledFor',
  263. value: [managedGroupSubscriptions2[0]._id],
  264. },
  265. {
  266. name: 'ol-ExposedSettings',
  267. value: {
  268. groupSSOEnabled: true,
  269. },
  270. },
  271. ],
  272. })
  273. await screen.findAllByText('Manage group settings')
  274. await screen.findAllByText('Configure and manage SSO and Managed Users')
  275. })
  276. describe('renders the the correct subText for Manage Group settings row', async function () {
  277. it('with managedUsers.enabled=true and groupSSO.enabled=true', async function () {
  278. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  279. metaTags: [
  280. {
  281. name: 'ol-managedGroupSubscriptions',
  282. value: managedGroupSubscriptions2,
  283. },
  284. {
  285. name: 'ol-groupSettingsEnabledFor',
  286. value: [managedGroupSubscriptions2[0]._id],
  287. },
  288. {
  289. name: 'ol-ExposedSettings',
  290. value: {
  291. groupSSOEnabled: true,
  292. },
  293. },
  294. ],
  295. })
  296. await screen.findAllByText('Configure and manage SSO and Managed Users')
  297. })
  298. it('with managedUsers.enabled=false and groupSSO.enabled=true', async function () {
  299. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  300. metaTags: [
  301. {
  302. name: 'ol-managedGroupSubscriptions',
  303. value: managedGroupSubscriptions3,
  304. },
  305. {
  306. name: 'ol-groupSettingsEnabledFor',
  307. value: [managedGroupSubscriptions3[0]._id],
  308. },
  309. {
  310. name: 'ol-ExposedSettings',
  311. value: {
  312. groupSSOEnabled: true,
  313. },
  314. },
  315. ],
  316. })
  317. await screen.findAllByText('Configure and manage SSO')
  318. })
  319. it('with managedUsers.enabled=true and groupSSO.enabled=false', async function () {
  320. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  321. metaTags: [
  322. {
  323. name: 'ol-managedGroupSubscriptions',
  324. value: managedGroupSubscriptions4,
  325. },
  326. {
  327. name: 'ol-groupSettingsEnabledFor',
  328. value: [managedGroupSubscriptions4[0]._id],
  329. },
  330. {
  331. name: 'ol-ExposedSettings',
  332. value: {
  333. groupSSOEnabled: true,
  334. },
  335. },
  336. ],
  337. })
  338. await screen.findAllByText('Turn on Managed Users')
  339. })
  340. it('with managedUsers.enabled=true and groupSSO.enabled=null', async function () {
  341. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  342. metaTags: [
  343. {
  344. name: 'ol-managedGroupSubscriptions',
  345. value: managedGroupSubscriptions5,
  346. },
  347. {
  348. name: 'ol-groupSettingsEnabledFor',
  349. value: [managedGroupSubscriptions5[0]._id],
  350. },
  351. {
  352. name: 'ol-ExposedSettings',
  353. value: {
  354. groupSSOEnabled: true,
  355. },
  356. },
  357. ],
  358. })
  359. await screen.findAllByText('Configure and manage SSO and Managed Users')
  360. })
  361. })
  362. })
  363. })