Browse Source

Add user model and unit tests

Lou 3 years ago
parent
commit
ef96307d39

+ 8
- 0
app/models/user.rb View File

@@ -0,0 +1,8 @@
1
+class User < ApplicationRecord
2
+  validates :email, uniqueness: true, presence: true, :on => :create
3
+  validates :username, uniqueness: true, presence: true, :on => :create
4
+  validates :email, format:{ with: /\S+@\S+[.]\S+/, message: "Must be a valid email format." }
5
+  validates :password_digest, presence: true
6
+
7
+  has_secure_password
8
+end

+ 5
- 1
config/routes.rb View File

@@ -1,3 +1,7 @@
1 1
 Rails.application.routes.draw do
2
-  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
2
+  namespace :api, defaults: { format: :json }  do
3
+    namespace :v1 do
4
+      resources :users
5
+    end
6
+  end
3 7
 end

+ 13
- 0
db/migrate/20210413093535_create_users.rb View File

@@ -0,0 +1,13 @@
1
+class CreateUsers < ActiveRecord::Migration[6.1]
2
+  def change
3
+    create_table :users do |t|
4
+      t.string :email, null: false
5
+      t.index :email, unique: true
6
+      t.string :username, null: false
7
+      t.index :username, unique: true
8
+      t.string :password_digest
9
+
10
+      t.timestamps
11
+    end
12
+  end
13
+end

+ 25
- 0
db/schema.rb View File

@@ -0,0 +1,25 @@
1
+# This file is auto-generated from the current state of the database. Instead
2
+# of editing this file, please use the migrations feature of Active Record to
3
+# incrementally modify your database, and then regenerate this schema definition.
4
+#
5
+# This file is the source Rails uses to define your schema when running `bin/rails
6
+# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
7
+# be faster and is potentially less error prone than running all of your
8
+# migrations from scratch. Old migrations may fail to apply correctly if those
9
+# migrations use external dependencies or application code.
10
+#
11
+# It's strongly recommended that you check this file into your version control system.
12
+
13
+ActiveRecord::Schema.define(version: 2021_04_13_093535) do
14
+
15
+  create_table "users", force: :cascade do |t|
16
+    t.string "email", null: false
17
+    t.string "username", null: false
18
+    t.string "password_digest"
19
+    t.datetime "created_at", precision: 6, null: false
20
+    t.datetime "updated_at", precision: 6, null: false
21
+    t.index ["email"], name: "index_users_on_email", unique: true
22
+    t.index ["username"], name: "index_users_on_username", unique: true
23
+  end
24
+
25
+end

+ 6
- 0
test/fixtures/users.yml View File

@@ -0,0 +1,6 @@
1
+# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
+
3
+one:
4
+  email: one@one.com
5
+  username: OneUsername
6
+  password_digest: v@lid_pa$$w0rd

+ 25
- 0
test/models/user_test.rb View File

@@ -0,0 +1,25 @@
1
+require "test_helper"
2
+
3
+class UserTest < ActiveSupport::TestCase
4
+  test 'user with a valid email should be valid' do
5
+  user = User.new(email: "test@test.com", username: "UserTest", password_digest: 'password')
6
+  assert user.valid?
7
+  end
8
+
9
+  test 'user with taken username should be invalid' do
10
+  other_user = users(:one)
11
+  user = User.new(email: "test@email.com", username: other_user.username, password_digest: "test_password")
12
+  assert_not user.valid?
13
+  end
14
+
15
+  test 'user with invalid email should be invalid' do
16
+  user = User.new(email: "test_invalid_email", username: "UserTest", password_digest: "test_password")
17
+  assert_not user.valid?
18
+  end
19
+
20
+  test 'user with taken email should be invalid' do
21
+  other_user = users(:one)
22
+  user = User.new(email: other_user.email, username: "UserTest", password_digest: "test_password")
23
+  assert_not user.valid?
24
+  end
25
+end

Loading…
Cancel
Save