@@ -22,22 +22,41 @@ import { MatButtonHarness } from '@angular/material/button/testing';
2222import { MatInputHarness } from '@angular/material/input/testing' ;
2323
2424import { LogInComponent } from './log-in.component' ;
25- import { AppTestingModule , TESTING_BASE_URL } from '../../testing/app-testing.module' ;
25+ import { IdentityProvider } from '../../entities/configuration/identity-provider' ;
26+ import { AuthService } from '../../services/auth.service' ;
27+ import { ConfigService } from '../../services/config.service' ;
28+ import { IdentityProviderService } from '../../services/identity-provider.service' ;
29+ import { AppTestingModule , ConfigServiceStub , IdentityProviderServiceStub , TESTING_BASE_URL } from '../../testing/app-testing.module' ;
30+ import { DivHarness } from '../../testing/div.harness' ;
2631
2732describe ( 'LogInComponent' , ( ) => {
2833 let fixture : ComponentFixture < LogInComponent > ;
2934 let harnessLoader : HarnessLoader ;
3035 let httpTestingController : HttpTestingController ;
3136
37+ async function initComponent ( ) {
38+ fixture = TestBed . createComponent ( LogInComponent ) ;
39+ harnessLoader = TestbedHarnessEnvironment . loader ( fixture ) ;
40+ httpTestingController = TestBed . inject ( HttpTestingController ) ;
41+ fixture . detectChanges ( ) ;
42+ }
43+
44+ function enableLocalLogin ( enable : boolean ) {
45+ const configService = TestBed . inject ( ConfigService ) as unknown as ConfigServiceStub ;
46+ configService . configuration . authentication ! . localLogin ! . enabled = enable ;
47+ }
48+
49+ function initSsoProviders ( providers : IdentityProvider [ ] ) {
50+ const identityProviderService = TestBed . inject ( IdentityProviderService ) as unknown as IdentityProviderServiceStub ;
51+ identityProviderService . providers = providers ;
52+ }
53+
3254 beforeEach ( async ( ) => {
3355 await TestBed . configureTestingModule ( {
3456 imports : [ LogInComponent , AppTestingModule ] ,
3557 } ) . compileComponents ( ) ;
3658
37- fixture = TestBed . createComponent ( LogInComponent ) ;
38- harnessLoader = TestbedHarnessEnvironment . loader ( fixture ) ;
39- httpTestingController = TestBed . inject ( HttpTestingController ) ;
40- fixture . detectChanges ( ) ;
59+ await initComponent ( ) ;
4160 } ) ;
4261
4362 afterEach ( ( ) => {
@@ -66,6 +85,7 @@ describe('LogInComponent', () => {
6685
6786 expect ( await submitButton . isDisabled ( ) ) . toEqual ( true ) ;
6887 } ) ;
88+
6989 it ( 'should not validate form with missing password' , async ( ) => {
7090 const submitButton = await harnessLoader . getHarness ( MatButtonHarness . with ( { text : 'Log in' } ) ) ;
7191
@@ -88,4 +108,81 @@ describe('LogInComponent', () => {
88108 httpTestingController . expectOne ( `${ TESTING_BASE_URL } /user` ) . flush ( { } ) ;
89109 httpTestingController . expectOne ( `${ TESTING_BASE_URL } /portal-menu-links` ) . flush ( { } ) ;
90110 } ) ;
111+
112+ it ( 'should not display log-in form' , async ( ) => {
113+ enableLocalLogin ( false ) ;
114+ initSsoProviders ( [
115+ {
116+ id : 'github' ,
117+ name : 'GitHub' ,
118+ } ,
119+ ] ) ;
120+
121+ await initComponent ( ) ;
122+
123+ const login = await harnessLoader . getHarnessOrNull ( DivHarness . with ( { selector : '.log-in__form' } ) ) ;
124+ expect ( login ) . toBeNull ( ) ;
125+
126+ const orSeparator = await harnessLoader . getHarnessOrNull ( DivHarness . with ( { selector : '.log-in__sso__separator' } ) ) ;
127+ expect ( orSeparator ) . toBeNull ( ) ;
128+
129+ const ssoProvider = await harnessLoader . getHarnessOrNull ( MatButtonHarness . with ( { selector : '.log-in__sso__idp' } ) ) ;
130+ expect ( ssoProvider ) . not . toBeNull ( ) ;
131+
132+ const providerText = await ssoProvider ! . getText ( ) ;
133+ expect ( providerText ) . toEqual ( 'Continue with GitHub' ) ;
134+ } ) ;
135+
136+ it ( 'should not display SSO providers' , async ( ) => {
137+ enableLocalLogin ( true ) ;
138+ initSsoProviders ( [ ] ) ;
139+
140+ await initComponent ( ) ;
141+
142+ const login = await harnessLoader . getHarnessOrNull ( DivHarness . with ( { selector : '.log-in__form' } ) ) ;
143+ expect ( login ) . not . toBeNull ( ) ;
144+
145+ const orSeparator = await harnessLoader . getHarnessOrNull ( DivHarness . with ( { selector : '.log-in__sso__separator' } ) ) ;
146+ expect ( orSeparator ) . toBeNull ( ) ;
147+
148+ const ssoProvider = await harnessLoader . getHarnessOrNull ( MatButtonHarness . with ( { selector : '.log-in__sso__idp' } ) ) ;
149+ expect ( ssoProvider ) . toBeNull ( ) ;
150+ } ) ;
151+
152+ it ( 'should display "or" separator and identity providers' , async ( ) => {
153+ const identityProviders = [
154+ { id : 'github' , name : 'GitHub' } ,
155+ { id : 'google' , name : 'Google' } ,
156+ { id : 'graviteeio_am' , name : 'Gravitee AM' } ,
157+ ] ;
158+ enableLocalLogin ( true ) ;
159+ initSsoProviders ( identityProviders ) ;
160+
161+ await initComponent ( ) ;
162+
163+ const orSeparator = await harnessLoader . getHarnessOrNull ( DivHarness . with ( { selector : '.log-in__sso__separator' } ) ) ;
164+ expect ( orSeparator ) . not . toBeNull ( ) ;
165+
166+ const ssoProviders = await harnessLoader . getAllHarnesses ( MatButtonHarness . with ( { selector : '.log-in__sso__idp' } ) ) ;
167+ const providerTexts = await Promise . all ( ssoProviders . map ( harness => harness . getText ( ) ) ) ;
168+ console . log ( 'providerTexts' , providerTexts ) ;
169+
170+ for ( const provider of identityProviders ) {
171+ const found = providerTexts . find ( text => text === `Continue with ${ provider . name } ` ) ;
172+ expect ( found ) . toBeDefined ( ) ;
173+ }
174+ } ) ;
175+
176+ it ( 'should redirect when clicked on SSO provider' , async ( ) => {
177+ enableLocalLogin ( true ) ;
178+ initSsoProviders ( [ { id : 'google' , name : 'Google' } ] ) ;
179+
180+ const authenticateSSO = jest . spyOn ( TestBed . inject ( AuthService ) , 'authenticateSSO' ) . mockReturnValue ( ) ;
181+
182+ await initComponent ( ) ;
183+
184+ const ssoProvider = await harnessLoader . getHarness ( MatButtonHarness . with ( { selector : '.log-in__sso__idp' } ) ) ;
185+ await ssoProvider . click ( ) ;
186+ expect ( authenticateSSO ) . toHaveBeenCalledWith ( { id : 'google' , name : 'Google' } , '' ) ;
187+ } ) ;
91188} ) ;
0 commit comments