public async Task GetOrganisationByIdReturnsOrganisation()
        {
            var session = EntityHelpers.CreateSession("Admin");

            DatabaseContext.Sessions.Add(session);
            Client.DefaultRequestHeaders.Add("Cookie", $"access_token={session.Payload}");

            var analyticsEvents = EntityHelpers.CreateAnalyticsEvents().ToList();
            var org             = analyticsEvents[0].Service.Organisation;

            analyticsEvents[1].Service.Organisation   = org;
            analyticsEvents[1].Service.OrganisationId = org.Id;
            DatabaseContext.AddRange(analyticsEvents);
            DatabaseContext.SaveChanges();

            var requestUri = new Uri($"api/v1/analytics-event/?organisationid={org.Id}", UriKind.Relative);
            var response   = await Client.GetAsync(requestUri).ConfigureAwait(true);

            response.StatusCode.Should().Be(200);
            var content        = response.Content;
            var stringResponse = await content.ReadAsStringAsync().ConfigureAwait(true);

            var deserializedBody = JsonConvert.DeserializeObject <AnalyticsResponseList>(stringResponse);

            deserializedBody.Should().NotBeNull();
        }
        public async Task SearchOrganisationBySearchParamsReturnsOrganisationsInTheSortOrderSpecified()
        {
            var session = EntityHelpers.CreateSession("Admin");

            DatabaseContext.Sessions.Add(session);
            Client.DefaultRequestHeaders.Add("Cookie", $"access_token={session.Payload}");
            var rand          = new Random();
            var organisations = EntityHelpers.CreateOrganisations(10).ToList();
            var searchParam   = Randomm.Word();
            var first         = rand.Next(10, 20);
            var second        = rand.Next(20, 30);

            organisations[5].Name = searchParam + first;
            organisations[3].Name = searchParam + second;
            DatabaseContext.Organisations.AddRange(organisations);
            DatabaseContext.SaveChanges();
            var requestUri = new Uri($"api/v1/organisations?search={searchParam}&sort=name&direction=asc", UriKind.Relative);
            var response   = await Client.GetAsync(requestUri).ConfigureAwait(true);

            response.StatusCode.Should().Be(200);
            var content        = response.Content;
            var stringResponse = await content.ReadAsStringAsync().ConfigureAwait(true);

            var deserializedBody = JsonConvert.DeserializeObject <OrganisationResponseList>(stringResponse);

            deserializedBody.Should().NotBeNull();
            deserializedBody.Organisations[0].Name.Should().BeEquivalentTo(organisations[5].Name);
            deserializedBody.Organisations[1].Name.Should().BeEquivalentTo(organisations[3].Name);
        }
예제 #3
0
        public async Task PatchOrganisationUpdatesOrganisation()
        {
            var session = EntityHelpers.CreateSession("Admin");

            DatabaseContext.Sessions.Add(session);
            Client.DefaultRequestHeaders.Add("Cookie", $"access_token={session.Payload}");
            var organisation = EntityHelpers.CreateOrganisation();

            DatabaseContext.Organisations.Add(organisation);
            DatabaseContext.SaveChanges();
            var updOrganisation = new OrganisationRequest();

            updOrganisation.Name            = Randomm.Text();
            updOrganisation.ReviewerMessage = null; // we are assuming null means no change to the record
            var         organisationString = JsonConvert.SerializeObject(updOrganisation);
            HttpContent patchContent       = new StringContent(organisationString, Encoding.UTF8, "application/json");
            var         requestUri         = new Uri($"api/v1/organisations/{organisation.Id}", UriKind.Relative);
            var         response           = await Client.PatchAsync(requestUri, patchContent).ConfigureAwait(false);

            patchContent.Dispose();
            response.StatusCode.Should().Be(200);
            var content        = response.Content;
            var stringResponse = await content.ReadAsStringAsync().ConfigureAwait(true);

            var deserializedBody = JsonConvert.DeserializeObject <OrganisationResponse>(stringResponse);
            var organisationId   = deserializedBody.Id;
            var dbOrganisation   = DatabaseContext.Organisations.Find(organisationId);

            dbOrganisation.Should().NotBeNull();
            dbOrganisation.Name.Should().Be(organisation.Name);
            dbOrganisation.ReviewerMessage.Should().Be(organisation.ReviewerMessage);  //should not be set to null if not changed
        }
예제 #4
0
        public void CanGetASessionEntity()
        {
            var session = EntityHelpers.CreateSession();

            DatabaseContext.Add(session);
            DatabaseContext.SaveChanges();
            var result = DatabaseContext.Sessions.ToList().FirstOrDefault();

            result.Should().BeEquivalentTo(session);
        }
        public void GivenValidSessionInDatabaseRefreshSessionUpdatesLastAccessAt()
        {
            var session = EntityHelpers.CreateSession("VCSO");

            DatabaseContext.Sessions.Add(session);
            DatabaseContext.SaveChanges();
            _classUnderTest.RefreshSessionExpiry(session.Id);
            var updatedSession = DatabaseContext.Sessions.Find(session.Id);

            updatedSession.LastAccessAt.Should().BeCloseTo(DateTime.Now);
        }
        public void GivenValidSessionInDatabaseSessionGetsReturned()
        {
            var session = EntityHelpers.CreateSession("VCSO");

            DatabaseContext.Sessions.Add(session);
            DatabaseContext.SaveChanges();
            var gatewayResult = _classUnderTest.GetSessionByToken(session.Payload);

            gatewayResult.Should().NotBeNull();
            gatewayResult.Should().BeEquivalentTo(session);
        }
        public async Task GetOrganisationWithExpiredSessionReturnsNotAuthorised()
        {
            var session = EntityHelpers.CreateSession("Admin");

            session.LastAccessAt = DateTime.Today.AddDays(-2);
            DatabaseContext.Sessions.Add(session);
            Client.DefaultRequestHeaders.Add("Cookie", $"access_token={session.Payload}");
            var organisation = EntityHelpers.CreateOrganisation();

            DatabaseContext.Organisations.Add(organisation);
            DatabaseContext.SaveChanges();
            var requestUri = new Uri($"api/v1/organisations/{organisation.Id}", UriKind.Relative);
            var response   = await Client.GetAsync(requestUri).ConfigureAwait(false);

            response.StatusCode.Should().Be(401);
            DatabaseContext.Sessions.Count(s => s.UserId == session.UserId).Should().Be(0);
        }
        public async Task GetanalyticsEventWithExpiredSessionReturnsNotAuthorised()
        {
            var session = EntityHelpers.CreateSession("Admin");

            session.LastAccessAt = DateTime.Today.AddDays(-2);
            DatabaseContext.Sessions.Add(session);
            Client.DefaultRequestHeaders.Add("Cookie", $"access_token={session.Payload}");
            var analyticsEvents = EntityHelpers.CreateAnalyticsEvents().ToList();
            var org             = analyticsEvents[0].Service.Organisation;

            analyticsEvents[1].Service.Organisation   = org;
            analyticsEvents[1].Service.OrganisationId = org.Id;
            DatabaseContext.AddRange(analyticsEvents);
            DatabaseContext.SaveChanges();

            var requestUri = new Uri($"api/v1/analytics-event/?{org.Id}", UriKind.Relative);
            var response   = await Client.GetAsync(requestUri).ConfigureAwait(false);

            response.StatusCode.Should().Be(401);
            DatabaseContext.Sessions.Count(s => s.UserId == session.UserId).Should().Be(0);
        }
        public async Task GetOrganisationByIdReturnsOrganisation()
        {
            var session = EntityHelpers.CreateSession("Admin");

            DatabaseContext.Sessions.Add(session);
            Client.DefaultRequestHeaders.Add("Cookie", $"access_token={session.Payload}");
            var organisation = EntityHelpers.CreateOrganisation();

            DatabaseContext.Organisations.Add(organisation);
            DatabaseContext.SaveChanges();
            var requestUri = new Uri($"api/v1/organisations/{organisation.Id}", UriKind.Relative);
            var response   = await Client.GetAsync(requestUri).ConfigureAwait(false);

            response.StatusCode.Should().Be(200);
            var content        = response.Content;
            var stringResponse = await content.ReadAsStringAsync().ConfigureAwait(true);

            var deserializedBody = JsonConvert.DeserializeObject <OrganisationResponse>(stringResponse);

            deserializedBody.Should().NotBeNull();
        }
        //[TestCase(TestName = "Given that valid parameters are provided, organisations are added to the database")]
        public async Task PostOrganisationCreatesOrganisation()
        {
            var session = EntityHelpers.CreateSession("Admin");

            DatabaseContext.Sessions.Add(session);
            Client.DefaultRequestHeaders.Add("Cookie", $"access_token={session.Payload}");
            DatabaseContext.SaveChanges();
            var         organisation       = EntityHelpers.CreatePostOrganisation();
            var         organisationString = JsonConvert.SerializeObject(organisation);
            HttpContent postContent        = new StringContent(organisationString, Encoding.UTF8, "application/json");
            var         requestUri         = new Uri("api/v1/organisations", UriKind.Relative);
            var         response           = await Client.PostAsync(requestUri, postContent).ConfigureAwait(false);

            postContent.Dispose();
            response.StatusCode.Should().Be(201);
            var content        = response.Content;
            var stringResponse = await content.ReadAsStringAsync().ConfigureAwait(true);

            var deserializedBody = JsonConvert.DeserializeObject <OrganisationResponse>(stringResponse);
            var organisationId   = deserializedBody.Id;
            var dbOrganisation   = DatabaseContext.Organisations.Find(organisationId);

            dbOrganisation.Should().NotBeNull();
        }