managed-group-subscriptions.test.tsx 12 KB

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