示例#1
0
        public new IActionResult Patch([FromODataUri] int key, Delta <LicensePlate> delta)
        {
            if (!CurrentUser.Id.Equals(Repo.AsQueryable().Single(x => x.Id.Equals(key)).PersonId))
            {
                return(StatusCode(StatusCodes.Status403Forbidden));
            }

            var primary = new object();

            if (delta.TryGetPropertyValue("IsPrimary", out primary) && (bool)primary)
            {
                _plateService.MakeLicensePlatePrimary(key);
            }
            return(base.Patch(key, delta));
        }
        public void MakeLicensePlatePrimaryWithAllTrue_ShouldSetPrimaryTrue_RestFalse()
        {
            var repoMock = NSubstitute.Substitute.For <IGenericRepository <LicensePlate> >();

            repoMock.AsQueryable().ReturnsForAnyArgs(new List <LicensePlate>()
            {
                new LicensePlate()
                {
                    Description = "Beskrivelse 1",
                    IsPrimary   = true,
                    Id          = 1,
                    PersonId    = 1,
                },
                new LicensePlate()
                {
                    Description = "Beskrivelse 2",
                    IsPrimary   = true,
                    Id          = 2,
                    PersonId    = 1,
                },
                new LicensePlate()
                {
                    Description = "Beskrivelse 2",
                    IsPrimary   = true,
                    Id          = 3,
                    PersonId    = 1,
                },
            }.AsQueryable());

            uut = new LicensePlateService(repoMock);
            uut.MakeLicensePlatePrimary(2);
            Assert.IsFalse(repoMock.AsQueryable().ElementAt(0).IsPrimary);
            Assert.IsFalse(repoMock.AsQueryable().ElementAt(2).IsPrimary);
            Assert.IsTrue(repoMock.AsQueryable().ElementAt(1).IsPrimary);
        }
        public void MakeLicensePlatePrimary_ShouldNotEdit_PlatesBelongingToDifferentUser()
        {
            var repoMock = NSubstitute.Substitute.For <IGenericRepository <LicensePlate> >();

            repoMock.AsQueryable().ReturnsForAnyArgs(new List <LicensePlate>()
            {
                new LicensePlate()
                {
                    Description = "Beskrivelse 1",
                    IsPrimary   = false,
                    Id          = 1,
                    PersonId    = 1,
                },
                new LicensePlate()
                {
                    Description = "Beskrivelse 2",
                    IsPrimary   = false,
                    Id          = 2,
                    PersonId    = 1,
                },
                new LicensePlate()
                {
                    Description = "Beskrivelse 2",
                    IsPrimary   = false,
                    Id          = 3,
                    PersonId    = 1,
                },
                new LicensePlate()
                {
                    Description = "Beskrivelse 2",
                    IsPrimary   = true,
                    Id          = 4,
                    PersonId    = 2,
                },
                new LicensePlate()
                {
                    Description = "Beskrivelse 2",
                    IsPrimary   = true,
                    Id          = 5,
                    PersonId    = 2,
                },
            }.AsQueryable());

            uut = new LicensePlateService(repoMock);
            uut.MakeLicensePlatePrimary(2);
            Assert.IsTrue(repoMock.AsQueryable().ElementAt(1).IsPrimary);
            Assert.True(repoMock.AsQueryable().ElementAt(3).IsPrimary);
            Assert.IsTrue(repoMock.AsQueryable().ElementAt(4).IsPrimary);
        }
        public void MakeLicensePlatePrimary_ShouldReturnFalse_WhenPlateDoesntExists()
        {
            var repoMock = NSubstitute.Substitute.For <IGenericRepository <LicensePlate> >();

            repoMock.AsQueryable().ReturnsForAnyArgs(new List <LicensePlate>()
            {
                new LicensePlate()
                {
                    Description = "Beskrivelse 2",
                    IsPrimary   = true,
                    Id          = 5,
                    PersonId    = 2,
                },
            }.AsQueryable());

            uut = new LicensePlateService(repoMock);
            Assert.IsFalse(uut.MakeLicensePlatePrimary(2));
        }