Skip to content

Commit

Permalink
Fix and add new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pkukielka committed Jan 14, 2025
1 parent 23fdad5 commit 5d457ce
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 5 deletions.
10 changes: 9 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,13 @@
"[typescriptreact]": {
"editor.defaultFormatter": "biomejs.biome"
},
"rust-analyzer.procMacro.ignored": { "napi-derive": ["napi"] }
"rust-analyzer.procMacro.ignored": {
"napi-derive": [
"napi"
]
},
"debug.javascript.defaultRuntimeExecutable": {
"pwa-node": "/Users/pkukielka/.local/share/mise/shims/node"
},
"python.defaultInterpreterPath": "/Users/pkukielka/.local/share/mise/shims/python"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ sealed class AuthenticationError {
"network-error" -> context.deserialize<NetworkAuthError>(element, NetworkAuthError::class.java)
"invalid-access-token" -> context.deserialize<InvalidAccessTokenError>(element, InvalidAccessTokenError::class.java)
"enterprise-user-logged-into-dotcom" -> context.deserialize<EnterpriseUserDotComError>(element, EnterpriseUserDotComError::class.java)
"auth-config-error" -> context.deserialize<AuthConfigError>(element, AuthConfigError::class.java)
else -> throw Exception("Unknown discriminator ${element}")
}
}
Expand Down Expand Up @@ -50,3 +51,14 @@ data class EnterpriseUserDotComError(
}
}

data class AuthConfigError(
val title: String? = null,
val message: String,
val type: TypeEnum, // Oneof: auth-config-error
) : AuthenticationError() {

enum class TypeEnum {
@SerializedName("auth-config-error") `Auth-config-error`,
}
}

Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
@file:Suppress("FunctionName", "ClassName", "unused", "EnumEntryName", "UnusedImport")
package com.sourcegraph.cody.agent.protocol_generated;

import com.google.gson.annotations.SerializedName;

data class CodyContextFilterItem(
val repoNamePattern: String,
val repoNamePattern: RepoNamePatternEnum, // Oneof: .*
val filePathPatterns: List<String>? = null,
)
) {

enum class RepoNamePatternEnum {
@SerializedName(".*") ``,
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package com.sourcegraph.cody.agent.protocol_generated;

object Constants {
const val `` = ".*"
const val Applied = "Applied"
const val Applying = "Applying"
const val Automatic = "Automatic"
Expand All @@ -15,6 +16,7 @@ object Constants {
const val agentic = "agentic"
const val ask = "ask"
const val assistant = "assistant"
const val `auth-config-error` = "auth-config-error"
const val authenticated = "authenticated"
const val autocomplete = "autocomplete"
const val balanced = "balanced"
Expand Down
67 changes: 65 additions & 2 deletions lib/shared/src/configuration/auth-resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ describe('auth-resolver', () => {
})

test('resolve custom auth provider', async () => {
const expirationEpoch = 2736874802
const credentialsJson = JSON.stringify({
headers: { Authorization: 'token X' },
expiration: 1337,
expiration: expirationEpoch,
})

const auth = await resolveAuth(
Expand Down Expand Up @@ -91,11 +92,73 @@ describe('auth-resolver', () => {
expect(auth.serverEndpoint).toBe('https://my-server.com/')

const headerCredential = auth.credentials as HeaderCredential
expect(headerCredential.expiration).toBe(1337)
expect(headerCredential.expiration).toBe(expirationEpoch)
expect(headerCredential.getHeaders()).toStrictEqual({
Authorization: 'token X',
})

expect(JSON.stringify(headerCredential)).not.toContain('token X')
})

test('resolve custom auth provider error handling - bad JSON', async () => {
const auth = await resolveAuth(
'sourcegraph.com',
{
authExternalProviders: [
{
endpoint: 'https://my-server.com',
executable: {
commandLine: ['echo '],
shell: isWindows() ? process.env.ComSpec : '/bin/bash',
timeout: 5000,
windowsHide: true,
},
},
],
overrideServerEndpoint: 'https://my-server.com',
overrideAuthToken: undefined,
},
new TempClientSecrets(new Map())
)

expect(auth.serverEndpoint).toBe('https://my-server.com/')

expect(auth.credentials).toBe(undefined)
expect(auth.error.message).toContain('Unexpected end of JSON input')

Check failure on line 127 in lib/shared/src/configuration/auth-resolver.test.ts

View workflow job for this annotation

GitHub Actions / test-unit (windows, 20)

src/configuration/auth-resolver.test.ts > auth-resolver > resolve custom auth provider error handling - bad JSON

AssertionError: expected 'Failed to execute external auth comma…' to contain 'Unexpected end of JSON input' Expected: "Unexpected end of JSON input" Received: "Failed to execute external auth command: Unexpected token 'E', "ECHO is on." is not valid JSON" ❯ src/configuration/auth-resolver.test.ts:127:36
})

test('resolve custom auth provider error handling - bad expiration', async () => {
const expirationEpoch = 1636865002
const credentialsJson = JSON.stringify({
headers: { Authorization: 'token X' },
expiration: expirationEpoch,
})

const auth = await resolveAuth(
'sourcegraph.com',
{
authExternalProviders: [
{
endpoint: 'https://my-server.com',
executable: {
commandLine: [
isWindows() ? `echo ${credentialsJson}` : `echo '${credentialsJson}'`,
],
shell: isWindows() ? process.env.ComSpec : '/bin/bash',
timeout: 5000,
windowsHide: true,
},
},
],
overrideServerEndpoint: 'https://my-server.com',
overrideAuthToken: undefined,
},
new TempClientSecrets(new Map())
)

expect(auth.serverEndpoint).toBe('https://my-server.com/')

expect(auth.credentials).toBe(undefined)
expect(auth.error.message).toContain('Credentials expiration cannot be se to the past date')
})
})

0 comments on commit 5d457ce

Please sign in to comment.