-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor credential management to handle non-existent files and…
… update session handling - Updated `getCredentials` to create a credentials file if it does not exist, returning an empty object. - Changed `removeCredentials` to accept a region code instead of a machine name, using the region to determine the machine name. - Refactored `persistCredentials` to use region code directly. - Updated tests to reflect changes in credential management and session handling.
- Loading branch information
1 parent
3d2f936
commit 445f8ab
Showing
7 changed files
with
75 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,60 @@ | ||
import { isAuthorized, removeAllNetrcEntries } from '../../creds' | ||
import { logoutCommand } from './' | ||
import { session } from '../../session' | ||
|
||
import { removeAllCredentials } from '../../creds' | ||
|
||
vi.mock('../../creds', () => ({ | ||
isAuthorized: vi.fn(), | ||
removeNetrcEntry: vi.fn(), | ||
removeAllNetrcEntries: vi.fn(), | ||
getCredentials: vi.fn(), | ||
addCredentials: vi.fn(), | ||
removeCredentials: vi.fn(), | ||
removeAllCredentials: vi.fn(), | ||
})) | ||
|
||
// Mocking the session module | ||
vi.mock('../../session', () => { | ||
let _cache: Record<string, any> | null = null | ||
const session = () => { | ||
if (!_cache) { | ||
_cache = { | ||
state: { | ||
isLoggedIn: true, | ||
password: 'valid-token', | ||
region: 'eu', | ||
}, | ||
updateSession: vi.fn(), | ||
persistCredentials: vi.fn(), | ||
initializeSession: vi.fn(), | ||
} | ||
} | ||
return _cache | ||
} | ||
|
||
return { | ||
session, | ||
} | ||
}) | ||
|
||
describe('logoutCommand', () => { | ||
beforeEach(() => { | ||
vi.resetAllMocks() | ||
vi.clearAllMocks() | ||
}) | ||
|
||
it('should log out the user if has previously login', async () => { | ||
vi.mocked(isAuthorized).mockResolvedValue(true) | ||
|
||
session().state = { | ||
isLoggedIn: true, | ||
password: 'valid-token', | ||
region: 'eu', | ||
} | ||
await logoutCommand.parseAsync(['node', 'test']) | ||
expect(removeAllNetrcEntries).toHaveBeenCalled() | ||
expect(removeAllCredentials).toHaveBeenCalled() | ||
}) | ||
|
||
it('should not log out the user if has not previously login', async () => { | ||
vi.mocked(isAuthorized).mockResolvedValue(false) | ||
session().state = { | ||
isLoggedIn: false, | ||
} | ||
await logoutCommand.parseAsync(['node', 'test']) | ||
expect(removeAllNetrcEntries).not.toHaveBeenCalled() | ||
expect(removeAllCredentials).not.toHaveBeenCalled() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters