Exemplo n.º 1
0
        public async Task AddShortUrl_AlreadyExists_ViolatesUniqueness(int id)
        {
            var url = new GkamaUrl()
            {
                Id       = id,
                Target   = "https://www.google.com/maps",
                ShortUrl = "https://gkama.it/5"
            };

            await Assert.ThrowsAsync <InvalidOperationException>(() => _repo.AddUrlAsync(url));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Shortens a url
        /// </summary>
        public async Task <string> ShortenUrlAsync(ShortenUrlRequest request)
        {
            if (request == null)
            {
                throw new InvalidShortenUrlRequestException("ShortenUrlRequest is null");
            }

            if (string.IsNullOrEmpty(request.LongUrl) || !Uri.TryCreate(request.LongUrl, UriKind.Absolute, out Uri uri))
            {
                throw new InvalidShortenUrlRequestException("The URL is either missing or not a valid URI");
            }

            // Check if the url is a known phishing site
            var isPhish = await _phishSubmissionsRepository.IsPhishAsync(uri.Host);

            if (isPhish)
            {
                //TODO: Flag ip address
                throw new InvalidShortenUrlRequestException($"The URL {request.LongUrl} is flagged for phishing");
            }

            // TODO: add logic for shortening the submitted URL
            var encodedUrl = request.LongUrl;

            var shortUrl = new ShortUrl
            {
                Id         = Guid.NewGuid(),
                ShortenUrl = encodedUrl,
                LongUrl    = request.LongUrl,
                CreatedOn  = DateTimeOffset.UtcNow
            };

            await _urlRepository.AddUrlAsync(shortUrl);

            return(encodedUrl);
        }