Permalink
Browse files

addition of work to try and authenticate a user on connection

  • Loading branch information...
1 parent 6045368 commit ab3eef2c99d3aabe2f2ee0890cd5d9e620fe1353 @stack72 stack72 committed Apr 11, 2012
@@ -1,4 +1,7 @@
using System;
+using System.Dynamic;
+using System.Net;
+using System.Security.Authentication;
using EasyHttp.Http;
using EasyHttp.Infrastructure;
using TeamCitySharp.DomainEntities;
@@ -73,6 +76,7 @@ HttpClient CreateHttpRequest(string userName, string password)
{
var httpClient = new HttpClient(new TeamcityJsonEncoderDecoderConfiguration());
httpClient.Request.Accept = HttpContentTypes.ApplicationJson;
+ httpClient.ThrowExceptionOnHttpError = true;
if (!_configuration.ActAsGuest)
{
httpClient.Request.SetBasicAuthentication(userName, password);
@@ -81,5 +85,29 @@ HttpClient CreateHttpRequest(string userName, string password)
return httpClient;
}
+
+ public bool Authenticate(string urlPart)
+ {
+ try
+ {
+ var httpClient = new HttpClient{};
+ httpClient.ThrowExceptionOnHttpError = true;
+ httpClient.Request.Accept = HttpContentTypes.TextPlain;
+ if (!_configuration.ActAsGuest)
+ {
+ httpClient.Request.SetBasicAuthentication(_configuration.UserName, _configuration.Password);
+ httpClient.Request.ForceBasicAuth = true;
+ }
+
+ httpClient.Get(CreateUrl(urlPart));
+ var response = httpClient.Response;
+
+ return (response.StatusCode == HttpStatusCode.OK);
+ }
+ catch (HttpException exception)
+ {
+ throw new AuthenticationException(exception.StatusDescription);
+ }
+ }
}
}
@@ -8,6 +8,7 @@ namespace TeamCitySharp
public interface ITeamCityClient
{
void Connect(string userName, string password, bool actAsGuest = false);
+ bool IsAuthenticated { get; }
List<Project> AllProjects();
Project ProjectByName(string projectLocatorName);
Project ProjectById(string projectLocatorId);
@@ -10,6 +10,7 @@ namespace TeamCitySharp
public class TeamCityClient : IClientConnection, ITeamCityClient
{
private readonly TeamCityCaller _caller;
+ public bool IsAuthenticated { get; private set; }
public TeamCityClient(string hostName, bool useSsl = false)
{
@@ -19,6 +20,7 @@ public TeamCityClient(string hostName, bool useSsl = false)
public void Connect(string userName, string password, bool actAsGuest = false)
{
_caller.Connect(userName, password, actAsGuest);
+ IsAuthenticated = _caller.Authenticate("/app/rest");
}
public List<Project> AllProjects()
@@ -0,0 +1,35 @@
+using System.Security.Authentication;
+using NUnit.Framework;
+
+namespace TeamCitySharp.IntegrationTests
+{
+ [TestFixture]
+ public class when_connecting_to_the_teamcity_server
+ {
+ private ITeamCityClient _client;
+
+ [SetUp]
+ public void SetUp()
+ {
+ _client = new TeamCityClient("localhost:81");
+
+ }
+
+ [Test]
+ public void it_will_authenticate_a_known_user()
+ {
+ _client.Connect("admin", "qwerty");
+ Assert.That(_client.IsAuthenticated);
+ }
+
+ [Test]
+ [ExpectedException(typeof(AuthenticationException))]
+ public void it_will_throw_an_exception_for_an_unknown_user()
+ {
+ _client.Connect("smithy", "smithy");
+
+ //Assert.Throws Exception
+ }
+
+ }
+}
@@ -54,6 +54,7 @@
<Compile Include="SampleChangeUsage.cs">
<SubType>Code</SubType>
</Compile>
+ <Compile Include="SampleConnectionUsage.cs" />
<Compile Include="SampleProjectUsage.cs" />
<Compile Include="SampleServerUsage.cs">
<SubType>Code</SubType>

0 comments on commit ab3eef2

Please sign in to comment.