Skip to content

Commit

Permalink
Add basic profile info endpoint and info
Browse files Browse the repository at this point in the history
  • Loading branch information
renatolond committed Sep 24, 2024
1 parent 3dafaa2 commit 047afa0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
8 changes: 7 additions & 1 deletion app/models/basic_profile_info.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
BasicProfileInfo = Data.define() do
# frozen_string_literal: true

BasicProfileInfo = Data.define(:display_name, :created_at) do
def initialize(display_name:, created_at:)
created_at = DateTime.parse(created_at) if created_at.is_a?(String)
super
end
end
10 changes: 8 additions & 2 deletions app/objects/retro_meet_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,20 @@ def base_headers
@base_headers ||= self.class.send(:base_headers).merge(authorization: @authorization_header)
end

# Calls the profile info endpoint in retromeet-core and returns the response as a ruby object
#
# @raise [UnauthorizedError] If it has a bad login
# @raise [UnknownError] If an unknown error happens
# @return [BasicProfileInfo]
def basic_profile_info
return nil if @authorization_header.blank?

Sync do
response = client.get("/api/profile_info", headers: base_headers)
response = client.get("/api/profile/info", headers: base_headers)
case response.status
when 200
BasicProfileInfo.new()
response_body = JSON.parse(response.read, symbolize_names: true)
BasicProfileInfo.new(response_body[:display_name], response_body[:created_at])
when 401
raise UnauthorizedError, "Not logged in"
else
Expand Down
2 changes: 2 additions & 0 deletions app/views/application/_header.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
.navbar-item= link_to image_tag("retromeet_long.png", class: %i[image]), root_url
.navbar-menu
.navbar-end
- if @basic_profile_info.present?
.navbar-item Welcome, #{@basic_profile_info.display_name}!
.navbar-item Profile
.navbar-item Settings
- if @basic_profile_info.present?
Expand Down
9 changes: 9 additions & 0 deletions test/objects/retro_meet_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,13 @@ class RetroMeetClientTest < ActiveSupport::TestCase
end
assert_requested(:post, "http://localhost:3000/create-account")
end

test "calls the profile info method with a good authorization header and gets the profile info" do
authorization_header = "good_header"
stub_request(:get, "http://localhost:3000/api/profile/info").with(headers: { "Content-Type" => "application/json", "Authorization" => authorization_header })
.to_return(webfixture_json_file("profile_info_good"))
expected_response = BasicProfileInfo.new("bob tables", DateTime.new(2024, 9, 23, 15, 45, 24, "+02:00"))
assert_equal expected_response, RetroMeetClient.new(authorization_header).basic_profile_info
assert_requested(:get, "http://localhost:3000/api/profile/info")
end
end
5 changes: 5 additions & 0 deletions test/webfixtures/profile_info_good.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
HTTP/1.1 200 OK
content-type: application/json
vary: accept-encoding

{"display_name":"bob tables","created_at":"2024-09-23T15:45:24+02:00"}

0 comments on commit 047afa0

Please sign in to comment.