public void Should_create_lea_association()
            {
                //Arrange
                var vendor = new Vendor {
                    VendorName = vendorName
                };

                vendor.CreateApplication(appName, ClaimSetName);

                var educationOrganizationAssociation = vendor.Applications.AsEnumerable()
                                                       .ElementAt(0)
                                                       .CreateApplicationEducationOrganization(leaId);

                using (var context = new SqlServerUsersContext(ConnectionString))
                {
                    vendor.Applications.AsEnumerable()
                    .ElementAt(0)
                    .OperationalContextUri = "uri://ed-fi-api-host.org";

                    context.ApplicationEducationOrganizations.AddOrUpdate(educationOrganizationAssociation);
                    context.Vendors.Add(vendor);
                    context.SaveChangesForTest();

                    //Act
                    var application = context.Applications.Where(app => app.ApplicationName == appName)
                                      .Include(x => x.ApplicationEducationOrganizations)
                                      .Single();

                    var applicationLocalEducationAgencies = application.ApplicationEducationOrganizations.ToArray();
                    applicationLocalEducationAgencies.Length.ShouldBe(1);

                    applicationLocalEducationAgencies[0]
                    .EducationOrganizationId.ShouldBe(leaId);
                }
            }
            public void Should_persist_the_lea_mapping_without_explicitly_adding_that_mapping_to_the_databaseContext()
            {
                using (var context = new SqlServerUsersContext(ConnectionString))
                {
                    //Arrange
                    var lea = new ApplicationEducationOrganization {
                        EducationOrganizationId = leaId
                    };

                    var application = new Application {
                        ApplicationName = appName
                    };

                    application.ApplicationEducationOrganizations.Add(lea);

                    application.OperationalContextUri = "uri://ed-fi-api-host.org";

                    //Act
                    context.Applications.Add(application);
                    context.SaveChangesForTest();

                    //Assert
                    var applicationFromDb = context.Applications.Where(x => x.ApplicationName == appName)
                                            .Include(x => x.ApplicationEducationOrganizations)
                                            .Single();

                    int[] leas = applicationFromDb.ApplicationEducationOrganizations.Select(x => x.EducationOrganizationId)
                                 .ToArray();

                    leas.ShouldBe(
                        new[] { leaId });
                }
            }
            public void Should_persist_the_lea_mapping_without_explicitly_adding_that_mapping_to_the_databaseContext()
            {
                using (var context = new SqlServerUsersContext(ConnectionString))
                {
                    //Arrange
                    var lea = new ApplicationEducationOrganization {
                        EducationOrganizationId = leaId
                    };

                    var client = new ApiClient(true)
                    {
                        Name = clientName
                    };

                    client.ApplicationEducationOrganizations.Add(lea);

                    //Act
                    context.Clients.Add(client);
                    context.SaveChangesForTest();

                    //Assert
                    var clientFromDb = context.Clients.Where(x => x.Name == clientName)
                                       .Include(x => x.ApplicationEducationOrganizations)
                                       .Single();

                    int[] leas = clientFromDb.ApplicationEducationOrganizations.Select(x => x.EducationOrganizationId)
                                 .ToArray();

                    leas.ShouldBe(
                        new[] { leaId });
                }
            }
示例#4
0
            protected override void Arrange()
            {
                _transaction = new TransactionScope();

                var configValueProviderStub = Stub <IConfigValueProvider>();
                var usersContextFactory     = A.Fake <IUsersContextFactory>();

                A.CallTo(() => usersContextFactory.CreateContext())
                .Returns(new SqlServerUsersContext());

                var clientAppRepo = new ClientAppRepo(usersContextFactory, configValueProviderStub);

                _testClient = new ApiClient(true)
                {
                    Name = $"ClientAppRepoTest{Guid.NewGuid():N}", Secret = "MySecret"
                };

                using (var context = new SqlServerUsersContext())
                {
                    context.Clients.Add(_testClient);
                    context.SaveChanges();
                }

                _apiClientSecretProvider = new EdFiAdminApiClientIdentityProvider(clientAppRepo);
            }
 public void OneTimeSetUp()
 {
     using (var usersContext = new SqlServerUsersContext())
     {
         ConnectionString = usersContext.Database.Connection.ConnectionString;
     }
 }
            public void Should_create_application()
            {
                //Arrange
                var vendor = new Vendor {
                    VendorName = vendorName
                };

                vendor.CreateApplication(appName, ClaimSetName);
                using (var context = new SqlServerUsersContext(ConnectionString))
                {
                    vendor.Applications.AsEnumerable()
                    .ElementAt(0)
                    .OperationalContextUri = "uri://ed-fi-api-host.org";

                    context.Vendors.Add(vendor);
                    context.SaveChangesForTest();

                    //Act
                    var vendorFromDb = context.Vendors.Where(v => v.VendorName == vendorName)
                                       .Include(x => x.Applications)
                                       .Single();

                    //Assert
                    vendorFromDb.ShouldNotBeNull();
                    vendorFromDb.Applications.Count.ShouldBe(1);

                    vendorFromDb.Applications.ToList()[0]
                    .ApplicationName.ShouldBe(appName);
                }
            }
示例#7
0
            protected override void Arrange()
            {
                _transaction = new TransactionScope();

                var config = new ConfigurationBuilder()
                             .SetBasePath(TestContext.CurrentContext.TestDirectory)
                             .AddJsonFile("appsettings.json", optional: true)
                             .AddEnvironmentVariables()
                             .Build();

                var    connectionStringProvider = new ConfigConnectionStringsProvider(config);
                string connectionstring         = connectionStringProvider.GetConnectionString("EdFi_Admin");

                var usersContextFactory = A.Fake <IUsersContextFactory>();

                A.CallTo(() => usersContextFactory.CreateContext())
                .Returns(new SqlServerUsersContext(connectionstring));

                var clientAppRepo = new ClientAppRepo(usersContextFactory, config);

                _testClient = new ApiClient(true)
                {
                    Name   = $"ClientAppRepoTest{Guid.NewGuid():N}",
                    Secret = "MySecret"
                };

                using (var context = new SqlServerUsersContext(connectionstring))
                {
                    context.Clients.Add(_testClient);
                    context.SaveChanges();
                }

                _apiClientSecretProvider = new EdFiAdminApiClientIdentityProvider(clientAppRepo);
            }
        private static void MockInstanceRegistrationSetup(List <OdsInstanceRegistration> odsInstanceRegistrations)
        {
            var odsInstances = odsInstanceRegistrations.Select(x => new OdsInstance
            {
                OdsInstanceId = x.Id,
                Name          = x.Name,
                InstanceType  = "Ods",
                IsExtended    = false,
                Status        = "OK",
                Version       = "1.0.0"
            }).ToList();

            var applications = odsInstances.Select(x => new Application
            {
                ApplicationName       = x.Name.GetAdminApplicationName(),
                OperationalContextUri = OperationalContext.DefaultOperationalContextUri,
                OdsInstance           = x
            }).ToList();

            foreach (var application in applications)
            {
                var client = new ApiClient
                {
                    Name           = application.ApplicationName,
                    Key            = "test key",
                    Secret         = "test secret",
                    ActivationCode = "test activation code"
                };
                var clientAccessToken = new ClientAccessToken
                {
                    ApiClient  = client,
                    Expiration = DateTime.Now.AddDays(1)
                };
                var appEduOrganization = new ApplicationEducationOrganization
                {
                    Application             = application,
                    EducationOrganizationId = application.OdsInstance.Name.ExtractNumericInstanceSuffix()
                };

                client.ClientAccessTokens.Add(clientAccessToken);
                application.ApiClients.Add(client);
                application.ApplicationEducationOrganizations.Add(appEduOrganization);
            }

            using (var database = new SqlServerUsersContext())
            {
                foreach (var odsInstance in odsInstances)
                {
                    database.OdsInstances.Add(odsInstance);
                }

                foreach (var application in applications)
                {
                    database.Applications.Add(application);
                }

                database.SaveChanges();
            }
        }
示例#9
0
            public new void Setup()
            {
                vendorName = string.Format("{0}_TestData", DateTime.Now.Ticks);
                leaId1     = int.MaxValue - 1;
                leaId2     = int.MaxValue - 2;

                DeleteApplicationEducationOrganization(leaId1);
                DeleteApplicationEducationOrganization(leaId2);
                DeleteVendor(vendorName);

                var leaQuery = Stub <ITemplateDatabaseLeaQuery>();

                A.CallTo(() => leaQuery.GetLocalEducationAgencyIds(A <string> ._))
                .Returns(
                    new[]
                {
                    leaId1,
                    leaId2
                });

                var configValueProvider = Stub <IConfigValueProvider>();

                A.CallTo(() => configValueProvider.GetValue("DefaultApplicationName"))
                .Returns(_defaultApplicationName);

                A.CallTo(() => configValueProvider.GetValue("DefaultClaimSetName"))
                .Returns(_defaultClaimSet);

                A.CallTo(() => configValueProvider.GetValue("DefaultOperationalContextUri"))
                .Returns(_defaultOperationalContextUri);

                var usersContextFactory = Stub <IUsersContextFactory>();

                A.CallTo(() => usersContextFactory.CreateContext())
                .Returns(new SqlServerUsersContext());

                using (var context = new SqlServerUsersContext())
                {
                    var vendor = new Vendor {
                        VendorName = vendorName
                    };

                    var application = vendor.CreateApplication(_defaultApplicationName + " Sample", _defaultClaimSet);
                    application.CreateApplicationEducationOrganization(leaId1);
                    application.OperationalContextUri = _defaultOperationalContextUri;
                    context.Vendors.Add(vendor);
                    context.SaveChanges();

                    var creator = new DefaultApplicationCreator(usersContextFactory, leaQuery, configValueProvider);
                    _foundApplication = creator.FindOrCreateUpdatedDefaultSandboxApplication(vendor.VendorId, SandboxType.Sample);
                    context.SaveChanges();

                    _applications = context.Applications.Where(a => a.Vendor.VendorName == vendorName)
                                    .Include(x => x.ApplicationEducationOrganizations)
                                    .ToArray();

                    _loadedApplication = _applications.FirstOrDefault();
                }
            }
示例#10
0
            protected override void Arrange()
            {
                _transaction = new TransactionScope();

                var configValueProviderStub = Stub <IConfigValueProvider>();

                var usersContextFactory = A.Fake <IUsersContextFactory>();

                A.CallTo(() => usersContextFactory.CreateContext())
                .Returns(new SqlServerUsersContext());

                var clientAppRepo = new ClientAppRepo(usersContextFactory, configValueProviderStub);

                var edOrgs = _expectedEducationOrganizations
                             .Select(
                    edOrgId =>
                    new ApplicationEducationOrganization
                {
                    EducationOrganizationId = edOrgId
                })
                             .ToList();

                var profiles = _expectedProfiles.Select(
                    profile => new Profile
                {
                    ProfileName = profile
                })
                               .ToList();

                var application = new Application
                {
                    Profiles = profiles, ClaimSetName = "MyTestClaimSetName", Vendor = new Vendor
                    {
                        VendorNamespacePrefixes =
                            new List <VendorNamespacePrefix>
                        {
                            new VendorNamespacePrefix
                            {
                                NamespacePrefix =
                                    "MyTestNamespacePrefix"
                            }
                        }
                    },
                    OperationalContextUri = "uri://ed-fi-api-host.org"
                };

                _testClient = new ApiClient(true)
                {
                    Name = $"ClientAppRepoTest{Guid.NewGuid():N}", Application = application, ApplicationEducationOrganizations = edOrgs
                };

                using (var context = new SqlServerUsersContext())
                {
                    context.Clients.Add(_testClient);
                    context.SaveChanges();
                }

                _apiClientIdentityProvider = new EdFiAdminApiClientIdentityProvider(clientAppRepo);
            }
示例#11
0
            public new void Setup()
            {
                _vendorName = $"{DateTime.Now.Ticks}_TestData";
                _leaId      = int.MaxValue - 1;

                DeleteApplicationEducationOrganization(_leaId);
                DeleteVendor(_vendorName);

                var leaQuery = Stub <ITemplateDatabaseLeaQuery>();

                A.CallTo(() => leaQuery.GetLocalEducationAgencyIds(A <string> ._))
                .Returns(
                    new[] { _leaId });

                var configValueProvider = Stub <IConfigValueProvider>();

                A.CallTo(() => configValueProvider.GetValue("DefaultApplicationName"))
                .Returns(_defaultApplicationName);

                A.CallTo(() => configValueProvider.GetValue("DefaultClaimSetName"))
                .Returns(_defaultClaimSet);

                A.CallTo(() => configValueProvider.GetValue("DefaultOperationalContextUri"))
                .Returns(_defaultOperationalContextUri);

                var usersContextFactory = Stub <IUsersContextFactory>();

                A.CallTo(() => usersContextFactory.CreateContext())
                .Returns(new SqlServerUsersContext());

                using (var context = new SqlServerUsersContext())
                {
                    var vendor = new Vendor {
                        VendorName = _vendorName
                    };

                    context.Vendors.Add(vendor);
                    context.SaveChanges();

                    var creator = new DefaultApplicationCreator(usersContextFactory, leaQuery, configValueProvider);

                    _createdApplication =
                        creator.FindOrCreateUpdatedDefaultSandboxApplication(vendor.VendorId, SandboxType.Sample);

                    context.SaveChanges();

                    _loadedApplication =
                        context.Applications.Where(
                            a => a.ApplicationName == _createdApplication.ApplicationName &&
                            a.Vendor.VendorName == _vendorName)
                        .Include(x => x.ApplicationEducationOrganizations)
                        .Single();
                }
            }
示例#12
0
        protected override void Arrange()
        {
            _transaction = new TransactionScope();

            Factory = Stub <IUsersContextFactory>();

            A.CallTo(() => Factory.CreateContext())
            .Returns(new SqlServerUsersContext());

            SystemUnderTest = new AccessTokenClientRepo(Factory);

            TestFixtureContext = new SqlServerUsersContext();
        }
        protected void Delete <T>(
            Func <SqlServerUsersContext, IDbSet <T> > dbObject,
            Func <SqlServerUsersContext, IQueryable <T> > filter)
            where T : class
        {
            using (var context = new SqlServerUsersContext())
            {
                foreach (var tDelete in filter(context))
                {
                    dbObject(context)
                    .Remove(tDelete);
                }

                context.SaveChangesForTest();
            }
        }
            public void Should_persist_the_user_to_the_database()
            {
                using (var context = new SqlServerUsersContext(ConnectionString))
                {
                    //Arrange
                    var user = new User {
                        Email = emailAddress
                    };

                    //Act
                    context.Users.Add(user);
                    context.SaveChangesForTest();

                    //Assert
                    context.Users.Count(x => x.Email == emailAddress)
                    .ShouldBe(1);
                }
            }
示例#15
0
        protected override void Arrange()
        {
            _transaction = new TransactionScope();
            Factory      = Stub <IUsersContextFactory>();

            var config = new ConfigurationBuilder()
                         .SetBasePath(TestContext.CurrentContext.TestDirectory)
                         .AddJsonFile("appsettings.json", optional: true)
                         .AddEnvironmentVariables()
                         .Build();

            var connectionStringProvider = new ConfigConnectionStringsProvider(config);

            A.CallTo(() => Factory.CreateContext())
            .Returns(new SqlServerUsersContext(connectionStringProvider.GetConnectionString("EdFi_Admin")));

            SystemUnderTest = new AccessTokenClientRepo(Factory, config);

            TestFixtureContext = new SqlServerUsersContext(connectionStringProvider.GetConnectionString("EdFi_Admin"));
        }
        public void ShouldDeregisterOdsInstance()
        {
            var users     = SetupUsers(2).ToList();
            var testUser1 = users[0];
            var testUser2 = users[1];

            var testInstances = SetupOdsInstanceRegistrations(2).OrderBy(x => x.Name).ToList();
            var testInstanceToBeDeregistered    = testInstances[0];
            var testInstanceNotToBeDeregistered = testInstances[1];

            MockInstanceRegistrationSetup(testInstances);

            using (var database = new SqlServerUsersContext())
            {
                database.OdsInstances.Count().ShouldBe(2);
                database.Applications.Count().ShouldBe(2);
                database.Clients.Count().ShouldBe(2);
                database.ApplicationEducationOrganizations.Count().ShouldBe(2);
                database.ClientAccessTokens.Count().ShouldBe(2);
            }

            ShouldNotBeNull <SecretConfiguration>(x => x.OdsInstanceRegistrationId == testInstanceToBeDeregistered.Id);
            ShouldNotBeNull <SecretConfiguration>(x => x.OdsInstanceRegistrationId == testInstanceNotToBeDeregistered.Id);

            SetupUserWithOdsInstanceRegistrations(testUser1.Id, testInstances);
            SetupUserWithOdsInstanceRegistrations(testUser2.Id, testInstances);

            var queryInstances = new GetOdsInstanceRegistrationsByUserIdQuery(SetupContext);

            queryInstances.Execute(testUser1.Id).Count().ShouldBe(2);
            queryInstances.Execute(testUser2.Id).Count().ShouldBe(2);

            var deregisterModel = new DeregisterOdsInstanceModel
            {
                OdsInstanceId = testInstanceToBeDeregistered.Id,
                Name          = testInstanceToBeDeregistered.Name,
                Description   = testInstanceToBeDeregistered.Description
            };


            using (var sqlServerUsersContext = new SqlServerUsersContext())
            {
                var command = new DeregisterOdsInstanceCommand(SetupContext, sqlServerUsersContext);

                command.Execute(deregisterModel);
            }

            var deregisteredOdsInstance = SetupContext.OdsInstanceRegistrations.SingleOrDefault(x => x.Id == testInstanceToBeDeregistered.Id);

            deregisteredOdsInstance.ShouldBeNull();

            var notDeregisteredOdsInstance = SetupContext.OdsInstanceRegistrations.SingleOrDefault(x => x.Id == testInstanceNotToBeDeregistered.Id);

            notDeregisteredOdsInstance.ShouldNotBeNull();

            ShouldBeNull <SecretConfiguration>(x => x.OdsInstanceRegistrationId == testInstanceToBeDeregistered.Id);
            ShouldNotBeNull <SecretConfiguration>(x => x.OdsInstanceRegistrationId == testInstanceNotToBeDeregistered.Id);

            var instancesAssignedToUser1 = queryInstances.Execute(testUser1.Id).ToList();

            instancesAssignedToUser1.Count.ShouldBe(1);
            var onlyInstanceAssignedToUser1 = instancesAssignedToUser1.Single();

            onlyInstanceAssignedToUser1.Id.ShouldBe(testInstanceNotToBeDeregistered.Id);
            onlyInstanceAssignedToUser1.Name.ShouldBe(testInstanceNotToBeDeregistered.Name);
            onlyInstanceAssignedToUser1.Description.ShouldBe(testInstanceNotToBeDeregistered.Description);

            var instancesAssignedToUser2 = queryInstances.Execute(testUser2.Id).ToList();

            instancesAssignedToUser2.Count.ShouldBe(1);
            var onlyInstanceAssignedToUser2 = instancesAssignedToUser2.Single();

            onlyInstanceAssignedToUser2.Id.ShouldBe(testInstanceNotToBeDeregistered.Id);
            onlyInstanceAssignedToUser2.Name.ShouldBe(testInstanceNotToBeDeregistered.Name);
            onlyInstanceAssignedToUser2.Description.ShouldBe(testInstanceNotToBeDeregistered.Description);

            using (var database = new SqlServerUsersContext())
            {
                database.OdsInstances.Count().ShouldBe(2);
                database.Applications.Count().ShouldBe(1);
                database.Clients.Count().ShouldBe(1);
                database.ApplicationEducationOrganizations.Count().ShouldBe(1);
                database.ClientAccessTokens.Count().ShouldBe(1);
            }
        }
示例#17
0
            protected override void Arrange()
            {
                _transaction = new TransactionScope();
                var config = new ConfigurationBuilder()
                             .SetBasePath(TestContext.CurrentContext.TestDirectory)
                             .AddJsonFile("appsettings.json", optional: true)
                             .AddEnvironmentVariables()
                             .Build();

                var    connectionStringProvider = new ConfigConnectionStringsProvider(config);
                string connectionstring         = connectionStringProvider.GetConnectionString("EdFi_Admin");

                var usersContextFactory = A.Fake <IUsersContextFactory>();

                A.CallTo(() => usersContextFactory.CreateContext())
                .Returns(new SqlServerUsersContext(connectionstring));

                var clientAppRepo = new ClientAppRepo(usersContextFactory, config);

                var edOrgs = _expectedEducationOrganizations
                             .Select(
                    edOrgId =>
                    new ApplicationEducationOrganization {
                    EducationOrganizationId = edOrgId
                })
                             .ToList();

                var profiles = _expectedProfiles.Select(
                    profile => new Profile {
                    ProfileName = profile
                })
                               .ToList();

                var application = new Application
                {
                    Profiles     = profiles,
                    ClaimSetName = "MyTestClaimSetName",
                    Vendor       = new Vendor
                    {
                        VendorNamespacePrefixes =
                            new List <VendorNamespacePrefix>
                        {
                            new VendorNamespacePrefix
                            {
                                NamespacePrefix =
                                    "MyTestNamespacePrefix"
                            }
                        }
                    },
                    OperationalContextUri = "uri://ed-fi-api-host.org"
                };

                _testClient = new ApiClient(true)
                {
                    Name        = $"ClientAppRepoTest{Guid.NewGuid():N}",
                    Application = application,
                    ApplicationEducationOrganizations = edOrgs
                };

                using (var context = new SqlServerUsersContext(connectionstring))
                {
                    context.Clients.Add(_testClient);
                    context.SaveChanges();
                }

                _apiClientIdentityProvider = new EdFiAdminApiClientIdentityProvider(clientAppRepo);
            }