Skip to content

Commit

Permalink
feat: updated tests to remove netrc
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarosabu committed Jan 9, 2025
1 parent 4573f25 commit c1cf584
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 499 deletions.
Empty file.
223 changes: 35 additions & 188 deletions src/creds.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { addNetrcEntry, getNetrcCredentials, getNetrcFilePath, isAuthorized, removeAllNetrcEntries, removeNetrcEntry } from './creds'
import { addCredentials, getCredentials, removeCredentials } from './creds'
import { vol } from 'memfs'
import { join } from 'pathe'
// tell vitest to use fs mock from __mocks__ folder
// this can be done in a setup file if fs should always be mocked
vi.mock('node:fs')
Expand All @@ -12,220 +11,68 @@ beforeEach(() => {
})

describe('creds', async () => {
describe('getNetrcFilePath', async () => {
const originalPlatform = process.platform
const originalEnv = { ...process.env }
const originalCwd = process.cwd

beforeEach(() => {
process.env = { ...originalEnv }
})

afterEach(() => {
// Restore the original platform after each test
Object.defineProperty(process, 'platform', {
value: originalPlatform,
})
// Restore process.cwd()
process.cwd = originalCwd
})

it('should return the correct path on Unix-like systems when HOME is set', () => {
// Mock the platform to be Unix-like
Object.defineProperty(process, 'platform', {
value: 'linux',
})

// Set the HOME environment variable
process.env.HOME = '/home/testuser'

const expectedPath = join('/home/testuser', '.netrc')
const result = getNetrcFilePath()

expect(result).toBe(expectedPath)
})

it('should return the correct path on Windows systems when USERPROFILE is set', () => {
// Mock the platform to be Windows
Object.defineProperty(process, 'platform', {
value: 'win32',
})

// Set the USERPROFILE environment variable
process.env.USERPROFILE = 'C:/Users/TestUser'

const expectedPath = join('C:/Users/TestUser', '.netrc')
const result = getNetrcFilePath()

expect(result).toBe(expectedPath)
})

it('should use process.cwd() when home directory is not set', () => {
// Mock the platform to be Unix-like
Object.defineProperty(process, 'platform', {
value: 'linux',
})

// Remove HOME and USERPROFILE
delete process.env.HOME
delete process.env.USERPROFILE

// Mock process.cwd()
process.cwd = vi.fn().mockReturnValue('/current/working/directory')

const expectedPath = join('/current/working/directory', '.netrc')
const result = getNetrcFilePath()

expect(result).toBe(expectedPath)
})

it('should use process.cwd() when HOME is empty', () => {
// Mock the platform to be Unix-like
Object.defineProperty(process, 'platform', {
value: 'linux',
})

// Set HOME to an empty string
process.env.HOME = ''

// Mock process.cwd()
process.cwd = vi.fn().mockReturnValue('/current/working/directory')

const expectedPath = join('/current/working/directory', '.netrc')
const result = getNetrcFilePath()

expect(result).toBe(expectedPath)
})

it('should handle Windows platform when USERPROFILE is not set', () => {
// Mock the platform to be Windows
Object.defineProperty(process, 'platform', {
value: 'win32',
})

// Remove USERPROFILE
delete process.env.USERPROFILE

// Mock process.cwd()
process.cwd = vi.fn().mockReturnValue('C:/Current/Directory')

const expectedPath = join('C:/Current/Directory', '.netrc')
const result = getNetrcFilePath()

expect(result).toBe(expectedPath)
})
})

describe('getNetrcCredentials', () => {
it('should return empty object if .netrc file does not exist', async () => {
const creds = await getNetrcCredentials()
expect(creds).toEqual({})
})
it('should return the parsed content of .netrc file', async () => {
describe('getCredentials', () => {
it('should return the parsed content of credentials.json file', async () => {
vol.fromJSON({
'test/.netrc': `machine api.storyblok.com
login [email protected]
password my_access_token
region eu`,
'test/credentials.json': JSON.stringify({
'api.storyblok.com': {
login: '[email protected]',
password: 'my_access_token',
region: 'eu',
},
}),
}, '/temp')

const credentials = await getNetrcCredentials('/temp/test/.netrc')
const credentials = await getCredentials('/temp/test/credentials.json')

expect(credentials['api.storyblok.com']).toEqual({
login: '[email protected]',
password: 'my_access_token',
region: 'eu',
})
})
it('should throw an error if credentials file does not exist', async () => {
await expect(getCredentials('/temp/test/nonexistent.json')).rejects.toThrow(
new Error('The file requested was not found'),
)
})
})

describe('addNetrcEntry', () => {
it('should add a new entry to an empty .netrc file', async () => {
describe('addCredentials', () => {
it('should add a new entry to an empty credentials file', async () => {
vol.fromJSON({
'test/.netrc': '',
'test/credentials.json': '{}',
}, '/temp')

await addNetrcEntry({
filePath: '/temp/test/.netrc',
await addCredentials({
filePath: '/temp/test/credentials.json',
machineName: 'api.storyblok.com',
login: '[email protected]',
password: 'my_access_token',
region: 'eu',
})

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

expect(content).toBe(`machine api.storyblok.com
login [email protected]
password my_access_token
region eu
`)
})
})

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

await removeNetrcEntry('api.storyblok.com', '/temp/test/.netrc')

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

expect(content).toBe('')
})
})

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('')
const content = vol.readFileSync('/temp/test/credentials.json', 'utf8')
expect(content).toBe('{\n "api.storyblok.com": {\n "login": "[email protected]",\n "password": "my_access_token",\n "region": "eu"\n }\n}')
})
})
describe('isAuthorized', () => {
beforeEach(() => {
vol.reset()
process.env.HOME = '/temp' // Ensure getNetrcFilePath points to /temp/.netrc

describe('removeCredentials', () => {
it('should remove an entry from credentials file', async () => {
vol.fromJSON({
'/temp/.netrc': `machine api.storyblok.com
login [email protected]
password my_access_token
region eu`,
})
})
it('should return true if .netrc file contains an entry', async () => {
vi.doMock('./creds', () => {
return {
getNetrcCredentials: async () => {
return {
'api.storyblok.com': {
login: '[email protected]',
password: 'my_access',
region: 'eu',
},
}
'test/credentials.json': JSON.stringify({
'api.storyblok.com': {
login: '[email protected]',
password: 'my_access_token',
region: 'eu',
},
}
})
}),
}, '/temp')

const result = await isAuthorized()
await removeCredentials('api.storyblok.com', '/temp/test')

expect(result).toBe(true)
const content = vol.readFileSync('/temp/test/credentials.json', 'utf8')
expect(content).toBe('{}')
})
})
})
Loading

0 comments on commit c1cf584

Please sign in to comment.