This commit is contained in:
jane 2022-04-09 21:16:01 -04:00
parent e253a71fc4
commit 2696e1f184
33 changed files with 443 additions and 0 deletions

1
.gitignore vendored
View file

@ -35,3 +35,4 @@
/config/master.key
postgres-data/
.env

View file

@ -6,6 +6,8 @@ ruby "3.1.1"
# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.0.2", ">= 7.0.2.3"
# gem "devise"
# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem "sprockets-rails"
@ -27,6 +29,12 @@ gem "stimulus-rails"
# Build JSON APIs with ease [https://github.com/rails/jbuilder]
gem "jbuilder"
gem "omniauth"
gem "omniauth-discord"
# gem "omniauth-rails_csrf_protection"
gem "dotenv-rails"
# Use Redis adapter to run Action Cable in production
# gem "redis", "~> 4.0"

View file

@ -88,9 +88,18 @@ GEM
irb (>= 1.3.6)
reline (>= 0.2.7)
digest (3.1.0)
dotenv (2.7.6)
dotenv-rails (2.7.6)
dotenv (= 2.7.6)
railties (>= 3.2)
erubi (1.10.0)
faraday (2.2.0)
faraday-net_http (~> 2.0)
ruby2_keywords (>= 0.0.4)
faraday-net_http (2.0.1)
globalid (1.0.0)
activesupport (>= 5.0)
hashie (5.0.0)
i18n (1.10.0)
concurrent-ruby (~> 1.0)
importmap-rails (1.0.3)
@ -102,6 +111,7 @@ GEM
jbuilder (2.11.5)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
jwt (2.3.0)
loofah (2.16.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
@ -113,6 +123,8 @@ GEM
mini_mime (1.1.2)
minitest (5.15.0)
msgpack (1.5.1)
multi_json (1.15.0)
multi_xml (0.6.0)
net-imap (0.2.3)
digest
net-protocol
@ -130,11 +142,29 @@ GEM
nio4r (2.5.8)
nokogiri (1.13.3-x86_64-linux)
racc (~> 1.4)
oauth2 (1.4.9)
faraday (>= 0.17.3, < 3.0)
jwt (>= 1.0, < 3.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
omniauth (2.0.4)
hashie (>= 3.4.6)
rack (>= 1.6.2, < 3)
rack-protection
omniauth-discord (1.0.2)
omniauth (~> 2.0.4)
omniauth-oauth2
omniauth-oauth2 (1.7.2)
oauth2 (~> 1.4)
omniauth (>= 1.9, < 3)
public_suffix (4.0.6)
puma (5.6.4)
nio4r (~> 2.0)
racc (1.6.0)
rack (2.2.3)
rack-protection (2.2.0)
rack
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (7.0.2.3)
@ -168,6 +198,7 @@ GEM
reline (0.3.1)
io-console (~> 0.5)
rexml (3.2.5)
ruby2_keywords (0.0.5)
rubyzip (2.3.2)
selenium-webdriver (4.1.0)
childprocess (>= 0.5, < 5.0)
@ -214,8 +245,11 @@ DEPENDENCIES
bootsnap
capybara
debug
dotenv-rails
importmap-rails
jbuilder
omniauth
omniauth-discord
puma (~> 5.0)
rails (~> 7.0.2, >= 7.0.2.3)
selenium-webdriver

View file

@ -0,0 +1,2 @@
class DiscordUsersController < ApplicationController
end

View file

@ -0,0 +1,2 @@
class SessionsController < ApplicationController
end

View file

@ -0,0 +1,70 @@
class UsersController < ApplicationController
before_action :set_user, only: %i[ show edit update destroy ]
# GET /users or /users.json
def index
@users = User.all
end
# GET /users/1 or /users/1.json
def show
end
# GET /users/new
def new
@user = User.new
end
# GET /users/1/edit
def edit
end
# POST /users or /users.json
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to user_url(@user), notice: "User was successfully created." }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1 or /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to user_url(@user), notice: "User was successfully updated." }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1 or /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: "User was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Only allow a list of trusted parameters through.
def user_params
params.require(:user).permit(:provider, :uid, :border)
end
end

View file

@ -0,0 +1,2 @@
module DiscordUsersHelper
end

View file

@ -0,0 +1,2 @@
module SessionsHelper
end

View file

@ -0,0 +1,2 @@
module UsersHelper
end

4
app/models/discord.rb Normal file
View file

@ -0,0 +1,4 @@
class DiscordUser < ApplicationRecord
attr_accessible :discord_id, :username, :discriminator, :avatar
validates :discord_id, :uniqueness => true
end

2
app/models/user.rb Normal file
View file

@ -0,0 +1,2 @@
class User < ApplicationRecord
end

View file

@ -0,0 +1,32 @@
<%= form_with(model: user) do |form| %>
<% if user.errors.any? %>
<div style="color: red">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% user.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form.label :provider, style: "display: block" %>
<%= form.text_field :provider %>
</div>
<div>
<%= form.label :uid, style: "display: block" %>
<%= form.text_field :uid %>
</div>
<div>
<%= form.label :border, style: "display: block" %>
<%= form.text_field :border %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>

View file

@ -0,0 +1,17 @@
<div id="<%= dom_id user %>">
<p>
<strong>Provider:</strong>
<%= user.provider %>
</p>
<p>
<strong>Uid:</strong>
<%= user.uid %>
</p>
<p>
<strong>Border:</strong>
<%= user.border %>
</p>
</div>

View file

@ -0,0 +1,2 @@
json.extract! user, :id, :provider, :uid, :border, :created_at, :updated_at
json.url user_url(user, format: :json)

View file

@ -0,0 +1,10 @@
<h1>Editing user</h1>
<%= render "form", user: @user %>
<br>
<div>
<%= link_to "Show this user", @user %> |
<%= link_to "Back to users", users_path %>
</div>

View file

@ -0,0 +1,14 @@
<p style="color: green"><%= notice %></p>
<h1>Users</h1>
<div id="users">
<% @users.each do |user| %>
<%= render user %>
<p>
<%= link_to "Show this user", user %>
</p>
<% end %>
</div>
<%= link_to "New user", new_user_path %>

View file

@ -0,0 +1 @@
json.array! @users, partial: "users/user", as: :user

View file

@ -0,0 +1,9 @@
<h1>New user</h1>
<%= render "form", user: @user %>
<br>
<div>
<%= link_to "Back to users", users_path %>
</div>

View file

@ -0,0 +1,10 @@
<p style="color: green"><%= notice %></p>
<%= render @user %>
<div>
<%= link_to "Edit this user", edit_user_path(@user) %> |
<%= link_to "Back to users", users_path %>
<%= button_to "Destroy this user", @user, method: :delete %>
</div>

View file

@ -0,0 +1 @@
json.partial! "users/user", user: @user

View file

@ -6,11 +6,15 @@ require "rails/all"
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
Dotenv::Railtie.load
module Borders
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 7.0
config.hosts << "dev.j4.pm"
# Configuration for the application, engines, and railties goes here.
#
# These settings can be overridden in specific environments using the files
@ -18,5 +22,15 @@ module Borders
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
config.session_store :cookie_store, key: '_interslice_session'
config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore, config.session_options
config.middleware.use OmniAuth::Builder do
provider :discord, ENV['DISCORD_CLIENT_ID'], ENV['DISCORD_CLIENT_SECRET'], scope: 'identify'
end
OmniAuth.config.logger = Rails.logger
end
end

View file

@ -1,6 +1,12 @@
Rails.application.routes.draw do
resources :users
resources :discord_users
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
# Defines the root path route ("/")
# root "articles#index"
match "/login" => "sessions#new", :as => :login, via: [:get, :post]
match "/auth/:provider/callback" => "sessions#create", via: [:post]
match "/logout" => "sessions#destroy", :as => :logout, via: [:post]
match "/auth/failure" => "sessions#failure", via: [:get]
end

View file

@ -0,0 +1,12 @@
class CreateDiscordusers < ActiveRecord::Migration[7.0]
def change
create_table :discord_users do |t|
t.string :discord_id
t.string :username
t.string :discriminator
t.string :avatar
t.timestamps
end
end
end

View file

@ -0,0 +1,11 @@
class CreateUsers < ActiveRecord::Migration[7.0]
def change
create_table :users do |t|
t.string :provider
t.string :uid
t.string :border
t.timestamps
end
end
end

30
db/schema.rb generated Normal file
View file

@ -0,0 +1,30 @@
# 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.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_04_09_220708) do
create_table "discord_users", force: :cascade do |t|
t.string "discord_id"
t.string "username"
t.string "discriminator"
t.string "avatar"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "provider"
t.string "uid"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end

View file

@ -0,0 +1,7 @@
require "test_helper"
class DiscordUsersControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

View file

@ -0,0 +1,7 @@
require "test_helper"
class SessionsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

View file

@ -0,0 +1,48 @@
require "test_helper"
class UsersControllerTest < ActionDispatch::IntegrationTest
setup do
@user = users(:one)
end
test "should get index" do
get users_url
assert_response :success
end
test "should get new" do
get new_user_url
assert_response :success
end
test "should create user" do
assert_difference("User.count") do
post users_url, params: { user: { border: @user.border, provider: @user.provider, uid: @user.uid } }
end
assert_redirected_to user_url(User.last)
end
test "should show user" do
get user_url(@user)
assert_response :success
end
test "should get edit" do
get edit_user_url(@user)
assert_response :success
end
test "should update user" do
patch user_url(@user), params: { user: { border: @user.border, provider: @user.provider, uid: @user.uid } }
assert_redirected_to user_url(@user)
end
test "should destroy user" do
assert_difference("User.count", -1) do
delete user_url(@user)
end
assert_redirected_to users_url
end
end

13
test/fixtures/identities.yml vendored Normal file
View file

@ -0,0 +1,13 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
id: MyString
username: MyString
discriminator: MyString
avatar: MyString
two:
id: MyString
username: MyString
discriminator: MyString
avatar: MyString

11
test/fixtures/users.yml vendored Normal file
View file

@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
provider: MyString
uid: MyString
border: MyString
two:
provider: MyString
uid: MyString
border: MyString

View file

@ -0,0 +1,7 @@
require "test_helper"
class IdentityTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

7
test/models/user_test.rb Normal file
View file

@ -0,0 +1,7 @@
require "test_helper"
class UserTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

45
test/system/users_test.rb Normal file
View file

@ -0,0 +1,45 @@
require "application_system_test_case"
class UsersTest < ApplicationSystemTestCase
setup do
@user = users(:one)
end
test "visiting the index" do
visit users_url
assert_selector "h1", text: "Users"
end
test "should create user" do
visit users_url
click_on "New user"
fill_in "Border", with: @user.border
fill_in "Provider", with: @user.provider
fill_in "Uid", with: @user.uid
click_on "Create User"
assert_text "User was successfully created"
click_on "Back"
end
test "should update User" do
visit user_url(@user)
click_on "Edit this user", match: :first
fill_in "Border", with: @user.border
fill_in "Provider", with: @user.provider
fill_in "Uid", with: @user.uid
click_on "Update User"
assert_text "User was successfully updated"
click_on "Back"
end
test "should destroy User" do
visit user_url(@user)
click_on "Destroy this user", match: :first
assert_text "User was successfully destroyed"
end
end