using NUnit.Framework; using Aurora.Proto.PartyV2; using Aurora.Services.Server; using Aurora.Services.Library; using Aurora.Services.Settings; using Aurora.test.Models.Mock; using Grpc.Core; using System.Threading.Tasks; using System.Linq; using System.IO; using Autofac; namespace Aurora.test.ControllerTests { public class MemberControllerTests { private RemotePartyService.RemotePartyServiceClient _remotePartyService; private Channel _channel; private IContainer _container; private IServerService _serverService; [OneTimeSetUp] public void SetupOneTime() { ContainerBuilder builder = new ContainerBuilder(); builder.RegisterType().As().SingleInstance(); builder.RegisterInstance(new SettingsServiceMock() { Username = "Test User 1", DefaultPort = 4005, LibraryLocation = string.Format("{0}/Resource", Directory.GetCurrentDirectory()) }).SingleInstance(); builder.RegisterType().As().SingleInstance(); _container = builder.Build(); } [OneTimeTearDown] public void TearDownOneTime() { _container.Dispose(); } [SetUp] public void Setup() { _serverService = _container.Resolve(); _serverService.Start("testParty", "asdf"); _channel = new Channel(string.Format("{0}:{1}", ServerService.GetLocalIPAddress(), 8080), ChannelCredentials.Insecure); _remotePartyService = new RemotePartyService.RemotePartyServiceClient(_channel); } [TearDown] public async Task TearDown() { await _serverService.Stop(); await _channel.ShutdownAsync(); } [Test] public void DefaultTest() { ListMembersResponse resp = _remotePartyService.ListMembers(new ListMembersRequest() { Parent = "party1", PageSize = 10, }); Assert.NotNull(resp); Assert.GreaterOrEqual(resp.Members.Count, 1); } [Test] [TestCase("Alex")] [TestCase("Alex Goldberg")] [TestCase("Alex/goldberg")] [TestCase("alex@welcome.com")] public void CreateMemberTest(string value) { Member member = _remotePartyService.CreateMember(new CreateMemberRequest() { Parent = "party1", Member = new Member() { UserName = value, IpAddress = ServerService.GetLocalIPAddress(), } }); Assert.NotNull(member); } static object[] MultipleMembersCases = { new object[] {"Tupac", "Aubrey Grahm", "Beyonce Knowls", "Ke$ha", "A$ap Ferg"}, }; [Test] [TestCaseSource("MultipleMembersCases")] public void CreateMultiplMembersTest(object[] memberNames) { //Add members foreach (string name in memberNames) { Member member = _remotePartyService.CreateMember(new CreateMemberRequest() { Parent = "party1", Member = new Member() { UserName = name } }); Assert.NotNull(member); } //List members ListMembersResponse resp = _remotePartyService.ListMembers(new ListMembersRequest() { Parent = "party1", PageSize = 10, }); Assert.NotNull(resp); Assert.AreEqual(resp.Members.Count, 6); } [Test] [TestCaseSource("MultipleMembersCases")] public void DeleteMemberTest(object[] memberNames) { //Add members foreach (string name in memberNames) { Member member = _remotePartyService.CreateMember(new CreateMemberRequest() { Parent = "party1", Member = new Member() { UserName = name } }); Assert.NotNull(member); } //List members ListMembersResponse resp = _remotePartyService.ListMembers(new ListMembersRequest() { Parent = "party1", PageSize = 10, }); Assert.NotNull(resp); Assert.AreEqual(resp.Members.Count, 6); string keshaResourceName = resp.Members.First(member => member.UserName == "Ke$ha").Name; //Delete member _remotePartyService.DeleteMember(new DeleteMemberRequest() { Name = keshaResourceName }); //List members resp = _remotePartyService.ListMembers(new ListMembersRequest() { Parent = "party1", PageSize = 10, }); Assert.NotNull(resp); Assert.AreEqual(resp.Members.Count, 5); Assert.False(resp.Members.Any(member => member.UserName == "Ke$sha")); } static object[] PagingCases = { new object[] {"Tupac", "Aubrey Grahm", "Beyonce Knowls", "Ke$ha", "A$ap Ferg", "asdf", "sdfa", "dfas", "fasd"}, }; [Test] [TestCaseSource("PagingCases")] public void MemberPagingTest(object[] members) { foreach (string name in members) { Member member = _remotePartyService.CreateMember(new CreateMemberRequest() { Parent = "party1", Member = new Member() { UserName = name } }); Assert.NotNull(member); } //List members ListMembersResponse resp = _remotePartyService.ListMembers(new ListMembersRequest() { Parent = "party1", PageSize = 2, }); string nextPageToken = resp.NextPageToken; Assert.AreEqual(resp.Members.Count, 2); //List members resp = _remotePartyService.ListMembers(new ListMembersRequest() { Parent = "party1", PageSize = 2, PageToken = nextPageToken, }); Assert.AreEqual(resp.Members.Count, 2); } } }