|
| 1 | +import { HttpClient, HTTP_INTERCEPTORS } from '@angular/common/http' |
| 2 | +import { |
| 3 | + HttpClientTestingModule, |
| 4 | + HttpTestingController, |
| 5 | +} from '@angular/common/http/testing' |
| 6 | +import { TestBed } from '@angular/core/testing' |
| 7 | +import { CookieService } from 'ngx-cookie-service' |
| 8 | +import { XsrfFallbackInterceptor } from './xsrf-fallback.interceptor' |
| 9 | + |
| 10 | +describe('XsrfFallbackInterceptor', () => { |
| 11 | + let http: HttpClient |
| 12 | + let httpMock: HttpTestingController |
| 13 | + let cookieGetSpy: jasmine.Spy |
| 14 | + |
| 15 | + const apiBase = 'http://api.example/' |
| 16 | + const baseUrl = 'http://orcid.example/' |
| 17 | + const authBase = 'http://auth.example/' |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + ;(window as any).runtimeEnvironment = { |
| 21 | + production: false, |
| 22 | + API_WEB: apiBase, |
| 23 | + BASE_URL: baseUrl, |
| 24 | + AUTH_SERVER: authBase, |
| 25 | + } |
| 26 | + cookieGetSpy = jasmine.createSpy('get').and.returnValue('') |
| 27 | + |
| 28 | + TestBed.configureTestingModule({ |
| 29 | + imports: [HttpClientTestingModule], |
| 30 | + providers: [ |
| 31 | + { |
| 32 | + provide: HTTP_INTERCEPTORS, |
| 33 | + useClass: XsrfFallbackInterceptor, |
| 34 | + multi: true, |
| 35 | + }, |
| 36 | + { provide: CookieService, useValue: { get: cookieGetSpy } }, |
| 37 | + ], |
| 38 | + }) |
| 39 | + |
| 40 | + http = TestBed.inject(HttpClient) |
| 41 | + httpMock = TestBed.inject(HttpTestingController) |
| 42 | + }) |
| 43 | + |
| 44 | + afterEach(() => { |
| 45 | + httpMock.verify() |
| 46 | + }) |
| 47 | + |
| 48 | + it('passes through without adding header when production is true', () => { |
| 49 | + const env = (window as any).runtimeEnvironment |
| 50 | + env.production = true |
| 51 | + cookieGetSpy.and.returnValue('xsrf-token-123') |
| 52 | + |
| 53 | + http.post(apiBase + 'works/work.json', {}).subscribe() |
| 54 | + |
| 55 | + const req = httpMock.expectOne(apiBase + 'works/work.json') |
| 56 | + expect(req.request.headers.has('x-xsrf-token')).toBe(false) |
| 57 | + req.flush({}) |
| 58 | + env.production = false |
| 59 | + }) |
| 60 | + |
| 61 | + it('passes through GET requests without adding header', () => { |
| 62 | + http.get(apiBase + 'works/works.json').subscribe() |
| 63 | + |
| 64 | + const req = httpMock.expectOne(apiBase + 'works/works.json') |
| 65 | + expect(req.request.headers.has('x-xsrf-token')).toBe(false) |
| 66 | + req.flush({}) |
| 67 | + }) |
| 68 | + |
| 69 | + it('passes through when x-xsrf-token header is already present', () => { |
| 70 | + cookieGetSpy.and.returnValue('cookie-token') |
| 71 | + http |
| 72 | + .post(apiBase + 'works/work.json', {}, { |
| 73 | + headers: { 'x-xsrf-token': 'existing-token' }, |
| 74 | + }) |
| 75 | + .subscribe() |
| 76 | + |
| 77 | + const req = httpMock.expectOne(apiBase + 'works/work.json') |
| 78 | + expect(req.request.headers.get('x-xsrf-token')).toBe('existing-token') |
| 79 | + req.flush({}) |
| 80 | + }) |
| 81 | + |
| 82 | + it('passes through when request URL is not to backend host', () => { |
| 83 | + cookieGetSpy.and.returnValue('token') |
| 84 | + http.post('https://other-origin.com/api', {}).subscribe() |
| 85 | + |
| 86 | + const req = httpMock.expectOne('https://other-origin.com/api') |
| 87 | + expect(req.request.headers.has('x-xsrf-token')).toBe(false) |
| 88 | + req.flush({}) |
| 89 | + }) |
| 90 | + |
| 91 | + it('passes through when cookie is missing', () => { |
| 92 | + cookieGetSpy.and.returnValue('') |
| 93 | + |
| 94 | + http.post(apiBase + 'works/work.json', {}).subscribe() |
| 95 | + |
| 96 | + const req = httpMock.expectOne(apiBase + 'works/work.json') |
| 97 | + expect(req.request.headers.has('x-xsrf-token')).toBe(false) |
| 98 | + req.flush({}) |
| 99 | + }) |
| 100 | + |
| 101 | + it('adds XSRF-TOKEN for POST to API_WEB when cookie is present', () => { |
| 102 | + cookieGetSpy.and.callFake((name: string) => |
| 103 | + name === 'XSRF-TOKEN' ? 'api-xsrf-token' : '' |
| 104 | + ) |
| 105 | + |
| 106 | + http.post(apiBase + 'works/work.json', {}).subscribe() |
| 107 | + |
| 108 | + const req = httpMock.expectOne(apiBase + 'works/work.json') |
| 109 | + expect(req.request.headers.get('x-xsrf-token')).toBe('api-xsrf-token') |
| 110 | + expect(req.request.withCredentials).toBe(true) |
| 111 | + req.flush({}) |
| 112 | + }) |
| 113 | + |
| 114 | + it('adds AUTH-XSRF-TOKEN for POST to AUTH_SERVER when cookie is present', () => { |
| 115 | + cookieGetSpy.and.callFake((name: string) => |
| 116 | + name === 'AUTH-XSRF-TOKEN' ? 'auth-xsrf-token' : '' |
| 117 | + ) |
| 118 | + |
| 119 | + http.post(authBase + 'signin/auth.json', {}).subscribe() |
| 120 | + |
| 121 | + const req = httpMock.expectOne(authBase + 'signin/auth.json') |
| 122 | + expect(req.request.headers.get('x-xsrf-token')).toBe('auth-xsrf-token') |
| 123 | + req.flush({}) |
| 124 | + }) |
| 125 | + |
| 126 | + it('adds XSRF-TOKEN for relative URL when cookie is present', () => { |
| 127 | + cookieGetSpy.and.returnValue('rel-xsrf-token') |
| 128 | + |
| 129 | + http.post('/signin/auth.json', {}).subscribe() |
| 130 | + |
| 131 | + const req = httpMock.expectOne('/signin/auth.json') |
| 132 | + expect(req.request.headers.get('x-xsrf-token')).toBe('rel-xsrf-token') |
| 133 | + req.flush({}) |
| 134 | + }) |
| 135 | + |
| 136 | + it('passes through PUT and PATCH and DELETE when not production', () => { |
| 137 | + cookieGetSpy.and.returnValue('token') |
| 138 | + |
| 139 | + http.put(apiBase + 'works/1.json', {}).subscribe() |
| 140 | + let req = httpMock.expectOne(apiBase + 'works/1.json') |
| 141 | + expect(req.request.headers.get('x-xsrf-token')).toBe('token') |
| 142 | + req.flush({}) |
| 143 | + |
| 144 | + http.patch(apiBase + 'works/1.json', {}).subscribe() |
| 145 | + req = httpMock.expectOne(apiBase + 'works/1.json') |
| 146 | + expect(req.request.headers.get('x-xsrf-token')).toBe('token') |
| 147 | + req.flush({}) |
| 148 | + |
| 149 | + http.delete(apiBase + 'works/1.json').subscribe() |
| 150 | + req = httpMock.expectOne(apiBase + 'works/1.json') |
| 151 | + expect(req.request.headers.get('x-xsrf-token')).toBe('token') |
| 152 | + req.flush({}) |
| 153 | + }) |
| 154 | +}) |
0 commit comments