Skip to content

Instantly share code, notes, and snippets.

@justinyoo
Created June 17, 2016 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinyoo/706ff88512255432e2ccb5697a3f3728 to your computer and use it in GitHub Desktop.
Save justinyoo/706ff88512255432e2ccb5697a3f3728 to your computer and use it in GitHub Desktop.
Mocking ADAL for Unit Tests
public class AuthenticationContextWrapper : IAuthenticationContextWrapper
{
public AuthenticationContextWrapper(AuthenticationContext context)
{
this.Context = context;
}
public AuthenticationContextWrapper(string authority)
{
this.Context = new AuthenticationContext(authority);
}
public AuthenticationContextWrapper(string authority, bool validateAuthority)
{
this.Context = new AuthenticationContext(authority, validateAuthority);
}
public AuthenticationContextWrapper(string authority, bool validateAuthority, TokenCache tokenCache)
{
this.Context = new AuthenticationContext(authority, validateAuthority, tokenCache);
}
public AuthenticationContext Context { get; }
public string Authority => this.Context.Authority;
public bool ValidateAuthority => this.Context.ValidateAuthority;
...
public async Task<IDeviceCodeResultWrapper> AcquireDeviceCodeAsync(string resource, string clientId)
{
var result = await this.Context.AcquireDeviceCodeAsync(resource, clientId).ConfigureAwait(false);
return new DeviceCodeResultWrapper(result);
}
...
public async Task<IAuthenticationResultWrapper> AcquireTokenAsync(string resource, string clientId, UserCredential userCredential)
{
var result = await this.Context.AcquireTokenAsync(resource, clientId, userCredential).ConfigureAwait(false);
return new AuthenticationResultWrapper(result);
}
...
public async Task<IAuthenticationResultWrapper> AcquireTokenAsync(string resource, ClientCredential clientCredential)
{
var result = await this.Context.AcquireTokenAsync(resource, clientCredential).ConfigureAwait(false);
return new AuthenticationResultWrapper(result);
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment