@wazo/sdk 0.43.3 → 0.43.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,5 @@
1
+ import BadResponse from '../../domain/BadResponse';
2
+ import ServerError from '../../domain/ServerError';
1
3
  import getApiClient, { setRefreshToken } from '../../service/getApiClient';
2
4
  import { Auth as AuthClass } from '../Auth';
3
5
 
@@ -124,13 +126,8 @@ describe('simple/Auth', () => {
124
126
  describe('initiateIdpAuthentication', () => {
125
127
  const validResponse = { location: 'idp.com/auth', saml_session_id: 'a1b2C3d4' };
126
128
 
127
- it('should return a `location` and `saml_session_id` if the response is ok', async () => {
128
- const initiateIdpAuthentication = jest.fn(() =>
129
- Promise.resolve({
130
- ok: true,
131
- status: 303,
132
- json: () => Promise.resolve(validResponse),
133
- }));
129
+ it('should return a `location` and `saml_session_id` if the response is valid', async () => {
130
+ const initiateIdpAuthentication = jest.fn(() => validResponse);
134
131
 
135
132
  (getApiClient as any).mockImplementation(() => ({
136
133
  auth: {
@@ -141,48 +138,9 @@ describe('simple/Auth', () => {
141
138
  const response = await Auth.initiateIdpAuthentication('example.com', 'https://myapp.xyz');
142
139
  expect(response).toBe(validResponse);
143
140
  });
144
- it('should return a `NoSamlRouteError` if the response is a 404', async () => {
145
- const initiateIdpAuthentication = jest.fn(() =>
146
- Promise.resolve({
147
- ok: false,
148
- status: 404,
149
- }));
150
-
151
- (getApiClient as any).mockImplementation(() => ({
152
- auth: {
153
- initiateIdpAuthentication,
154
- },
155
- }));
156
-
157
- await expect(Auth.initiateIdpAuthentication('example.com', 'https://myapp.xyz')).rejects.toThrow(
158
- 'No route found for saml sso',
159
- );
160
- });
161
141
 
162
- it('should return an error `message` if the response is NOT ok', async () => {
163
- const initiateIdpAuthentication = jest.fn(() =>
164
- Promise.resolve({
165
- ok: false,
166
- json: () => Promise.resolve({ message: 'an error message', reason: 'an error reason' }),
167
- }));
168
-
169
- (getApiClient as any).mockImplementation(() => ({
170
- auth: {
171
- initiateIdpAuthentication,
172
- },
173
- }));
174
-
175
- await expect(Auth.initiateIdpAuthentication('example.com', 'https://myapp.xyz')).rejects.toThrow(
176
- 'an error message',
177
- );
178
- });
179
-
180
- it('should return an error `reason` if there is no `message` and the response is NOT ok', async () => {
181
- const initiateIdpAuthentication = jest.fn(() =>
182
- Promise.resolve({
183
- ok: false,
184
- json: () => Promise.resolve({ reason: 'an error reason' }),
185
- }));
142
+ it('should return a `NoSamlRouteError` if the response is a 404', async () => {
143
+ const initiateIdpAuthentication = jest.fn(() => { throw BadResponse.fromText('an error message', 404); });
186
144
 
187
145
  (getApiClient as any).mockImplementation(() => ({
188
146
  auth: {
@@ -191,48 +149,12 @@ describe('simple/Auth', () => {
191
149
  }));
192
150
 
193
151
  await expect(Auth.initiateIdpAuthentication('example.com', 'https://myapp.xyz')).rejects.toThrow(
194
- 'an error reason',
152
+ 'No route found for SAML SSO',
195
153
  );
196
154
  });
197
155
 
198
- it('should return an error with a null message if there is no `message` and `reason`', async () => {
199
- const initiateIdpAuthentication = jest.fn(() =>
200
- Promise.resolve({
201
- ok: false,
202
- json: () => Promise.resolve({}),
203
- }));
204
-
205
- (getApiClient as any).mockImplementation(() => ({
206
- auth: {
207
- initiateIdpAuthentication,
208
- },
209
- }));
210
-
211
- await expect(Auth.initiateIdpAuthentication('example.com', 'https://myapp.xyz')).rejects.toThrow('');
212
- });
213
-
214
- it('should return a NoSAMLRouteError if the response is a 404', async () => {
215
- const initiateIdpAuthentication = jest.fn(() =>
216
- Promise.resolve({
217
- ok: false,
218
- status: 404,
219
- }));
220
-
221
- (getApiClient as any).mockImplementation(() => ({
222
- auth: {
223
- initiateIdpAuthentication,
224
- },
225
- }));
226
-
227
- await expect(Auth.initiateIdpAuthentication('example.com', 'https://myapp.xyz')).rejects.toThrow('No route found for saml sso');
228
- });
229
-
230
- it('should return a SamlConfigError if the response is a 500', async () => {
231
- const initiateIdpAuthentication = jest.fn(() =>
232
- Promise.resolve({
233
- ok: false,
234
- status: 500,
235
- }));
156
+ it('should return a `SamlConfigError` if the response is a 500', async () => {
157
+ const initiateIdpAuthentication = jest.fn(() => { throw ServerError.fromText('an error message', 500); });
236
158
 
237
159
  (getApiClient as any).mockImplementation(() => ({
238
160
  auth: {
@@ -158,3 +158,23 @@ describe('With a refresh token', () => {
158
158
  });
159
159
  });
160
160
  });
161
+
162
+ describe('base64Encode', () => {
163
+ it('should encode a string using base64', () => {
164
+ const input = 'Hello World!';
165
+ const expectedOutput = 'SGVsbG8gV29ybGQh';
166
+ expect(ApiRequester.base64Encode(input)).toBe(expectedOutput);
167
+ });
168
+
169
+ it('should handle special characters', () => {
170
+ const input = 'éà$@€';
171
+ const expectedOutput = 'w6nDoCRA4oKs';
172
+ expect(ApiRequester.base64Encode(input)).toBe(expectedOutput);
173
+ });
174
+
175
+ it('should handle empty string', () => {
176
+ const input = '';
177
+ const expectedOutput = '';
178
+ expect(ApiRequester.base64Encode(input)).toBe(expectedOutput);
179
+ });
180
+ });
@@ -64,7 +64,15 @@ export default class ApiRequester {
64
64
  }
65
65
 
66
66
  static base64Encode(str: string): string {
67
- return typeof btoa !== 'undefined' ? btoa(str) : Base64.encode(str);
67
+ if (typeof btoa !== 'undefined') {
68
+ try {
69
+ return btoa(str);
70
+ } catch (error) {
71
+ // fall back to Base64.encode if btoa fails
72
+ }
73
+ }
74
+
75
+ return Base64.encode(str);
68
76
  }
69
77
 
70
78
  // @see https://github.com/facebook/flow/issues/183#issuecomment-358607052