Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace netrc with json #146

Merged
merged 7 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/commands/login/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ vi.mock('./actions', () => ({
}))

vi.mock('../../creds', () => ({
addNetrcEntry: vi.fn(),
isAuthorized: vi.fn(),
getNetrcCredentials: vi.fn(),
getCredentialsForMachine: vi.fn(),
getCredentials: vi.fn(),
addCredentials: vi.fn(),
removeCredentials: vi.fn(),
removeAllCredentials: vi.fn(),
}))

// Mocking the session module
Expand Down
8 changes: 4 additions & 4 deletions src/commands/login/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chalk from 'chalk'
import { input, password, select } from '@inquirer/prompts'
import type { RegionCode } from '../../constants'
import { colorPalette, commands, regionNames, regions, regionsDomain } from '../../constants'
import { colorPalette, commands, regionNames, regions } from '../../constants'
import { getProgram } from '../../program'
import { CommandError, handleError, isRegion, konsola } from '../../utils'
import { loginWithEmailAndPassword, loginWithOtp, loginWithToken } from './actions'
Expand Down Expand Up @@ -63,7 +63,7 @@ export const loginCommand = program
try {
const { user } = await loginWithToken(token, region)
updateSession(user.email, token, region)
await persistCredentials(regionsDomain[region])
await persistCredentials(region)

konsola.ok(`Successfully logged in with token`)
}
Expand All @@ -85,7 +85,7 @@ export const loginCommand = program
const { user } = await loginWithToken(userToken, region)

updateSession(user.email, userToken, region)
await persistCredentials(regionsDomain[region])
await persistCredentials(region)

konsola.ok(`Successfully logged in with token`)
}
Expand Down Expand Up @@ -126,7 +126,7 @@ export const loginCommand = program
else if (response?.access_token) {
updateSession(userEmail, response.access_token, userRegion)
}
await persistCredentials(regionsDomain[userRegion])
await persistCredentials(region)
konsola.ok(`Successfully logged in with email ${chalk.hex(colorPalette.PRIMARY)(userEmail)}`)
}
}
Expand Down
50 changes: 41 additions & 9 deletions src/commands/logout/index.test.ts
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()
})
})
10 changes: 6 additions & 4 deletions src/commands/logout/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { isAuthorized, removeAllNetrcEntries } from '../../creds'
import { removeAllCredentials } from '../../creds'
import { commands } from '../../constants'
import { getProgram } from '../../program'
import { handleError, konsola } from '../../utils'
import { session } from '../../session'

const program = getProgram() // Get the shared singleton instance

Expand All @@ -11,12 +12,13 @@ export const logoutCommand = program
.action(async () => {
const verbose = program.opts().verbose
try {
const isAuth = await isAuthorized()
if (!isAuth) {
const { state, initializeSession } = session()
await initializeSession()
if (!state.isLoggedIn || !state.password || !state.region) {
konsola.ok(`You are already logged out. If you want to login, please use the login command.`)
return
}
await removeAllNetrcEntries()
await removeAllCredentials()

konsola.ok(`Successfully logged out`)
}
Expand Down
6 changes: 4 additions & 2 deletions src/commands/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import chalk from 'chalk'
import type { NetrcMachine } from '../../creds'
import { colorPalette, commands } from '../../constants'
import { getProgram } from '../../program'
import { CommandError, handleError, konsola } from '../../utils'
Expand All @@ -21,7 +20,10 @@ export const userCommand = program
return
}
try {
const { password, region } = state as NetrcMachine
const { password, region } = state
if (!password || !region) {
throw new Error('No password or region found')
}
const { user } = await getUser(password, region)
konsola.ok(`Hi ${chalk.bold(user.friendly_name)}, you are currently logged in with ${chalk.hex(colorPalette.PRIMARY)(user.email)} on ${chalk.bold(region)} region`)
}
Expand Down
Loading
Loading