diff --git a/Gemfile b/Gemfile index 88403db..cebe77d 100644 --- a/Gemfile +++ b/Gemfile @@ -61,4 +61,5 @@ end group :development, :test do gem 'byebug' gem 'rspec-rails', '~> 3.4.2' + gem "better_errors" end diff --git a/Gemfile.lock b/Gemfile.lock index 5eeb476..6eea82c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -29,6 +29,10 @@ GEM tzinfo (~> 1.1) addressable (2.3.6) arel (5.0.1.20140414130214) + better_errors (2.1.1) + coderay (>= 1.0.0) + erubis (>= 2.6.6) + rack (>= 0.9.0) builder (3.2.2) byebug (3.1.2) columnize (~> 0.8) @@ -40,6 +44,7 @@ GEM rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) + coderay (1.1.0) coffee-rails (4.0.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.0) @@ -227,6 +232,7 @@ PLATFORMS ruby DEPENDENCIES + better_errors byebug coffee-rails (~> 4.0.0) cucumber-rails diff --git a/app/controllers/urls_controller.rb b/app/controllers/urls_controller.rb index e662c97..3d7ce68 100644 --- a/app/controllers/urls_controller.rb +++ b/app/controllers/urls_controller.rb @@ -29,16 +29,45 @@ def edit end def create - urls = create_shortenURL(params[:url], params[:unique_key]) - - @url = urls[:short_url] - url = urls[:url] + url_to_shorten = params[:url] + is_url_working = URLValidator.new(url: url_to_shorten).works? + domains = params[:domain_name] + unique_key = params[:unique_key] + + urls_with_errors = domains.map do |domain| + url = Shortener::ShortenedUrl.new url_params + if is_url_working + if unique_key.present? + if unique_key =~ /\A[a-zA-Z0-9]+\Z/ + if Shortener::ShortenedUrl.where(domain_name: domain). + where("lower(unique_key) = ?", unique_key.downcase).exists? + url.errors[:base] << "That short code already exists for #{domain}." + end + else + url.errors[:base] << 'Short codes can only include numbers and letters.' + end + end + url + else + url.errors[:base] << 'That URL doesn\'t seem to work.' + end + end.select do |url| + url.errors.present? + end respond_to do |format| - if url.works? and @url.errors.messages.empty? + if urls_with_errors.empty? + urls = domains.map do |domain| + url = Shortener::ShortenedUrl.create url: url_to_shorten + url.unique_key = unique_key if unique_key.present? + url.domain_name = domain + url.save + url + end format.html { redirect_to root_url } - format.json { render :show, status: :created, location: @url } + format.json { render :index, status: :created, location: urls } else + @url = urls_with_errors.first format.html { render :new } format.json { render json: @url.errors, status: :unprocessable_entity } end @@ -105,7 +134,8 @@ def url_params def load_domains @current_domain = session[:domain_name] @request_domain = request.server_name.downcase - @domains = Shortener::ShortenedUrl.select(:domain_name).distinct(:domain_name).map(&:domain_name) + @domains = Shortener::ShortenedUrl.select(:domain_name).where("domain_name is not null"). + distinct(:domain_name).map(&:domain_name) @domains << @request_domain unless @domains.include?(@request_domain) end diff --git a/features/step_definitions/url_steps.rb b/features/step_definitions/url_steps.rb index b392f25..74a0ed0 100644 --- a/features/step_definitions/url_steps.rb +++ b/features/step_definitions/url_steps.rb @@ -14,6 +14,16 @@ expect(Shortener::ShortenedUrl.count).to eq count.to_i end +Then(/^I should have Shorted URL with "(.*)\/(.*)" as url and "([^\"]*)" as target$/) do |domain, unique_key, url| + expect( + Shortener::ShortenedUrl.find_by( + domain_name: domain, + unique_key: unique_key, + url: url + ) + ).to_not be nil +end + Then(/^I should have (\d+) Shorted URL with domain name as "([^"]*)"$/) do |count, domain_name| expect(Shortener::ShortenedUrl.where(domain_name: domain_name).count).to eq count.to_i end @@ -21,3 +31,7 @@ Then /^I should see "([^\"]*)" domain checkbox as checked$/ do |domain| expect(response).to have_selector("input", id: "domain_name_input_#{domain}", checked: "checked") end + +Then /^I should see "([^\"]*)" domain checkbox as unchecked$/ do |domain| + expect(response).to_not have_selector("input", id: "domain_name_input_#{domain}", checked: "checked") +end diff --git a/features/url_management.feature b/features/url_management.feature index 04c49c3..75a3d20 100644 --- a/features/url_management.feature +++ b/features/url_management.feature @@ -3,7 +3,7 @@ Feature: URL Management and see list of previously generated short URLs @admin - Scenario: Admin logs into the dashboard and creates a short URL + Scenario: Dashboard Admin shortens URLs for more than one domain at once Given Our host is "hkk.sn" And I am an authorized user with credentials as name and password And there is a shortened URL http://omnia.ws/blah1 that goes to http://example1.com/ @@ -17,7 +17,52 @@ Feature: URL Management When I enter "http://external.com" into the input box as the URL in new url form And I enter "blah" into the input box as the short code in new url form And I submit the new url form - Then I should have 2 Shorted URL with domain name as "hkk.sn" + Then I should have 1 Shorted URL with domain name as "hkk.sn" + And I should have 2 Shorted URL with domain name as "omnia.ws" + And I should have Shorted URL with "hkk.sn/blah" as url and "http://external.com" as target + And I should have Shorted URL with "omnia.ws/blah" as url and "http://external.com" as target When I go to homepage Then I should see "hkk.sn/blah" And I should see "omnia.ws/blah" + + @admin + Scenario: Dashboard Admin shortens URLs for the selected domain by default + Given Our host is "hkk.sn" + And I am an authorized user with credentials as name and password + And there is a shortened URL http://omnia.ws/blah1 that goes to http://example1.com/ + When I go to homepage and provide name and password + And I select omnia.ws in domains dropdown + Then I should see "Custom branded short URLs" + And I should see "Create new short URL" + When I click "Create new short URL" + Then I should see "New URL" + And I should see "hkk.sn" domain checkbox as unchecked + And I should see "omnia.ws" domain checkbox as checked + When I enter "http://external.com" into the input box as the URL in new url form + And I enter "blah" into the input box as the short code in new url form + And I submit the new url form + Then I should have 0 Shorted URL with domain name as "hkk.sn" + And I should have 2 Shorted URL with domain name as "omnia.ws" + And I should have Shorted URL with "omnia.ws/blah" as url and "http://external.com" as target + When I go to homepage + Then I should not see "hkk.sn/blah" + And I should see "omnia.ws/blah" + + @admin + Scenario: Dashboard Admin fails to shortens URLs and is provided proper error messages + Given Our host is "hkk.sn" + And I am an authorized user with credentials as name and password + And there is a shortened URL http://omnia.ws/blah that goes to http://external.com/ + When I go to homepage and provide name and password + Then I should see "Custom branded short URLs" + And I should see "Create new short URL" + When I click "Create new short URL" + Then I should see "New URL" + And I should see "hkk.sn" domain checkbox as checked + And I should see "omnia.ws" domain checkbox as checked + When I enter "http://external.com" into the input box as the URL in new url form + And I enter "blah" into the input box as the short code in new url form + And I submit the new url form + Then I should have 0 Shorted URL with domain name as "hkk.sn" + And I should have 1 Shorted URL with domain name as "omnia.ws" + And I should see "That short code already exists for omnia.ws."