diff --git a/app/controllers/api/group_controller.rb b/app/controllers/api/group_controller.rb index 8f6566f..2bae7be 100644 --- a/app/controllers/api/group_controller.rb +++ b/app/controllers/api/group_controller.rb @@ -14,9 +14,12 @@ def create end group = Group.new(group_params) - group.update( - handle: handle - ) + ActiveRecord::Base.transaction do + group.update( + handle: handle + ) + Domain.create(handle: handle, fullname: "#{handle}.sola.day", item_type: "Group", item_id: group.id) + end Membership.create(profile_id: profile.id, group_id: group.id, role: "owner", status: "active") group.increment!(:memberships_count) diff --git a/app/controllers/api/profile_controller.rb b/app/controllers/api/profile_controller.rb index 6944093..286d863 100644 --- a/app/controllers/api/profile_controller.rb +++ b/app/controllers/api/profile_controller.rb @@ -240,8 +240,10 @@ def create render json: { result: "error", message: "profile handle exists" } return end - - profile.update(handle: handle) + ActiveRecord::Base.transaction do + profile.update(handle: handle) + Domain.create(handle: handle, fullname: "#{handle}.sola.day", item_type: "Profile", item_id: profile.id) + end render json: { result: "ok" } end diff --git a/app/controllers/api/service_controller.rb b/app/controllers/api/service_controller.rb index 39b1f38..c0e1841 100644 --- a/app/controllers/api/service_controller.rb +++ b/app/controllers/api/service_controller.rb @@ -36,8 +36,8 @@ def send_email code = rand(10_000..100_000) token = ProfileToken.create(context: params[:context], sent_to: params[:email], code: code) - mailer = SigninMailer.with(code: code, recipient: params[:email]).signin_email - mailer.deliver_now! + # mailer = SigninMailer.with(code: code, recipient: params[:email]).signin_email + # mailer.deliver_now! render json: { result: "ok", email: params[:email] } end diff --git a/app/models/domain.rb b/app/models/domain.rb new file mode 100644 index 0000000..5cd0fc4 --- /dev/null +++ b/app/models/domain.rb @@ -0,0 +1,2 @@ +class Domain < ApplicationRecord +end diff --git a/db/migrate/20240910055659_create_domains.rb b/db/migrate/20240910055659_create_domains.rb new file mode 100644 index 0000000..3fabc72 --- /dev/null +++ b/db/migrate/20240910055659_create_domains.rb @@ -0,0 +1,11 @@ +class CreateDomains < ActiveRecord::Migration[7.2] + def change + create_table :domains do |t| + t.string :handle + t.string :fullname + t.string :item_type + t.integer :item_id + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index f4b2712..bee507b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2024_08_21_170124) do +ActiveRecord::Schema[7.2].define(version: 2024_09_10_055659) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -123,6 +123,15 @@ t.datetime "updated_at", null: false end + create_table "domains", force: :cascade do |t| + t.string "handle" + t.string "fullname" + t.string "item_type" + t.integer "item_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "event_roles", force: :cascade do |t| t.integer "event_id" t.integer "profile_id" diff --git a/test/controllers/api/group_controller_test.rb b/test/controllers/api/group_controller_test.rb index 6a05026..1fde63f 100644 --- a/test/controllers/api/group_controller_test.rb +++ b/test/controllers/api/group_controller_test.rb @@ -19,9 +19,11 @@ class Api::GroupControllerTest < ActionDispatch::IntegrationTest assert group.active? assert group.is_owner(profile.id) assert group.memberships_count == 1 + assert Domain.find_by(handle: "newworld", item_type: "Group", item_id: group.id).present? end - test "api#group/update" do + test "api#group/update" do # optimize this test function + assert_changes "Group.find_by(handle: 'guildx').timezone" do profile = Profile.find_by(handle: "cookie") auth_token = profile.gen_auth_token group = Group.find_by(handle: "guildx") @@ -31,8 +33,7 @@ class Api::GroupControllerTest < ActionDispatch::IntegrationTest timezone: "asia/hongkong", } } assert_response :success - group = Group.find_by(handle: "guildx") - assert group.timezone == "asia/hongkong" + end end test "api#group/transfer_owner fails for non-member recipient" do diff --git a/test/controllers/api/profile_controller_test.rb b/test/controllers/api/profile_controller_test.rb index a3d77ef..a95c25c 100644 --- a/test/controllers/api/profile_controller_test.rb +++ b/test/controllers/api/profile_controller_test.rb @@ -2,28 +2,25 @@ class Api::ProfileControllerTest < ActionDispatch::IntegrationTest - # test "api#profile/signin_with_email" do - # post api_service_send_email_url, params: { context: "email-signin", email: "example@gmail.com" } - # assert_response :success - # p response.body + test "api#profile/signin_with_email" do + post api_service_send_email_url, params: { context: "email-signin", email: "example@gmail.com" } + assert_response :success - # post api_profile_signin_with_email_url, params: { email: "example@gmail.com", code: ProfileToken.last.code } - # assert_response :success - # p response.body - # auth_token = JSON.parse(response.body)["auth_token"] - # p Profile.find_by(email: "example@gmail.com") + post api_profile_signin_with_email_url, params: { email: "example@gmail.com", code: ProfileToken.last.code } + assert_response :success + auth_token = JSON.parse(response.body)["auth_token"] + assert Profile.find_by(email: "example@gmail.com") - # post api_profile_create_url, params: { auth_token: auth_token, handle: "example" } - # assert_response :success - # p response.body + post api_profile_create_url, params: { auth_token: auth_token, handle: "example" } + assert_response :success + assert Domain.find_by(handle: "example", item_type: "Profile", item_id: Profile.find_by(handle: "example").id).present? - # get api_profile_get_by_email_url, params: { email: "example@gmail.com" } - # assert_response :success - # p response.body + get api_profile_get_by_email_url, params: { email: "example@gmail.com" } + assert_response :success - # get api_profile_me_url, params: { auth_token: auth_token } - # assert_response :success - # end + get api_profile_me_url, params: { auth_token: auth_token } + assert_response :success + end # test "api#profile/set_verified_email" do # post api_service_send_email_url, params: { context: "email-verify", email: "biscuit@gmail.com" } diff --git a/test/controllers/api/voucher_controller_test.rb b/test/controllers/api/voucher_controller_test.rb index 438eceb..5d3259e 100644 --- a/test/controllers/api/voucher_controller_test.rb +++ b/test/controllers/api/voucher_controller_test.rb @@ -1,7 +1,14 @@ require "test_helper" class Api::VoucherControllerTest < ActionDispatch::IntegrationTest - # test "the truth" do - # assert true - # end + # generate test for voucher/create + test "api#voucher/create" do + profile = Profile.find_by(handle: "cookie") + auth_token = profile.gen_auth_token + + post api_voucher_create_url, params: { auth_token: auth_token, voucher: { + code: "VOUCHER123", + description: "Voucher for 123", + } } + end end diff --git a/test/fixtures/domains.yml b/test/fixtures/domains.yml new file mode 100644 index 0000000..1f0df1d --- /dev/null +++ b/test/fixtures/domains.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the "{}" from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +# one: {} +# column: value +# +# two: {} +# column: value diff --git a/test/models/domain_test.rb b/test/models/domain_test.rb new file mode 100644 index 0000000..7b12fc2 --- /dev/null +++ b/test/models/domain_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class DomainTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end