Ejemplo n.º 1
0
        public async Task <ActionResult <GuidDataResponse> > CreateGuidData(GuidDataRequest request)
        {
            GuidDataRequestValidator.EnsureValid(request);

            var response = await _provider.CreateGuidData(request);

            return(Created($"{UriHelper.GetEncodedUrl(Request)}/{response.Guid}", response));
        }
 public static void EnsureValid(GuidDataRequest data, string parent = null)
 {
     if (!ValidationFunctions.MaxLengthNullable(data.User, 255, out var messageUser))
     {
         throw new ValidationException("user"._path(parent), messageUser);
     }
     if (!ValidationFunctions.ExpireNullable(data.Expire, out var expire, out var messageExpire))
     {
         throw new ValidationException("expire"._path(parent), messageExpire);
     }
 }
Ejemplo n.º 3
0
        public async Task Can_Delete_Returns_False_When_Guid_Not_Found()
        {
            var guid       = Guid.NewGuid();
            var guidString = guid.ToString("N").ToUpper();
            var request    = new GuidDataRequest()
            {
                User = "******", Expire = DateTimeOffset.UtcNow.AddDays(30).ToUnixTimeSeconds().ToString()
            };

            _repoMock.DeleteGuidData(Arg.Any <Guid>()).Returns(false);

            var actual = await _provider.DeleteGuidData(guidString);

            Assert.False(actual);

            await _repoMock.Received(1).DeleteGuidData(Arg.Any <Guid>());
        }
Ejemplo n.º 4
0
        public async Task Can_Upsert_Returns_GuidData_Using_30_Days_Expire_When_No_Expire_Given()
        {
            var guid       = Guid.NewGuid();
            var guidString = guid.ToString("N").ToUpper();
            var request    = new GuidDataRequest()
            {
                User = "******", Expire = null
            };

            long autoGeneratedExpire = 0;

            await _repoMock.UpsertGuidData(Arg.Do <GuidData>(x => autoGeneratedExpire = x.Expire));

            var actual = await _provider.UpsertGuidData(guidString, request);

            Assert.True(autoGeneratedExpire > 0);

            await _repoMock.Received(1).UpsertGuidData(Arg.Any <GuidData>());
        }
Ejemplo n.º 5
0
        public async Task Can_Create_Returns_GuidData_When_No_Guid_Given()
        {
            var guid       = Guid.NewGuid();
            var guidString = guid.ToString("N").ToUpper();
            var request    = new GuidDataRequest()
            {
                User = "******", Expire = DateTimeOffset.UtcNow.AddDays(30).ToUnixTimeSeconds().ToString()
            };

            _repoMock.CreateGuidData(Arg.Any <GuidData>()).Returns(new GuidData()
            {
                Guid = guid, User = request.User, Expire = long.Parse(request.Expire)
            });

            var actual = await _provider.CreateGuidData(request);

            Assert.NotNull(actual);
            Assert.Equal(guidString, actual.Guid);
            Assert.Equal(request.User, actual.User);
            Assert.Equal(request.Expire, actual.Expire);

            await _repoMock.Received(1).CreateGuidData(Arg.Any <GuidData>());
        }
Ejemplo n.º 6
0
        public async Task Can_Upsert_Returns_Update_Flag_False_When_It_Does_A_Create()
        {
            var guid       = Guid.NewGuid();
            var guidString = guid.ToString("N").ToUpper();
            var request    = new GuidDataRequest()
            {
                User = "******", Expire = DateTimeOffset.UtcNow.AddDays(30).ToUnixTimeSeconds().ToString()
            };

            _repoMock.UpsertGuidData(Arg.Any <GuidData>()).Returns((new GuidData()
            {
                Guid = guid, User = request.User, Expire = long.Parse(request.Expire)
            }, false));

            var(actual, isUpdate) = await _provider.UpsertGuidData(guidString, request);

            Assert.NotNull(actual);
            Assert.Equal(guidString, actual.Guid);
            Assert.Equal(request.User, actual.User);
            Assert.Equal(request.Expire, actual.Expire);
            Assert.False(isUpdate);

            await _repoMock.Received(1).UpsertGuidData(Arg.Any <GuidData>());
        }
Ejemplo n.º 7
0
        public async Task <ActionResult <GuidDataResponse> > UpsertGuidData(string guid, GuidDataRequest request)
        {
            GuidValidator.EnsureValid(guid);
            GuidDataRequestValidator.EnsureValid(request);

            var(response, isUpdated) = await _provider.UpsertGuidData(guid, request);

            if (isUpdated)
            {
                return(Ok(response));
            }
            else
            {
                return(Created(UriHelper.GetEncodedUrl(Request), response));
            }
        }