33 * SPDX-License-Identifier: BUSL-1.1
44 */
55
6- import { click , settled , visit , fillIn , currentURL , waitFor } from '@ember/test-helpers' ;
6+ import { click , settled , visit , fillIn , currentURL , waitFor , findAll } from '@ember/test-helpers' ;
77import { module , test } from 'qunit' ;
88import { setupApplicationTest } from 'ember-qunit' ;
99import { runCmd , createNS } from 'vault/tests/helpers/commands' ;
@@ -12,13 +12,112 @@ import { AUTH_FORM } from 'vault/tests/helpers/auth/auth-form-selectors';
1212import { GENERAL } from '../helpers/general-selectors' ;
1313import { NAMESPACE_PICKER_SELECTORS } from '../helpers/namespace-picker' ;
1414
15+ import sinon from 'sinon' ;
16+
1517module ( 'Acceptance | Enterprise | namespaces' , function ( hooks ) {
1618 setupApplicationTest ( hooks ) ;
1719
18- hooks . beforeEach ( function ( ) {
20+ let fetchSpy ;
21+
22+ hooks . beforeEach ( ( ) => {
23+ fetchSpy = sinon . spy ( window , 'fetch' ) ;
1924 return login ( ) ;
2025 } ) ;
2126
27+ hooks . afterEach ( ( ) => {
28+ fetchSpy . restore ( ) ;
29+ } ) ;
30+
31+ test ( 'it filters namespaces based on search input' , async function ( assert ) {
32+ assert . expect ( 7 ) ;
33+
34+ await click ( NAMESPACE_PICKER_SELECTORS . toggle ) ;
35+
36+ // Verify all namespaces are displayed initially
37+ assert . dom ( NAMESPACE_PICKER_SELECTORS . link ( ) ) . exists ( 'Namespace link(s) exist' ) ;
38+ assert . strictEqual (
39+ findAll ( NAMESPACE_PICKER_SELECTORS . link ( ) ) . length ,
40+ 5 ,
41+ 'All namespaces are displayed initially'
42+ ) ;
43+
44+ // Verify the search input field exists
45+ assert . dom ( '[type="search"]' ) . exists ( 'The namespace search field exists' ) ;
46+
47+ // Verify 3 namespaces are displayed after searching for "beep"
48+ await fillIn ( '[type="search"]' , 'beep' ) ;
49+ assert . strictEqual (
50+ findAll ( NAMESPACE_PICKER_SELECTORS . link ( ) ) . length ,
51+ 3 ,
52+ 'Display 3 namespaces matching "beep" after searching'
53+ ) ;
54+
55+ // Verify 1 namespace is displayed after searching for "bop"
56+ await fillIn ( '[type="search"]' , 'bop' ) ;
57+ assert . strictEqual (
58+ findAll ( NAMESPACE_PICKER_SELECTORS . link ( ) ) . length ,
59+ 1 ,
60+ 'Display 1 namespace matching "bop" after searching'
61+ ) ;
62+
63+ // Verify no namespaces are displayed after searching for "other"
64+ await fillIn ( '[type="search"]' , 'other' ) ;
65+ assert . strictEqual (
66+ findAll ( NAMESPACE_PICKER_SELECTORS . link ( ) ) . length ,
67+ 0 ,
68+ 'No namespaces are displayed after searching for "other"'
69+ ) ;
70+
71+ // Clear the search input & verify all namespaces are displayed again
72+ await fillIn ( '[type="search"]' , '' ) ;
73+ assert . strictEqual (
74+ findAll ( NAMESPACE_PICKER_SELECTORS . link ( ) ) . length ,
75+ 5 ,
76+ 'All namespaces are displayed after clearing search input'
77+ ) ;
78+ } ) ;
79+
80+ test ( 'it updates the namespace list after clicking "Refresh list"' , async function ( assert ) {
81+ assert . expect ( 3 ) ;
82+
83+ await click ( NAMESPACE_PICKER_SELECTORS . toggle ) ;
84+
85+ // Verify that the namespace list was fetched on load
86+ let listNamespaceRequests = fetchSpy
87+ . getCalls ( )
88+ . filter ( ( call ) => call . args [ 0 ] . includes ( '/v1/sys/internal/ui/namespaces' ) ) ;
89+ assert . strictEqual (
90+ listNamespaceRequests . length ,
91+ 1 ,
92+ 'The network call to the specific endpoint was made twice (once on load, once on refresh)'
93+ ) ;
94+
95+ // Refresh the list of namespaces
96+ assert . dom ( NAMESPACE_PICKER_SELECTORS . refreshList ) . exists ( 'Refresh list button exists' ) ;
97+ await click ( NAMESPACE_PICKER_SELECTORS . refreshList ) ;
98+
99+ // Verify that the namespace list was fetched on refresh
100+ listNamespaceRequests = fetchSpy
101+ . getCalls ( )
102+ . filter ( ( call ) => call . args [ 0 ] . includes ( '/v1/sys/internal/ui/namespaces' ) ) ;
103+ assert . strictEqual (
104+ listNamespaceRequests . length ,
105+ 2 ,
106+ 'The network call to the specific endpoint was made twice (once on load, once on refresh)'
107+ ) ;
108+ } ) ;
109+
110+ test ( 'it displays the "Manage" button with the correct URL' , async function ( assert ) {
111+ assert . expect ( 1 ) ;
112+
113+ await click ( NAMESPACE_PICKER_SELECTORS . toggle ) ;
114+
115+ // Verify the "Manage" button is rendered and has the correct URL
116+ assert
117+ . dom ( '[href="/ui/vault/access/namespaces"]' )
118+ . exists ( 'The "Manage" button is displayed with the correct URL' ) ;
119+ } ) ;
120+
22121 test ( 'it clears namespaces when you log out' , async function ( assert ) {
23122 const ns = 'foo' ;
24123 await runCmd ( createNS ( ns ) , false ) ;
@@ -31,7 +130,7 @@ module('Acceptance | Enterprise | namespaces', function (hooks) {
31130 . exists ( 'The root namespace is selected' ) ;
32131 } ) ;
33132
34- // TODO: Is this test description still accurate?
133+ // TODO: revisit test name/ description, is this still relevant? A '/' prefix is stripped from namespace on login form
35134 test ( 'it shows nested namespaces if you log in with a namespace starting with a /' , async function ( assert ) {
36135 assert . expect ( 6 ) ;
37136
@@ -41,17 +140,24 @@ module('Acceptance | Enterprise | namespaces', function (hooks) {
41140 for ( const [ i , ns ] of nses . entries ( ) ) {
42141 await runCmd ( createNS ( ns ) , false ) ;
43142 await settled ( ) ;
143+
44144 // the namespace path will include all of the namespaces up to this point
45145 const targetNamespace = nses . slice ( 0 , i + 1 ) . join ( '/' ) ;
46146 const url = `/vault/secrets?namespace=${ targetNamespace } ` ;
147+
47148 // this is usually triggered when creating a ns in the form -- trigger a reload of the namespaces manually
48149 await click ( NAMESPACE_PICKER_SELECTORS . toggle ) ;
150+
151+ // refresh the list of namespaces
152+ await waitFor ( NAMESPACE_PICKER_SELECTORS . refreshList ) ;
49153 await click ( NAMESPACE_PICKER_SELECTORS . refreshList ) ;
50- await waitFor ( NAMESPACE_PICKER_SELECTORS . link ( targetNamespace ) ) ;
154+
51155 // check that the full namespace path, like "beep/boop", shows in the toggle display
156+ await waitFor ( NAMESPACE_PICKER_SELECTORS . link ( targetNamespace ) ) ;
52157 assert
53158 . dom ( NAMESPACE_PICKER_SELECTORS . link ( targetNamespace ) )
54159 . hasText ( targetNamespace , `shows the namespace ${ targetNamespace } in the toggle component` ) ;
160+
55161 // because quint does not like page reloads, visiting url directly instead of clicking on namespace in toggle
56162 await visit ( url ) ;
57163 }
0 commit comments