juice-shop

Форк
0
/
challenge.service.spec.ts 
145 строк · 5.2 Кб
1
/*
2
 * Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3
 * SPDX-License-Identifier: MIT
4
 */
5

6
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'
7
import { fakeAsync, inject, TestBed, tick } from '@angular/core/testing'
8

9
import { ChallengeService } from './challenge.service'
10

11
describe('ChallengeService', () => {
12
  beforeEach(() => {
13
    TestBed.configureTestingModule({
14
      imports: [
15
        HttpClientTestingModule
16
      ],
17
      providers: [ChallengeService]
18
    })
19
  })
20

21
  it('should be created', inject([ChallengeService], (service: ChallengeService) => {
22
    expect(service).toBeTruthy()
23
  }))
24

25
  it('should get all challenges directly from the rest api', inject([ChallengeService, HttpTestingController],
26
    fakeAsync((service: ChallengeService, httpMock: HttpTestingController) => {
27
      let res: any
28
      service.find().subscribe((data) => (res = data))
29

30
      const req = httpMock.expectOne('http://localhost:3000/api/Challenges/')
31
      req.flush({ data: 'apiResponse' })
32
      tick()
33

34
      expect(req.request.method).toBe('GET')
35
      expect(res).toBe('apiResponse')
36
      httpMock.verify()
37
    })
38
  ))
39

40
  it('should get current continue code directly from the rest api', inject([ChallengeService, HttpTestingController],
41
    fakeAsync((service: ChallengeService, httpMock: HttpTestingController) => {
42
      let res: any
43
      service.continueCode().subscribe((data) => (res = data))
44

45
      const req = httpMock.expectOne('http://localhost:3000/rest/continue-code')
46
      req.flush({ continueCode: 'apiResponse' })
47
      tick()
48

49
      expect(req.request.method).toBe('GET')
50
      expect(res).toBe('apiResponse')
51
      httpMock.verify()
52
    })
53
  ))
54

55
  it('should pass continue code for restoring challenge progress on to the rest api', inject([ChallengeService, HttpTestingController],
56
    fakeAsync((service: ChallengeService, httpMock: HttpTestingController) => {
57
      let res: any
58
      service.restoreProgress('CODE').subscribe((data) => (res = data))
59

60
      const req = httpMock.expectOne('http://localhost:3000/rest/continue-code/apply/CODE')
61
      req.flush({ data: 'apiResponse' })
62
      tick()
63

64
      expect(req.request.method).toBe('PUT')
65
      expect(res).toBe('apiResponse')
66
      httpMock.verify()
67
    })
68
  ))
69

70
  it('should get current "Find It" coding challenge continue code directly from the rest api', inject([ChallengeService, HttpTestingController],
71
    fakeAsync((service: ChallengeService, httpMock: HttpTestingController) => {
72
      let res: any
73
      service.continueCodeFindIt().subscribe((data) => (res = data))
74

75
      const req = httpMock.expectOne('http://localhost:3000/rest/continue-code-findIt')
76
      req.flush({ continueCode: 'apiResponse' })
77
      tick()
78

79
      expect(req.request.method).toBe('GET')
80
      expect(res).toBe('apiResponse')
81
      httpMock.verify()
82
    })
83
  ))
84

85
  it('should pass "Find It" coding challenge continue code for restoring progress on to the rest api', inject([ChallengeService, HttpTestingController],
86
    fakeAsync((service: ChallengeService, httpMock: HttpTestingController) => {
87
      let res: any
88
      service.restoreProgressFindIt('CODE').subscribe((data) => (res = data))
89

90
      const req = httpMock.expectOne('http://localhost:3000/rest/continue-code-findIt/apply/CODE')
91
      req.flush({ data: 'apiResponse' })
92
      tick()
93

94
      expect(req.request.method).toBe('PUT')
95
      expect(res).toBe('apiResponse')
96
      httpMock.verify()
97
    })
98
  ))
99

100
  it('should get current "Fix It" coding challenge continue code directly from the rest api', inject([ChallengeService, HttpTestingController],
101
    fakeAsync((service: ChallengeService, httpMock: HttpTestingController) => {
102
      let res: any
103
      service.continueCodeFixIt().subscribe((data) => (res = data))
104

105
      const req = httpMock.expectOne('http://localhost:3000/rest/continue-code-fixIt')
106
      req.flush({ continueCode: 'apiResponse' })
107
      tick()
108

109
      expect(req.request.method).toBe('GET')
110
      expect(res).toBe('apiResponse')
111
      httpMock.verify()
112
    })
113
  ))
114

115
  it('should pass "Fix It" coding challenge continue code for restoring progress on to the rest api', inject([ChallengeService, HttpTestingController],
116
    fakeAsync((service: ChallengeService, httpMock: HttpTestingController) => {
117
      let res: any
118
      service.restoreProgressFixIt('CODE').subscribe((data) => (res = data))
119

120
      const req = httpMock.expectOne('http://localhost:3000/rest/continue-code-fixIt/apply/CODE')
121
      req.flush({ data: 'apiResponse' })
122
      tick()
123

124
      expect(req.request.method).toBe('PUT')
125
      expect(res).toBe('apiResponse')
126
      httpMock.verify()
127
    })
128
  ))
129

130
  it('should repeat notification directly from the rest api', inject([ChallengeService, HttpTestingController],
131
    fakeAsync((service: ChallengeService, httpMock: HttpTestingController) => {
132
      let res: any
133
      service.repeatNotification('CHALLENGE').subscribe((data) => (res = data))
134

135
      const req = httpMock.expectOne(req => req.url === 'http://localhost:3000/rest/repeat-notification')
136
      req.flush('apiResponse')
137
      tick()
138

139
      expect(req.request.method).toBe('GET')
140
      expect(req.request.params.get('challenge')).toBe('CHALLENGE')
141
      expect(res).toBe('apiResponse')
142
      httpMock.verify()
143
    })
144
  ))
145
})
146

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.