Skip to content

Commit

Permalink
feat: PPT-1470 Add lookup by email endpoint in domains controller
Browse files Browse the repository at this point in the history
  • Loading branch information
naqvis committed Aug 5, 2024
1 parent 34f47dd commit 1d85d8a
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 10 deletions.
85 changes: 83 additions & 2 deletions OPENAPI_DOC.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5477,6 +5477,77 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/PlaceOS__Api__Application__ContentError'
/api/engine/v2/domains/lookup/{email}:
get:
summary: Find the domain name by looking into domain registerd email domains.
tags:
- Domains
operationId: PlaceOS::Api::Domains_lookup
parameters:
- name: email
in: path
description: User email to lookup domain for
example: [email protected]
required: true
schema:
type: string
responses:
200:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/String'
409:
description: Conflict
content:
application/json:
schema:
$ref: '#/components/schemas/PlaceOS__Api__Application__CommonError'
401:
description: Unauthorized
content:
application/json:
schema:
$ref: '#/components/schemas/PlaceOS__Api__Application__CommonError'
403:
description: Forbidden
404:
description: Not Found
content:
application/json:
schema:
$ref: '#/components/schemas/PlaceOS__Api__Application__CommonError'
408:
description: Request Timeout
content:
application/json:
schema:
$ref: '#/components/schemas/PlaceOS__Api__Application__CommonError'
400:
description: Bad Request
content:
application/json:
schema:
$ref: '#/components/schemas/PlaceOS__Api__Application__ParameterError'
422:
description: Unprocessable Entity
content:
application/json:
schema:
$ref: '#/components/schemas/PlaceOS__Api__Application__ParameterError'
406:
description: Not Acceptable
content:
application/json:
schema:
$ref: '#/components/schemas/PlaceOS__Api__Application__ContentError'
415:
description: Unsupported Media Type
content:
application/json:
schema:
$ref: '#/components/schemas/PlaceOS__Api__Application__ContentError'
/api/engine/v2/domains/{id}:
get:
summary: show the selected domain
Expand Down Expand Up @@ -20891,6 +20962,11 @@ components:
additionalProperties:
type: object
nullable: true
email_domains:
type: array
items:
type: string
nullable: true
id:
type: string
nullable: true
Expand Down Expand Up @@ -21836,9 +21912,16 @@ components:
additionalProperties:
type: object
nullable: true
email_domains:
type: array
items:
type: string
nullable: true
id:
type: string
nullable: true
String:
type: string
PlaceOS__Model__Driver:
type: object
properties:
Expand Down Expand Up @@ -22396,8 +22479,6 @@ components:
id:
type: string
nullable: true
String:
type: string
Array_JSON__Any_:
type: array
items:
Expand Down
2 changes: 1 addition & 1 deletion shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ shards:

placeos-models:
git: https://github.com/placeos/models.git
version: 9.54.0
version: 9.55.0

placeos-resource:
git: https://github.com/place-labs/resource.git
Expand Down
14 changes: 14 additions & 0 deletions spec/controllers/domains_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "../helper"

module PlaceOS::Api
describe Domains do
it "Lookup domain via user email" do
authority = Model::Generator.authority("https://www.dev-placeos.com", ["placeos.com", "dev-placeos.com"]).save!
email = URI.encode_www_form("[email protected]")
path = "#{Domains.base_route}lookup/#{email}"
result = client.get(path)
result.status_code.should eq(200)
result.body.strip('"').should eq(authority.domain)
end
end
end
19 changes: 17 additions & 2 deletions src/placeos-rest-api/controllers/domains.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ module PlaceOS::Api
before_action :can_read, only: [:index, :show]
before_action :can_write, only: [:create, :update, :destroy, :remove]

before_action :check_admin, except: [:index, :show]
before_action :check_admin, except: [:index, :show, :lookup]
before_action :check_support, only: [:index, :show]

###############################################################################################

@[AC::Route::Filter(:before_action, except: [:index, :create])]
@[AC::Route::Filter(:before_action, except: [:index, :lookup, :create])]
def find_current_domain(id : String)
Log.context.set(authority_id: id)
# Find will raise a 404 (not found) if there is an error
Expand All @@ -35,6 +35,21 @@ module PlaceOS::Api
paginate_results(elastic, query)
end

# skip authentication for the lookup
skip_action :authorize!, only: :lookup
skip_action :set_user_id, only: :lookup

# Find the domain name by looking into domain registerd email domains.
@[AC::Route::GET("/lookup/:email")]
def lookup(
@[AC::Param::Info(name: "email", description: "User email to lookup domain for", example: "[email protected]")]
email : String
) : String
authority = ::PlaceOS::Model::Authority.find_by_email(email)
raise Error::NotFound.new("No matching domain found") unless authority
authority.domain
end

# show the selected domain
@[AC::Route::GET("/:id")]
def show : ::PlaceOS::Model::Authority
Expand Down
10 changes: 5 additions & 5 deletions test
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ set -eu
# this function is called when Ctrl-C is sent
function trap_ctrlc ()
{
docker-compose down &> /dev/null
docker compose down &> /dev/null
exit 2
}

# initialise trap to call trap_ctrlc function
# when signal 2 (SIGINT) is received
trap "trap_ctrlc" 2

docker-compose pull
docker compose pull

docker-compose build
docker compose build

exit_code="0"

docker-compose run \
docker compose run \
--rm \
test "$@" \
|| exit_code="$?"

docker-compose down &> /dev/null
docker compose down &> /dev/null

exit ${exit_code}

0 comments on commit 1d85d8a

Please sign in to comment.