juice-shop
113 строк · 4.2 Кб
1/*
2* Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3* SPDX-License-Identifier: MIT
4*/
5
6import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'
7import { fakeAsync, inject, TestBed, tick } from '@angular/core/testing'
8
9import { BasketService } from './basket.service'
10
11describe('BasketService', () => {
12beforeEach(() => {
13TestBed.configureTestingModule({
14imports: [HttpClientTestingModule],
15providers: [BasketService]
16})
17})
18
19it('should be created', inject([BasketService], (service: BasketService) => {
20expect(service).toBeTruthy()
21}))
22
23it('should get basket directly from the rest api', inject([BasketService, HttpTestingController],
24fakeAsync((service: BasketService, httpMock: HttpTestingController) => {
25let res: any
26service.find(1).subscribe((data) => (res = data))
27const req = httpMock.expectOne('http://localhost:3000/rest/basket/1')
28req.flush({ data: 'apiResponse' })
29tick()
30expect(req.request.method).toBe('GET')
31expect(res).toBe('apiResponse')
32httpMock.verify()
33})
34))
35
36it('should get single basket item directly from the rest api', inject([BasketService, HttpTestingController],
37fakeAsync((service: BasketService, httpMock: HttpTestingController) => {
38let res: any
39service.get(1).subscribe((data) => (res = data))
40const req = httpMock.expectOne('http://localhost:3000/api/BasketItems/1')
41req.flush({ data: 'apiResponse' })
42tick()
43expect(req.request.method).toBe('GET')
44expect(res).toBe('apiResponse')
45httpMock.verify()
46})
47))
48
49it('should create basket item directly from the rest api', inject([BasketService, HttpTestingController],
50fakeAsync((service: BasketService, httpMock: HttpTestingController) => {
51let res: any
52service.save().subscribe((data) => (res = data))
53const req = httpMock.expectOne('http://localhost:3000/api/BasketItems/')
54req.flush({ data: 'apiResponse' })
55tick()
56expect(req.request.method).toBe('POST')
57expect(res).toBe('apiResponse')
58httpMock.verify()
59})
60))
61
62it('should update basket item directly from the rest api', inject([BasketService, HttpTestingController],
63fakeAsync((service: BasketService, httpMock: HttpTestingController) => {
64let res: any
65service.put(1, {}).subscribe((data) => (res = data))
66const req = httpMock.expectOne('http://localhost:3000/api/BasketItems/1')
67req.flush({ data: 'apiResponse' })
68tick()
69expect(req.request.method).toBe('PUT')
70expect(res).toBe('apiResponse')
71httpMock.verify()
72})
73))
74
75it('should delete basket item directly from the rest api', inject([BasketService, HttpTestingController],
76fakeAsync((service: BasketService, httpMock: HttpTestingController) => {
77let res: any
78service.del(1).subscribe((data) => (res = data))
79const req = httpMock.expectOne('http://localhost:3000/api/BasketItems/1')
80req.flush({ data: 'apiResponse' })
81tick()
82expect(req.request.method).toBe('DELETE')
83expect(res).toBe('apiResponse')
84httpMock.verify()
85})
86))
87
88it('should place order for basket via the rest api', inject([BasketService, HttpTestingController],
89fakeAsync((service: BasketService, httpMock: HttpTestingController) => {
90let res: any
91service.checkout(1).subscribe((data) => (res = data))
92const req = httpMock.expectOne('http://localhost:3000/rest/basket/1/checkout')
93req.flush({ orderConfirmation: 'apiResponse' })
94tick()
95expect(req.request.method).toBe('POST')
96expect(res).toBe('apiResponse')
97httpMock.verify()
98})
99))
100
101it('should apply coupon to basket via the rest api', inject([BasketService, HttpTestingController],
102fakeAsync((service: BasketService, httpMock: HttpTestingController) => {
103let res: any
104service.applyCoupon(1, '1234567890').subscribe((data) => (res = data))
105const req = httpMock.expectOne('http://localhost:3000/rest/basket/1/coupon/1234567890')
106req.flush({ discount: 'apiResponse' })
107tick()
108expect(req.request.method).toBe('PUT')
109expect(res).toBe('apiResponse')
110httpMock.verify()
111})
112))
113})
114