# ApiKeyStoreContract

> Tracon documentation. Tracon is a .NET package family that adds a control
> plane on Microsoft Agent Framework, published as NuGet packages that run
> inside your own ASP.NET Core application.
>
> Page: https://tracon.dev/api/tracon.testing.contracts.storage.apikeystorecontract/
> Summary: ApiKeyStoreContract: Behavior tests for the IApiKeyStore contract.

<div class="api-provenance">Namespace Tracon.Testing.Contracts.Storage · Assembly <code>Tracon.Testing.Contracts.Xunit.dll</code></div>

Behavior tests for the [IApiKeyStore](/api/tracon.iapikeystore/) contract.

```csharp
public abstract class ApiKeyStoreContract : TenantIsolationContract<IApiKeyStore>, IAsyncLifetime, IAsyncDisposable
```

<dl class="api-relations"><dt>Inheritance</dt><dd><a href="https://learn.microsoft.com/dotnet/api/system.object">object</a> ← <a href="/api/tracon.testing.contracts.storage.storecancellationcontract-1/">StoreCancellationContract&lt;IApiKeyStore&gt;</a> ← <a href="/api/tracon.testing.contracts.storage.tenantisolationcontract-1/">TenantIsolationContract&lt;IApiKeyStore&gt;</a> ← <a href="/api/tracon.testing.contracts.storage.apikeystorecontract/">ApiKeyStoreContract</a></dd><dt>Implements</dt><dd>IAsyncLifetime, <a href="https://learn.microsoft.com/dotnet/api/system.iasyncdisposable">IAsyncDisposable</a></dd><dt>Inherited Members</dt><dd><a href="/api/tracon.testing.contracts.storage.tenantisolationcontract-1/#Tracon_Testing_Contracts_Storage_TenantIsolationContract_1_TenantA">TenantIsolationContract&lt;IApiKeyStore&gt;.TenantA</a>, <a href="/api/tracon.testing.contracts.storage.tenantisolationcontract-1/#Tracon_Testing_Contracts_Storage_TenantIsolationContract_1_TenantB">TenantIsolationContract&lt;IApiKeyStore&gt;.TenantB</a>, <a href="/api/tracon.testing.contracts.storage.tenantisolationcontract-1/#Tracon_Testing_Contracts_Storage_TenantIsolationContract_1_UseAmbientTenant_Tracon_Testing_Contracts_Storage_MutableTenantContext_">TenantIsolationContract&lt;IApiKeyStore&gt;.UseAmbientTenant(MutableTenantContext)</a>, <a href="/api/tracon.testing.contracts.storage.tenantisolationcontract-1/#Tracon_Testing_Contracts_Storage_TenantIsolationContract_1_SeedAsync_System_String_System_String_System_Threading_CancellationToken_">TenantIsolationContract&lt;IApiKeyStore&gt;.SeedAsync(string, string, CancellationToken)</a>, <a href="/api/tracon.testing.contracts.storage.tenantisolationcontract-1/#Tracon_Testing_Contracts_Storage_TenantIsolationContract_1_ExistsAsync_System_String_System_Object_">TenantIsolationContract&lt;IApiKeyStore&gt;.ExistsAsync(string, object)</a>, <a href="/api/tracon.testing.contracts.storage.tenantisolationcontract-1/#Tracon_Testing_Contracts_Storage_TenantIsolationContract_1_CountAsync_System_String_System_Threading_CancellationToken_">TenantIsolationContract&lt;IApiKeyStore&gt;.CountAsync(string, CancellationToken)</a>, <a href="/api/tracon.testing.contracts.storage.tenantisolationcontract-1/#Tracon_Testing_Contracts_Storage_TenantIsolationContract_1_TryDeleteAsync_System_String_System_Object_">TenantIsolationContract&lt;IApiKeyStore&gt;.TryDeleteAsync(string, object)</a>, <a href="/api/tracon.testing.contracts.storage.tenantisolationcontract-1/#Tracon_Testing_Contracts_Storage_TenantIsolationContract_1_TryOverwriteAsync_System_String_System_String_">TenantIsolationContract&lt;IApiKeyStore&gt;.TryOverwriteAsync(string, string)</a>, <a href="/api/tracon.testing.contracts.storage.tenantisolationcontract-1/#Tracon_Testing_Contracts_Storage_TenantIsolationContract_1_CancellableReadAsync_System_Threading_CancellationToken_">TenantIsolationContract&lt;IApiKeyStore&gt;.CancellableReadAsync(CancellationToken)</a>, <a href="/api/tracon.testing.contracts.storage.tenantisolationcontract-1/#Tracon_Testing_Contracts_Storage_TenantIsolationContract_1_CancellableWriteAsync_System_Threading_CancellationToken_">TenantIsolationContract&lt;IApiKeyStore&gt;.CancellableWriteAsync(CancellationToken)</a>, <a href="/api/tracon.testing.contracts.storage.tenantisolationcontract-1/#Tracon_Testing_Contracts_Storage_TenantIsolationContract_1_WroteAnythingAsync">TenantIsolationContract&lt;IApiKeyStore&gt;.WroteAnythingAsync()</a>, <a href="/api/tracon.testing.contracts.storage.tenantisolationcontract-1/#Tracon_Testing_Contracts_Storage_TenantIsolationContract_1_Tenant_cannot_read_another_tenants_record">TenantIsolationContract&lt;IApiKeyStore&gt;.Tenant_cannot_read_another_tenants_record()</a>, <a href="/api/tracon.testing.contracts.storage.tenantisolationcontract-1/#Tracon_Testing_Contracts_Storage_TenantIsolationContract_1_Tenant_reads_its_own_record">TenantIsolationContract&lt;IApiKeyStore&gt;.Tenant_reads_its_own_record()</a>, <a href="/api/tracon.testing.contracts.storage.tenantisolationcontract-1/#Tracon_Testing_Contracts_Storage_TenantIsolationContract_1_Tenant_does_not_see_another_tenants_record_in_the_list">TenantIsolationContract&lt;IApiKeyStore&gt;.Tenant_does_not_see_another_tenants_record_in_the_list()</a>, <a href="/api/tracon.testing.contracts.storage.tenantisolationcontract-1/#Tracon_Testing_Contracts_Storage_TenantIsolationContract_1_Tenant_cannot_delete_another_tenants_record">TenantIsolationContract&lt;IApiKeyStore&gt;.Tenant_cannot_delete_another_tenants_record()</a>, <a href="/api/tracon.testing.contracts.storage.tenantisolationcontract-1/#Tracon_Testing_Contracts_Storage_TenantIsolationContract_1_Same_name_lives_independently_across_two_tenants">TenantIsolationContract&lt;IApiKeyStore&gt;.Same_name_lives_independently_across_two_tenants()</a>, <a href="/api/tracon.testing.contracts.storage.tenantisolationcontract-1/#Tracon_Testing_Contracts_Storage_TenantIsolationContract_1_AmbientTenant">TenantIsolationContract&lt;IApiKeyStore&gt;.AmbientTenant</a>, <a href="/api/tracon.testing.contracts.storage.storecancellationcontract-1/#Tracon_Testing_Contracts_Storage_StoreCancellationContract_1_CreateStoreAsync">StoreCancellationContract&lt;IApiKeyStore&gt;.CreateStoreAsync()</a>, <a href="/api/tracon.testing.contracts.storage.storecancellationcontract-1/#Tracon_Testing_Contracts_Storage_StoreCancellationContract_1_InitializeAsync">StoreCancellationContract&lt;IApiKeyStore&gt;.InitializeAsync()</a>, <a href="/api/tracon.testing.contracts.storage.storecancellationcontract-1/#Tracon_Testing_Contracts_Storage_StoreCancellationContract_1_DisposeAsync">StoreCancellationContract&lt;IApiKeyStore&gt;.DisposeAsync()</a>, <a href="/api/tracon.testing.contracts.storage.storecancellationcontract-1/#Tracon_Testing_Contracts_Storage_StoreCancellationContract_1_OnDisposeAsync">StoreCancellationContract&lt;IApiKeyStore&gt;.OnDisposeAsync()</a>, <a href="/api/tracon.testing.contracts.storage.storecancellationcontract-1/#Tracon_Testing_Contracts_Storage_StoreCancellationContract_1_CancellableReadAsync_System_Threading_CancellationToken_">StoreCancellationContract&lt;IApiKeyStore&gt;.CancellableReadAsync(CancellationToken)</a>, <a href="/api/tracon.testing.contracts.storage.storecancellationcontract-1/#Tracon_Testing_Contracts_Storage_StoreCancellationContract_1_CancellableWriteAsync_System_Threading_CancellationToken_">StoreCancellationContract&lt;IApiKeyStore&gt;.CancellableWriteAsync(CancellationToken)</a>, <a href="/api/tracon.testing.contracts.storage.storecancellationcontract-1/#Tracon_Testing_Contracts_Storage_StoreCancellationContract_1_WroteAnythingAsync">StoreCancellationContract&lt;IApiKeyStore&gt;.WroteAnythingAsync()</a>, <a href="/api/tracon.testing.contracts.storage.storecancellationcontract-1/#Tracon_Testing_Contracts_Storage_StoreCancellationContract_1_Canceled_token_throws_on_read">StoreCancellationContract&lt;IApiKeyStore&gt;.Canceled_token_throws_on_read()</a>, <a href="/api/tracon.testing.contracts.storage.storecancellationcontract-1/#Tracon_Testing_Contracts_Storage_StoreCancellationContract_1_The_write_this_contract_checks_can_be_read_back">StoreCancellationContract&lt;IApiKeyStore&gt;.The_write_this_contract_checks_can_be_read_back()</a>, <a href="/api/tracon.testing.contracts.storage.storecancellationcontract-1/#Tracon_Testing_Contracts_Storage_StoreCancellationContract_1_Canceled_token_throws_on_write_and_leaves_no_trace">StoreCancellationContract&lt;IApiKeyStore&gt;.Canceled_token_throws_on_write_and_leaves_no_trace()</a>, <a href="/api/tracon.testing.contracts.storage.storecancellationcontract-1/#Tracon_Testing_Contracts_Storage_StoreCancellationContract_1_Store">StoreCancellationContract&lt;IApiKeyStore&gt;.Store</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.gettype">object.GetType()</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone">object.MemberwiseClone()</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.tostring">object.ToString()</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object)">object.Equals(object?)</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)">object.Equals(object?, object?)</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.referenceequals">object.ReferenceEquals(object?, object?)</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.gethashcode">object.GetHashCode()</a></dd></dl>

## Remarks

In this contract, the raw key value is visible only in the
[ApiKeyCreationResult.PlaintextKey](/api/tracon.apikeycreationresult/#Tracon_ApiKeyCreationResult_PlaintextKey) returned by
[IApiKeyStore.CreateAsync](/api/tracon.iapikeystore/#Tracon_IApiKeyStore_CreateAsync_Tracon_ApiKeyDraft_System_Threading_CancellationToken_); no read path
([IApiKeyStore.ListAsync](/api/tracon.iapikeystore/#Tracon_IApiKeyStore_ListAsync_System_String_System_Threading_CancellationToken_), [IApiKeyStore.FindByHashAsync](/api/tracon.iapikeystore/#Tracon_IApiKeyStore_FindByHashAsync_System_ReadOnlyMemory_System_Byte__System_Threading_CancellationToken_))
returns the raw value or its hash.

## Constructors

### <a id="Tracon_Testing_Contracts_Storage_ApiKeyStoreContract__ctor"></a> ApiKeyStoreContract\(\)

```csharp
protected ApiKeyStoreContract()
```

## Methods

### <a id="Tracon_Testing_Contracts_Storage_ApiKeyStoreContract_Active_scope_is_found_system_wide"></a> Active\_scope\_is\_found\_system\_wide\(\)

```csharp
[Fact]
public Task Active_scope_is_found_system_wide()
```

#### Returns

 [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task)

### <a id="Tracon_Testing_Contracts_Storage_ApiKeyStoreContract_Cannot_revoke_another_tenants_key"></a> Cannot\_revoke\_another\_tenants\_key\(\)

```csharp
[Fact]
public Task Cannot_revoke_another_tenants_key()
```

#### Returns

 [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task)

### <a id="Tracon_Testing_Contracts_Storage_ApiKeyStoreContract_CountAsync_System_String_System_Threading_CancellationToken_"></a> CountAsync\(string, CancellationToken\)

Reports how many records the given tenant sees through the listing endpoint.

```csharp
protected override ValueTask<int> CountAsync(string tenantId, CancellationToken cancellationToken)
```

#### Parameters

`tenantId` [string](https://learn.microsoft.com/dotnet/api/system.string)

The reading tenant.

`cancellationToken` [CancellationToken](https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken)

The token every store call inside the hook must be handed; see
[TenantIsolationContract.SeedAsync](/api/tracon.testing.contracts.storage/).

#### Returns

 [ValueTask](https://learn.microsoft.com/dotnet/api/system.threading.tasks.valuetask\-1)<[int](https://learn.microsoft.com/dotnet/api/system.int32)\>

The number of records seen.

### <a id="Tracon_Testing_Contracts_Storage_ApiKeyStoreContract_ExistsAsync_System_String_System_Object_"></a> ExistsAsync\(string, object\)

Reports whether the record is visible from the given tenant's viewpoint.

```csharp
protected override ValueTask<bool> ExistsAsync(string tenantId, object key)
```

#### Parameters

`tenantId` [string](https://learn.microsoft.com/dotnet/api/system.string)

The reading tenant.

`key` [object](https://learn.microsoft.com/dotnet/api/system.object)

The key returned by [TenantIsolationContract.SeedAsync](/api/tracon.testing.contracts.storage/).

#### Returns

 [ValueTask](https://learn.microsoft.com/dotnet/api/system.threading.tasks.valuetask\-1)<[bool](https://learn.microsoft.com/dotnet/api/system.boolean)\>

[true](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool) if the record is visible.

### <a id="Tracon_Testing_Contracts_Storage_ApiKeyStoreContract_Expiration_is_read_back"></a> Expiration\_is\_read\_back\(\)

```csharp
[Fact]
public Task Expiration_is_read_back()
```

#### Returns

 [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task)

### <a id="Tracon_Testing_Contracts_Storage_ApiKeyStoreContract_Finding_another_tenants_key_by_hash_returns_the_correct_tenant"></a> Finding\_another\_tenants\_key\_by\_hash\_returns\_the\_correct\_tenant\(\)

```csharp
[Fact]
public Task Finding_another_tenants_key_by_hash_returns_the_correct_tenant()
```

#### Returns

 [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task)

### <a id="Tracon_Testing_Contracts_Storage_ApiKeyStoreContract_Found_by_hash_of_the_raw_value"></a> Found\_by\_hash\_of\_the\_raw\_value\(\)

```csharp
[Fact]
public Task Found_by_hash_of_the_raw_value()
```

#### Returns

 [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task)

### <a id="Tracon_Testing_Contracts_Storage_ApiKeyStoreContract_Last_used_timestamp_is_updated"></a> Last\_used\_timestamp\_is\_updated\(\)

```csharp
[Fact]
public Task Last_used_timestamp_is_updated()
```

#### Returns

 [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task)

### <a id="Tracon_Testing_Contracts_Storage_ApiKeyStoreContract_Revoked_key_carries_RevokedAt_and_appears_inactive"></a> Revoked\_key\_carries\_RevokedAt\_and\_appears\_inactive\(\)

```csharp
[Fact]
public Task Revoked_key_carries_RevokedAt_and_appears_inactive()
```

#### Returns

 [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task)

### <a id="Tracon_Testing_Contracts_Storage_ApiKeyStoreContract_Revoked_keys_scope_is_no_longer_active"></a> Revoked\_keys\_scope\_is\_no\_longer\_active\(\)

```csharp
[Fact]
public Task Revoked_keys_scope_is_no_longer_active()
```

#### Returns

 [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task)

### <a id="Tracon_Testing_Contracts_Storage_ApiKeyStoreContract_Same_key_cannot_be_revoked_twice"></a> Same\_key\_cannot\_be\_revoked\_twice\(\)

```csharp
[Fact]
public Task Same_key_cannot_be_revoked_twice()
```

#### Returns

 [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task)

### <a id="Tracon_Testing_Contracts_Storage_ApiKeyStoreContract_Saved_key_is_read_back"></a> Saved\_key\_is\_read\_back\(\)

```csharp
[Fact]
public Task Saved_key_is_read_back()
```

#### Returns

 [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task)

### <a id="Tracon_Testing_Contracts_Storage_ApiKeyStoreContract_SeedAsync_System_String_System_String_System_Threading_CancellationToken_"></a> SeedAsync\(string, string, CancellationToken\)

Writes a sample record for the given tenant.

```csharp
protected override ValueTask<object> SeedAsync(string tenantId, string name, CancellationToken cancellationToken)
```

#### Parameters

`tenantId` [string](https://learn.microsoft.com/dotnet/api/system.string)

The tenant that owns the record.

`name` [string](https://learn.microsoft.com/dotnet/api/system.string)

The name distinguishing the record. The same name may be used by two tenants.

`cancellationToken` [CancellationToken](https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken)

The token every store call inside the hook must be handed. The
cancellation contract calls this hook with a token that is already
cancelled, so a hook that drops the token reports a store as sound
when it is not.

#### Returns

 [ValueTask](https://learn.microsoft.com/dotnet/api/system.threading.tasks.valuetask\-1)<[object](https://learn.microsoft.com/dotnet/api/system.object)\>

The key to use for reading the record back.

### <a id="Tracon_Testing_Contracts_Storage_ApiKeyStoreContract_TryDeleteAsync_System_String_System_Object_"></a> TryDeleteAsync\(string, object\)

Attempts to delete the record on behalf of the given tenant.

```csharp
protected override ValueTask<bool?> TryDeleteAsync(string tenantId, object key)
```

#### Parameters

`tenantId` [string](https://learn.microsoft.com/dotnet/api/system.string)

The tenant attempting the delete.

`key` [object](https://learn.microsoft.com/dotnet/api/system.object)

The key returned by [TenantIsolationContract.SeedAsync](/api/tracon.testing.contracts.storage/).

#### Returns

 [ValueTask](https://learn.microsoft.com/dotnet/api/system.threading.tasks.valuetask\-1)<[bool](https://learn.microsoft.com/dotnet/api/system.boolean)?\>

[true](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool) if the delete succeeded, [false](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool)
if the record was not found; [null](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null) if the store does
not offer deletion.

### <a id="Tracon_Testing_Contracts_Storage_ApiKeyStoreContract_Unknown_hash_is_not_found"></a> Unknown\_hash\_is\_not\_found\(\)

```csharp
[Fact]
public Task Unknown_hash_is_not_found()
```

#### Returns

 [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task)
