Skip to content

Commit d77a0dd

Browse files
committed
user preference change for token
1 parent b9e427f commit d77a0dd

6 files changed

Lines changed: 30 additions & 12 deletions

File tree

app/lib/methods/helpers/fetch.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,16 @@ describe('helpers/fetch', () => {
8989
describe('helpers/fetch setBasicAuth', () => {
9090
it('delegates to sdk.setBasicAuth', () => {
9191
setBasicAuth('dXNlcjpwYXNz');
92-
expect(sdk.setBasicAuth).toHaveBeenCalledWith('dXNlcjpwYXNz');
92+
expect(sdk.setBasicAuth).toHaveBeenCalledWith('dXNlcjpwYXNz', undefined);
9393
});
9494

9595
it('forwards null to clear basic auth', () => {
9696
setBasicAuth(null);
97-
expect(sdk.setBasicAuth).toHaveBeenCalledWith(null);
97+
expect(sdk.setBasicAuth).toHaveBeenCalledWith(null, undefined);
98+
});
99+
100+
it('forwards an explicit server so the target-server key is used, not the sdk-tracked one', () => {
101+
setBasicAuth('dXNlcjpwYXNz', 'https://new.example.com');
102+
expect(sdk.setBasicAuth).toHaveBeenCalledWith('dXNlcjpwYXNz', 'https://new.example.com');
98103
});
99104
});

app/lib/methods/helpers/fetch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ interface IOptions {
1313

1414
export { headers };
1515

16-
export const setBasicAuth = (basicAuth: string | null): void => {
17-
sdk.setBasicAuth(basicAuth);
16+
export const setBasicAuth = (basicAuth: string | null, server?: string): void => {
17+
sdk.setBasicAuth(basicAuth, server);
1818
};
1919

2020
export const BASIC_AUTH_KEY = 'BASIC_AUTH_KEY';

app/lib/services/sdk.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,20 @@ describe('Sdk header lifecycle', () => {
764764
delete (headers as any).Authorization; // mutate the returned copy
765765
expect(sdk.getHeaders().Authorization).toBe('Basic dXNlcjpwYXNz');
766766
});
767+
768+
it('persists setBasicAuth under an explicitly passed server, not the stale this.serverUrl (switch-server regression)', () => {
769+
(sdk as any).serverUrl = 'https://old.example.com';
770+
sdk.setBasicAuth('dXNlcjpwYXNz', 'https://new.example.com');
771+
expect(UserPreferences.setString).toHaveBeenCalledWith('BASIC_AUTH_KEY-https://new.example.com', 'dXNlcjpwYXNz');
772+
expect(UserPreferences.setString).not.toHaveBeenCalledWith('BASIC_AUTH_KEY-https://old.example.com', expect.anything());
773+
});
774+
775+
it('clears setBasicAuth(null) under an explicitly passed server, not the stale this.serverUrl', () => {
776+
(sdk as any).serverUrl = 'https://old.example.com';
777+
sdk.setBasicAuth(null, 'https://new.example.com');
778+
expect(UserPreferences.removeItem).toHaveBeenCalledWith('BASIC_AUTH_KEY-https://new.example.com');
779+
expect(UserPreferences.removeItem).not.toHaveBeenCalledWith('BASIC_AUTH_KEY-https://old.example.com');
780+
});
767781
});
768782

769783
describe('Sdk.onStreamData', () => {

app/lib/services/sdk.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,15 @@ class Sdk {
120120
return { ...this.headers };
121121
}
122122

123-
setBasicAuth(basicAuth: string | null): void {
124-
const url = this.serverUrl;
123+
setBasicAuth(basicAuth: string | null, server: string | undefined = this.serverUrl): void {
125124
if (basicAuth) {
126-
if (url) {
127-
UserPreferences.setString(`${BASIC_AUTH_KEY}-${url}`, basicAuth);
125+
if (server) {
126+
UserPreferences.setString(`${BASIC_AUTH_KEY}-${server}`, basicAuth);
128127
}
129128
this.setHeaders({ Authorization: `Basic ${basicAuth}` });
130129
} else {
131-
if (url) {
132-
UserPreferences.removeItem(`${BASIC_AUTH_KEY}-${url}`);
130+
if (server) {
131+
UserPreferences.removeItem(`${BASIC_AUTH_KEY}-${server}`);
133132
}
134133
const next = { ...this.headers };
135134
delete next.Authorization;

app/sagas/selectServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ const handleSelectServer = function* handleSelectServer({ server, version, fetch
182182
}
183183

184184
const basicAuth = UserPreferences.getString(`${BASIC_AUTH_KEY}-${server}`);
185-
setBasicAuth(basicAuth);
185+
setBasicAuth(basicAuth, server);
186186

187187
if (user) {
188188
yield put(clearSettings());

app/views/NewServerView/methods/basicAuth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const basicAuth = (server: string, text: string) => {
1010
if (parsedUrl.auth.length) {
1111
const credentials = Base64.encode(parsedUrl.auth);
1212
UserPreferences.setString(`${BASIC_AUTH_KEY}-${server}`, credentials);
13-
setBasicAuth(credentials);
13+
setBasicAuth(credentials, server);
1414
}
1515
} catch {
1616
// do nothing

0 commit comments

Comments
 (0)