diff --git a/.gitignore b/.gitignore index 70737010..78d1a466 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ tmp/ node_modules/ *bundle.js npm-debug.log +config/secrets.yml diff --git a/.rspec b/.rspec new file mode 100644 index 00000000..83e16f80 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--require spec_helper diff --git a/Gemfile b/Gemfile index 5d1817a7..6a661460 100644 --- a/Gemfile +++ b/Gemfile @@ -17,6 +17,8 @@ gem 'sass-rails', '~> 5.0' gem 'httparty', '0.13.7' gem 'exception_notification', '~> 4.1' gem 'rails_logger', git: 'git@git.liveramp.net:RailsRepos/rails_logger.git' +gem 'mysql2', '~> 0.4' +gem 'validate_url', '~> 1.0' group :development, :test do gem 'capybara' @@ -27,7 +29,9 @@ group :development, :test do gem 'awesome_print' gem 'passenger', '~> 5.0' end + group :test do + gem 'rspec-rails' gem 'rack_session_access', '~> 0.1.1' gem 'selenium-webdriver' gem 'shoulda' diff --git a/Gemfile.lock b/Gemfile.lock index d7259102..ecb10718 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -66,6 +66,7 @@ GEM ffi (~> 1.0, >= 1.0.11) coderay (1.1.1) concurrent-ruby (1.0.5) + diff-lcs (1.3) erubis (2.7.0) exception_notification (4.2.1) actionmailer (>= 4.0, < 6) @@ -117,6 +118,7 @@ GEM minitest (5.10.1) multi_json (1.12.1) multi_xml (0.5.5) + mysql2 (0.4.4) newrelic_rpm (3.16.2.321) nio4r (1.2.1) nokogiri (1.7.0.1) @@ -162,7 +164,7 @@ GEM method_source rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) - rake (12.0.0) + rake (12.3.1) redis (3.3.2) redis-actionpack (5.0.1) actionpack (>= 4.0, < 6) @@ -181,6 +183,23 @@ GEM redis-store (1.2.0) redis (>= 2.2) request_store (1.3.1) + rspec-core (3.5.4) + rspec-support (~> 3.5.0) + rspec-expectations (3.5.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.5.0) + rspec-mocks (3.5.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.5.0) + rspec-rails (3.5.1) + actionpack (>= 3.0) + activesupport (>= 3.0) + railties (>= 3.0) + rspec-core (~> 3.5.0) + rspec-expectations (~> 3.5.0) + rspec-mocks (~> 3.5.0) + rspec-support (~> 3.5.0) + rspec-support (3.5.0) ruby-saml (1.4.1) nokogiri (>= 1.5.10) rubyzip (1.2.0) @@ -219,6 +238,9 @@ GEM uglifier (2.7.2) execjs (>= 0.3.0) json (>= 1.8.0) + validate_url (1.0.2) + activemodel (>= 3.0.0) + addressable websocket (1.2.3) websocket-driver (0.6.5) websocket-extensions (>= 0.1.0) @@ -243,6 +265,7 @@ DEPENDENCIES jquery-rails (~> 4.1) jquery-ui-rails (~> 5.0) letter_opener + mysql2 (~> 0.4) newrelic_rpm (~> 3.14) passenger (~> 5.0) pry-byebug @@ -251,6 +274,7 @@ DEPENDENCIES rails (= 5.0.1) rails_logger! redis-rails (= 5.0.1) + rspec-rails ruby-saml sass-rails (~> 5.0) select2-rails (= 3.5.9) @@ -258,6 +282,7 @@ DEPENDENCIES shoulda sprockets-rails (~> 2.0) uglifier (= 2.7.2) + validate_url (~> 1.0) BUNDLED WITH - 1.14.6 + 1.16.0 diff --git a/app/models/.keep b/app/models/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/app/models/concerns/.keep b/app/models/concerns/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/app/models/link.rb b/app/models/link.rb new file mode 100644 index 00000000..fbbe0456 --- /dev/null +++ b/app/models/link.rb @@ -0,0 +1,11 @@ +class Link < ActiveRecord::Base + validates :url, url: true + validates :alias, uniqueness: true + before_save :change_alias_underscores_to_dashes + + private + + def change_alias_underscores_to_dashes + self.alias.gsub!("_", "-") + end +end diff --git a/config/application.rb b/config/application.rb index 08a6c195..20474204 100644 --- a/config/application.rb +++ b/config/application.rb @@ -4,6 +4,7 @@ require 'action_mailer/railtie' require 'action_controller/railtie' require 'sprockets/railtie' +require 'rails/all' # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. diff --git a/config/database.yml b/config/database.yml new file mode 100644 index 00000000..c0a21b81 --- /dev/null +++ b/config/database.yml @@ -0,0 +1,28 @@ +production: + database: go_links_db + username: go_links_user + password: "<%= Rails.application.secrets.go_links_user_password %>" + host: apex-dbs.liveramp.net + encoding: utf8 + reconnect: 'true' + adapter: mysql2 + retries: 0 + pool: 5 + reaping_frequency: 60 + wait_timeout: 28800 +development: + adapter: mysql2 + username: root + password: + database: go_links_local_db + host: localhost + encoding: utf8 + reconnect: true +test: + adapter: mysql2 + username: root + password: + host: localhost + database: go_links_test_db + encoding: utf8 + reconnect: true diff --git a/config/secrets.yml b/config/secrets.yml deleted file mode 100644 index bb49dc0d..00000000 --- a/config/secrets.yml +++ /dev/null @@ -1,22 +0,0 @@ -# Be sure to restart your server when you modify this file. - -# Your secret key is used for verifying the integrity of signed cookies. -# If you change this key, all old signed cookies will become invalid! - -# Make sure the secret is at least 30 characters and all random, -# no regular words or you'll be exposed to dictionary attacks. -# You can use `rake secret` to generate a secure secret key. - -# Make sure the secrets in this file are kept private -# if you're sharing your code publicly. - -development: - secret_key_base: 63c8eb30a30e1d4f4224a96da78c4bdb859f37f632962e1515cb7023b8ec2adb6e5063025c9cb4ab3d5cacfefe5ea4250ad5e5cd4fd7a2addfe48f52e2e76553 - -test: - secret_key_base: 6e8747796758022e74d47ea2c9cec5dabec6a51f9f8b0fd21b9518b1c3e473e52be58ffed71d27ae8bd7b974b6af4cec3267c9e59102df1af9892d2a038860b8 - -# Do not keep production secrets in the repository, -# instead read values from the environment. -production: - secret_key_base: 63c8eb30a30e1d4f4224a96da78c4bdb859f37f632962e1515cb7023b8ec2adb6e5063025c9cb4ab3d5cacfefe5ea4250ad5e5cd4fd7a2addfe48f52e2e76553 diff --git a/db/migrate/20180821101102_create_links.rb b/db/migrate/20180821101102_create_links.rb new file mode 100644 index 00000000..31535b90 --- /dev/null +++ b/db/migrate/20180821101102_create_links.rb @@ -0,0 +1,13 @@ +class CreateLinks < ActiveRecord::Migration[5.0] + def change + create_table :links do |t| + t.string :alias, null: false + t.string :url, null: false + t.string :owner, null: false + t.text :description + t.timestamps + + t.index :alias, unique: true + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 00000000..71eac1fd --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,25 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20180821101102) do + + create_table "links", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| + t.string "alias", null: false + t.string "url", null: false + t.string "owner", null: false + t.text "description", limit: 65535 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["alias"], name: "index_links_on_alias", unique: true, using: :btree + end + +end diff --git a/spec/app/models/link_spec.rb b/spec/app/models/link_spec.rb new file mode 100644 index 00000000..9f2ad1d5 --- /dev/null +++ b/spec/app/models/link_spec.rb @@ -0,0 +1,48 @@ +describe Link do + context "validation" do + let(:link) { FactoryGirl.build(:link) } + + it "does not allow invalid urls" do + [ + "foo.net", + "www.foo.com", + "foo.biz", + "", + "a_word", + "http://a space.com", + nil + ].each do |url| + link.url = url + expect(link).not_to be_valid + end + end + + it "allows non-local urls" do + [ + "http://foo.com", + "https://foo.biz", + "http://www.foo.net", + "https://www.foo.com", + ].each do |url| + link.url = url + expect(link).to be_valid + end + end + + it "allows local urls" do + [ + "http://foo", + "https://foo", + ].each do |url| + link.url = url + expect(link).to be_valid + end + end + end + + it "replaces underscores with dashes" do + expect( + FactoryGirl.create(:link, alias: "foo_bar").alias + ).to eq "foo-bar" + end +end diff --git a/spec/factories/links.rb b/spec/factories/links.rb new file mode 100644 index 00000000..b8e445ad --- /dev/null +++ b/spec/factories/links.rb @@ -0,0 +1,9 @@ +FactoryGirl.define do + factory :link do + created_at Time.now + updated_at Time.now + sequence(:owner) { |i| "owner_#{i}" } + sequence(:url) { |i| "http://url_#{i}.com" } + sequence(:alias) { |i| "alias-#{i}" } + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 00000000..0dbbfa5c --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,14 @@ +# This file is copied to spec/ when you run 'rails generate rspec:install' +ENV["RAILS_ENV"] ||= 'test' +require File.expand_path("../../config/environment", __FILE__) +require 'rspec/rails' + +RSpec.configure do |config| + config.expect_with :rspec do |expectations| + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + config.mock_with :rspec do |mocks| + mocks.verify_partial_doubles = true + end +end diff --git a/test/controllers/.keep b/test/controllers/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/test/fixtures/.keep b/test/fixtures/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/test/helpers/.keep b/test/helpers/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/test/integration/.keep b/test/integration/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/test/mailers/.keep b/test/mailers/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/test/models/.keep b/test/models/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/test/test_helper.rb b/test/test_helper.rb deleted file mode 100644 index 92e39b2d..00000000 --- a/test/test_helper.rb +++ /dev/null @@ -1,10 +0,0 @@ -ENV['RAILS_ENV'] ||= 'test' -require File.expand_path('../../config/environment', __FILE__) -require 'rails/test_help' - -class ActiveSupport::TestCase - # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. - fixtures :all - - # Add more helper methods to be used by all tests here... -end