-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add validation function for authorizationArgs (#35)
- Loading branch information
1 parent
3507bf0
commit e897b04
Showing
3 changed files
with
131 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { | ||
NotarizeApiKeyCredentials, | ||
NotarizeCredentials, | ||
NotarizePasswordCredentials, | ||
} from './index'; | ||
|
||
export function isPasswordCredentials( | ||
opts: NotarizeCredentials, | ||
): opts is NotarizePasswordCredentials { | ||
const creds = opts as NotarizePasswordCredentials; | ||
return creds.appleId !== undefined || creds.appleIdPassword !== undefined; | ||
} | ||
|
||
export function isApiKeyCredentials(opts: NotarizeCredentials): opts is NotarizeApiKeyCredentials { | ||
const creds = opts as NotarizeApiKeyCredentials; | ||
return creds.appleApiKey !== undefined || creds.appleApiIssuer !== undefined; | ||
} | ||
|
||
export function validateAuthorizationArgs(opts: NotarizeCredentials): NotarizeCredentials { | ||
const isPassword = isPasswordCredentials(opts); | ||
const isApiKey = isApiKeyCredentials(opts); | ||
if (isPassword && isApiKey) { | ||
throw new Error('Cannot use both password credentials and API key credentials at once'); | ||
} | ||
if (isPassword) { | ||
const passwordCreds = opts as NotarizePasswordCredentials; | ||
if (!passwordCreds.appleId) { | ||
throw new Error( | ||
'The appleId property is required when using notarization with appleIdPassword', | ||
); | ||
} else if (!passwordCreds.appleIdPassword) { | ||
throw new Error( | ||
'The appleIdPassword property is required when using notarization with appleId', | ||
); | ||
} | ||
return passwordCreds; | ||
} | ||
if (isApiKey) { | ||
const apiKeyCreds = opts as NotarizeApiKeyCredentials; | ||
if (!apiKeyCreds.appleApiKey) { | ||
throw new Error( | ||
'The appleApiKey property is required when using notarization with appleApiIssuer', | ||
); | ||
} else if (!apiKeyCreds.appleApiIssuer) { | ||
throw new Error( | ||
'The appleApiIssuer property is required when using notarization with appleApiKey', | ||
); | ||
} | ||
return apiKeyCreds; | ||
} | ||
throw new Error('No authentication properties provided (e.g. appleId, appleApiKey)'); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { | ||
NotarizeApiKeyCredentials, | ||
NotarizeCredentials, | ||
NotarizePasswordCredentials, | ||
} from '../src/index'; | ||
import { validateAuthorizationArgs } from '../src/validate-args'; | ||
|
||
describe('index', () => { | ||
describe('validateAuthorizationArgs', () => { | ||
test('password credentials provided', () => { | ||
const opts = { | ||
'appleId': 'fakeId', | ||
'appleIdPassword': 'fakePassword', | ||
} as NotarizePasswordCredentials; | ||
expect(validateAuthorizationArgs(opts)).toEqual(opts); | ||
}); | ||
|
||
test('API key credentials provided', () => { | ||
const opts = { | ||
'appleApiKey': 'fakeApiKey', | ||
'appleApiIssuer': 'fakeApiIssuer', | ||
} as NotarizeApiKeyCredentials; | ||
expect(validateAuthorizationArgs(opts)).toEqual(opts); | ||
}); | ||
|
||
test('credentials are required', () => { | ||
const opts = {} as NotarizeCredentials; | ||
expect(() => validateAuthorizationArgs(opts)) | ||
.toThrowError(new Error('No authentication properties provided (e.g. appleId, appleApiKey)')); | ||
}); | ||
|
||
test('only one kind of credentials', () => { | ||
const opts = { | ||
'appleId': 'fakeId', | ||
'appleApiKey': 'fakeApiKey', | ||
} as any; | ||
expect(() => validateAuthorizationArgs(opts)) | ||
.toThrowError(new Error('Cannot use both password credentials and API key credentials at once')); | ||
}); | ||
|
||
test('missing appleId', () => { | ||
const opts = { | ||
'appleIdPassword': 'fakePassword', | ||
} as any; | ||
expect(() => validateAuthorizationArgs(opts)) | ||
.toThrowError(new Error('The appleId property is required when using notarization with appleIdPassword')); | ||
}); | ||
|
||
test('missing appleIdPassword', () => { | ||
const opts = { | ||
'appleId': 'fakeId', | ||
} as any; | ||
expect(() => validateAuthorizationArgs(opts)) | ||
.toThrowError(new Error('The appleIdPassword property is required when using notarization with appleId')); | ||
}); | ||
|
||
test('missing appleApiKey', () => { | ||
const opts = { | ||
'appleApiIssuer': 'fakeApiIssuer', | ||
} as any; | ||
expect(() => validateAuthorizationArgs(opts)) | ||
.toThrowError(new Error('The appleApiKey property is required when using notarization with appleApiIssuer')); | ||
}); | ||
|
||
test('missing appleApiIssuer', () => { | ||
const opts = { | ||
'appleApiKey': 'fakeApiKey', | ||
} as any; | ||
expect(() => validateAuthorizationArgs(opts)) | ||
.toThrowError(new Error('The appleApiIssuer property is required when using notarization with appleApiKey')); | ||
}); | ||
}); | ||
}); |