Skip to content

Commit

Permalink
feat: remove all netrc entries on logout
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarosabu committed Oct 29, 2024
1 parent 72d788f commit 1860665
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/commands/login/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('loginCommand', () => {

await loginCommand.parseAsync(['node', 'test'])

expect(konsola.error).toHaveBeenCalledWith(mockError, true)
expect(konsola.error).toHaveBeenCalledWith(mockError, false)
})
})

Expand Down Expand Up @@ -210,15 +210,15 @@ describe('loginCommand', () => {
await loginCommand.parseAsync(['node', 'test', '--token', 'invalid-token'])

// expect(handleError).toHaveBeenCalledWith(mockError, true)
expect(konsola.error).toHaveBeenCalledWith(mockError, true)
expect(konsola.error).toHaveBeenCalledWith(mockError, false)
})
})

describe('--region', () => {
it('should handle invalid region error with correct message', async () => {
await loginCommand.parseAsync(['node', 'test', '--region', 'invalid-region'])

expect(konsola.error).toHaveBeenCalledWith(expect.any(Error), true)
expect(konsola.error).toHaveBeenCalledWith(expect.any(Error), false)

// Access the error argument
const errorArg = vi.mocked(konsola.error).mock.calls[0][0]
Expand Down
7 changes: 4 additions & 3 deletions src/commands/logout/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { isAuthorized, removeNetrcEntry } from '../../creds'
import { isAuthorized, removeAllNetrcEntries } from '../../creds'
import { logoutCommand } from './'

vi.mock('../../creds', () => ({
isAuthorized: vi.fn(),
removeNetrcEntry: vi.fn(),
removeAllNetrcEntries: vi.fn(),
}))

describe('logoutCommand', () => {
Expand All @@ -16,12 +17,12 @@ describe('logoutCommand', () => {
vi.mocked(isAuthorized).mockResolvedValue(true)

await logoutCommand.parseAsync(['node', 'test'])
expect(removeNetrcEntry).toHaveBeenCalled()
expect(removeAllNetrcEntries).toHaveBeenCalled()
})

it('should not log out the user if has not previously login', async () => {
vi.mocked(isAuthorized).mockResolvedValue(false)
await logoutCommand.parseAsync(['node', 'test'])
expect(removeNetrcEntry).not.toHaveBeenCalled()
expect(removeAllNetrcEntries).not.toHaveBeenCalled()
})
})
4 changes: 2 additions & 2 deletions src/commands/logout/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isAuthorized, removeNetrcEntry } from '../../creds'
import { isAuthorized, removeAllNetrcEntries, removeNetrcEntry } from '../../creds'

Check failure on line 1 in src/commands/logout/index.ts

View workflow job for this annotation

GitHub Actions / Lint (20)

'removeNetrcEntry' is defined but never used
import { commands } from '../../constants'
import { getProgram } from '../../program'
import { handleError, konsola } from '../../utils'
Expand All @@ -17,7 +17,7 @@ export const logoutCommand = program
return
}
try {
await removeNetrcEntry('api.storyblok.com')
await removeAllNetrcEntries()

konsola.ok(`Successfully logged out`)
}
Expand Down
18 changes: 17 additions & 1 deletion src/creds.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addNetrcEntry, getNetrcCredentials, getNetrcFilePath, isAuthorized, removeNetrcEntry } from './creds'
import { addNetrcEntry, getNetrcCredentials, getNetrcFilePath, isAuthorized, removeAllNetrcEntries, removeNetrcEntry } from './creds'
import { vol } from 'memfs'
import { join } from 'pathe'
// tell vitest to use fs mock from __mocks__ folder
Expand Down Expand Up @@ -180,6 +180,22 @@ describe('creds', async () => {
})
})

describe('removeAllNetrcEntries', () => {
it('should remove all entries from .netrc file', async () => {
vol.fromJSON({
'test/.netrc': `machine api.storyblok.com
login [email protected]
password my_access_token
region eu`,
}, '/temp')

await removeAllNetrcEntries('/temp/test/.netrc')

const content = vol.readFileSync('/temp/test/.netrc', 'utf8')

expect(content).toBe('')
})
})
describe('isAuthorized', () => {
beforeEach(() => {
vol.reset()
Expand Down
6 changes: 6 additions & 0 deletions src/creds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ export const removeNetrcEntry = async (
}
}

export function removeAllNetrcEntries(filePath = getNetrcFilePath()) {
return writeFile(filePath, '', {
mode: 0o600, // Set file permissions
})
}

export async function isAuthorized() {
try {
const machines = await getNetrcCredentials()
Expand Down

0 comments on commit 1860665

Please sign in to comment.