diff --git a/app/api/api_routes.rb b/app/api/api_routes.rb index e07fa6b..c027a7f 100644 --- a/app/api/api_routes.rb +++ b/app/api/api_routes.rb @@ -1,3 +1,8 @@ class APIRoutes < Grape::API + use ActionDispatch::Session::CookieStore, key: '_sbhacks_session' format :json + + helpers APIHelpers + + mount API::Auth end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..24262df --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,10 @@ +class User < ApplicationRecord + has_secure_password + + validates :name, presence: true + validates :email, format: { + with: URI::MailTo::EMAIL_REGEXP, + message: 'is not a valid email address' + } + validates :password, length: { in: 8..72 } +end diff --git a/db/migrate/20200824035326_create_users.rb b/db/migrate/20200824035326_create_users.rb new file mode 100644 index 0000000..5987b7c --- /dev/null +++ b/db/migrate/20200824035326_create_users.rb @@ -0,0 +1,11 @@ +class CreateUsers < ActiveRecord::Migration[6.0] + def change + create_table :users do |t| + t.string :name, nil: false + t.string :email, index: { unique: true }, nil: false + t.string :password_digest, nil: false + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index b10373b..147138a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,9 +10,18 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 0) do +ActiveRecord::Schema.define(version: 2020_08_24_035326) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "users", force: :cascade do |t| + t.string "name" + t.string "email" + t.string "password_digest" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["email"], name: "index_users_on_email", unique: true + end + end diff --git a/db/seeds.rb b/db/seeds.rb index 1beea2a..4468e03 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,7 +1,5 @@ -# This file should contain all the record creation needed to seed the database with its default values. -# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup). -# -# Examples: -# -# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) -# Character.create(name: 'Luke', movie: movies.first) +User.create!( + name: 'SBHacks Admin', + email: 'development@sbhacks.com', + password: 'password' +) diff --git a/test/factories/user.rb b/test/factories/user.rb new file mode 100644 index 0000000..7528474 --- /dev/null +++ b/test/factories/user.rb @@ -0,0 +1,9 @@ +FactoryBot.define do + factory :user do + name { 'Akshay Heda' } + sequence :email do |n| + "akshay.#{n}@example.com" + end + password { 'ilovesbhacks' } + end +end diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 0000000..fd81ae9 --- /dev/null +++ b/test/models/user_test.rb @@ -0,0 +1,31 @@ +require 'test_helper' + +class UserTest < ActiveSupport::TestCase + test 'factory is valid' do + assert_predicate build(:user), :valid? + end + + test 'requires a name' do + user = build(:user) + user.name = nil + assert_not_predicate user, :valid? + + user.name = '' + assert_not_predicate user, :valid? + end + + test 'requires a valid email address' do + user = build(:user) + user.email = 'not an email' + assert_not_predicate user, :valid? + end + + test 'requires a password' do + user = build(:user) + user.password = nil + assert_not_predicate user, :valid? + + user.password = 'short' + assert_not_predicate user, :valid? + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 73a23b4..8bf52e4 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -4,6 +4,8 @@ module ActiveSupport class TestCase + include GrapeRouteHelpers::NamedRouteMatcher + include FactoryBot::Syntax::Methods # Run tests in parallel with specified workers parallelize(workers: :number_of_processors)