juice-shop

Форк
0
/
user.service.spec.ts 
158 строк · 5.5 Кб
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 { UserService } from './user.service'
10

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

19
  it('should be created', inject([UserService], (service: UserService) => {
20
    expect(service).toBeTruthy()
21
  }))
22

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

28
      const req = httpMock.expectOne('http://localhost:3000/rest/user/authentication-details/')
29
      req.flush({ data: 'apiResponse' })
30
      tick()
31

32
      expect(req.request.method).toBe('GET')
33
      expect(req.request.params.toString()).toBeFalsy()
34
      expect(res).toBe('apiResponse')
35
      httpMock.verify()
36
    })
37
  ))
38

39
  it('should get single users directly from the rest api', inject([UserService, HttpTestingController],
40
    fakeAsync((service: UserService, httpMock: HttpTestingController) => {
41
      let res: any
42
      service.get(1).subscribe((data) => (res = data))
43

44
      const req = httpMock.expectOne('http://localhost:3000/api/Users/1')
45
      req.flush({ data: 'apiResponse' })
46
      tick()
47

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

54
  it('should create user directly via the rest api', inject([UserService, HttpTestingController],
55
    fakeAsync((service: UserService, httpMock: HttpTestingController) => {
56
      let res: any
57
      service.save(null).subscribe((data) => (res = data))
58

59
      const req = httpMock.expectOne('http://localhost:3000/api/Users/')
60
      req.flush({ data: 'apiResponse' })
61
      tick()
62

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

70
  it('should login user directly via the rest api', inject([UserService, HttpTestingController],
71
    fakeAsync((service: UserService, httpMock: HttpTestingController) => {
72
      let res: any
73
      service.login(null).subscribe((data) => (res = data))
74

75
      const req = httpMock.expectOne('http://localhost:3000/rest/user/login')
76
      req.flush({ authentication: 'apiResponse' })
77
      tick()
78

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

86
  it('should change user password directly via the rest api', inject([UserService, HttpTestingController],
87
    fakeAsync((service: UserService, httpMock: HttpTestingController) => {
88
      let res: any
89
      service.changePassword({ current: 'foo', new: 'bar', repeat: 'bar' }).subscribe((data) => (res = data))
90

91
      const req = httpMock.expectOne('http://localhost:3000/rest/user/change-password?current=foo&new=bar&repeat=bar')
92
      req.flush({ user: 'apiResponse' })
93
      tick()
94

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

101
  it('should return the logged-in users identity directly from the rest api', inject([UserService, HttpTestingController],
102
    fakeAsync((service: UserService, httpMock: HttpTestingController) => {
103
      let res: any
104
      service.whoAmI().subscribe((data) => (res = data))
105

106
      const req = httpMock.expectOne('http://localhost:3000/rest/user/whoami')
107
      req.flush({ user: 'apiResponse' })
108
      tick()
109

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

116
  it('should reset the password directly from the rest api', inject([UserService, HttpTestingController],
117
    fakeAsync((service: UserService, httpMock: HttpTestingController) => {
118
      let res: any
119
      const mockObject = { req: 'apiRequest' }
120
      service.resetPassword(mockObject).subscribe((data) => (res = data))
121

122
      const req = httpMock.expectOne('http://localhost:3000/rest/user/reset-password')
123
      req.flush({ user: 'apiResponse' })
124
      tick()
125

126
      expect(req.request.method).toBe('POST')
127
      expect(req.request.body).toEqual(mockObject)
128
      expect(res).toBe('apiResponse')
129
      httpMock.verify()
130
    })
131
  ))
132

133
  it('should get users deluxe status directly from the rest api', inject([UserService, HttpTestingController],
134
    fakeAsync((service: UserService, httpMock: HttpTestingController) => {
135
      let res
136
      service.deluxeStatus().subscribe((data) => (res = data))
137
      const req = httpMock.expectOne('http://localhost:3000/rest/deluxe-membership')
138
      req.flush({ data: 'apiResponse' })
139
      tick()
140
      expect(req.request.method).toBe('GET')
141
      expect(res).toBe('apiResponse')
142
      httpMock.verify()
143
    })
144
  ))
145

146
  it('should upgrade users deluxe status directly from the rest api', inject([UserService, HttpTestingController],
147
    fakeAsync((service: UserService, httpMock: HttpTestingController) => {
148
      let res
149
      service.upgradeToDeluxe('wallet', null).subscribe((data) => (res = data))
150
      const req = httpMock.expectOne('http://localhost:3000/rest/deluxe-membership')
151
      req.flush({ data: 'apiResponse' })
152
      tick()
153
      expect(req.request.method).toBe('POST')
154
      expect(res).toBe('apiResponse')
155
      httpMock.verify()
156
    })
157
  ))
158
})
159

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

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

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

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