<commit_before>﻿using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using biz.dfch.CS.Testing.Attributes;
using Microsoft.Data.OData;

namespace biz.dfch.CS.CoffeeTracker.Client.Tests
{
    [TestClass]
    public class CoffeeTrackerServiceContextTests
    {
        private string uri = "http://CoffeeTracker/";
        private string adminUserName = "<EMAIL>";
        private string adminPassword = "<PASSWORD>";
        private const string INVALID_USERNAME = "notAnEmail";
        private const string INVALID_PASSWORD = "<PASSWORD>";


        [TestMethod]
        [ExpectedException(typeof(Exception), "token")]
        public void TryRequestWithoutTokenThrowsException()
        {
            // Arrange
            var id = 1183;

            // Act / Assert
            var sut = new CoffeeTrackerServiceContext(uri);
            // oData Client can't resolve FirstOrDefault() directly
            sut.container.Coffees.Where(c => c.Id == id).FirstOrDefault();
        }

        [TestMethod]
        [ExpectContractFailure]
        public async Task TryInstantiateWithInvalidUsernameAndPasswordThrows()
        {
            // Arrange
            var sut = new CoffeeTrackerServiceContext(uri);

            // Act / Assert
            await sut.authenticationHelper.ReceiveAndSetToken(INVALID_USERNAME, INVALID_PASSWORD);
        }

        // If this test works, the client is able to send CRUD requests
        [TestMethod]
        public void CreateReadUpdateAndDeleteUser()
        {
            // Arrange
            var userName = string.Format("ClientIntegrationTest-{0}<EMAIL>", Guid.NewGuid());
            var newUserName = string.Format("ClientIntegrationTest-{0}@<EMAIL>", Guid.NewGuid());
            var password = "<PASSWORD>";

            var user = new CoffeeTrackerService.ApplicationUser
            {
                AspNetUserId = string.Empty,
                Name = userName,
                Password = password
            };

            // / Create
            var sut = new CoffeeTrackerServiceContext(uri);
            Assert.IsNotNull(sut);

            sut.container.AddToUsers(user);
            sut.container.SaveChanges();

            // Login into the api
            sut.authenticationHelper.ReceiveAndSetToken(userName, password).Wait();

            // / Read
            // oData Client can't resolve FirstOrDefault() directly
            var createdUserFromDb = sut.container.Users.Where(u => u.Name == userName).FirstOrDefault();
            Assert.IsNotNull(createdUserFromDb);

            // / Update
            createdUserFromDb.Name = newUserName;
            sut.container.SaveChanges();

            // oData Client can't resolve FirstOrDefault() directly
            createdUserFromDb = sut.container.Users.Where(u => u.Name == userName).FirstOrDefault();
            Assert.IsNotNull(createdUserFromDb);
            Assert.AreEqual(newUserName, createdUserFromDb.Name);

            // / Delete
            sut.container.DeleteObject(createdUserFromDb);
            sut.container.SaveChanges();

            // Assert deletion
            // Since the old user should be deleted, the client needs a new token, and an admin token because
            // the client is requesting users
            sut.authenticationHelper.ReceiveAndSetToken(adminUserName, adminPassword).Wait();

            var userShouldBeNull = sut.container.Users.Where(u => u.Name == newUserName).FirstOrDefault();
            Assert.IsNull(userShouldBeNull);
        }
    }
}<commit_msg>adjusted test<commit_after>﻿using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using biz.dfch.CS.Testing.Attributes;
using Microsoft.Data.OData;

namespace biz.dfch.CS.CoffeeTracker.Client.Tests
{
    [TestClass]
    public class CoffeeTrackerServiceContextTests
    {
        private string uri = "http://CoffeeTracker/";
        private string adminUserName = "<EMAIL>";
        private string adminPassword = "<PASSWORD>";
        private const string INVALID_USERNAME = "notAnEmail";
        private const string INVALID_PASSWORD = "<PASSWORD>";


        [TestMethod]
        [ExpectedException(typeof(Exception), "token")]
        public void TryRequestWithoutTokenThrowsException()
        {
            // Arrange
            var id = 1183;

            // Act / Assert
            var sut = new CoffeeTrackerServiceContext(uri);
            // oData Client can't resolve FirstOrDefault() directly
            sut.Coffees.Where(c => c.Id == id).FirstOrDefault();
        }

        [TestMethod]
        [ExpectContractFailure]
        public async Task TryInstantiateWithInvalidUsernameAndPasswordThrows()
        {
            // Arrange
            var sut = new CoffeeTrackerServiceContext(uri);

            // Act / Assert
            await sut.authenticationHelper.ReceiveAndSetToken(INVALID_USERNAME, INVALID_PASSWORD);
        }

        // If this test works, the client is able to send CRUD requests
        [TestMethod]
        public void CreateReadUpdateAndDeleteUser()
        {
            // Arrange
            var userName = string.Format("<EMAIL>", Guid.NewGuid());
            var newUserName = string.Format("<EMAIL>", Guid.NewGuid());
            var password = "<PASSWORD>";

            var user = new CoffeeTrackerService.ApplicationUser
            {
                AspNetUserId = string.Empty,
                Name = userName,
                Password = password
            };

            // / Create
            var sut = new CoffeeTrackerServiceContext(uri);
            Assert.IsNotNull(sut);

            sut.AddToUsers(user);
            sut.SaveChanges();

            // Login into the api
            sut.authenticationHelper.ReceiveAndSetToken(userName, password).Wait();

            // / Read
            // oData Client can't resolve FirstOrDefault() directly
            var createdUserFromDb = sut.Users.Where(u => u.Name == userName).FirstOrDefault();
            Assert.IsNotNull(createdUserFromDb);

            // / Update
            createdUserFromDb.Name = newUserName;
            sut.SaveChanges();

            // oData Client can't resolve FirstOrDefault() directly
            createdUserFromDb = sut.Users.Where(u => u.Name == userName).FirstOrDefault();
            Assert.IsNotNull(createdUserFromDb);
            Assert.AreEqual(newUserName, createdUserFromDb.Name);

            // / Delete
            sut.DeleteObject(createdUserFromDb);
            sut.SaveChanges();

            // Assert deletion
            // Since the old user should be deleted, the client needs a new token, and an admin token because
            // the client is requesting users
            sut.authenticationHelper.ReceiveAndSetToken(adminUserName, adminPassword).Wait();

            var userShouldBeNull = sut.Users.Where(u => u.Name == newUserName).FirstOrDefault();
            Assert.IsNull(userShouldBeNull);
        }
    }
}
{'fraction_non_alphanumeric': 0.09478002546329042, 'fraction_numerical': 0.0014146272457207527, 'mean_word_length': 2.096802452912834, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5018086', 'n_tokens_mistral': 1836, 'n_tokens_neox': 1752, 'n_words': 403}