|
| 1 | +require "webmock" |
| 2 | +require "../helper" |
| 3 | + |
| 4 | +# Verifies that successful logins publish a `{user_id, provider}` event |
| 5 | +# through `LoginEvents`. We don't go through real Redis here — we swap |
| 6 | +# `LoginEvents.publisher` for a recording Proc. This keeps the spec |
| 7 | +# independent of the Redis stub in `docker-compose.yml` and proves the |
| 8 | +# wire-up (which is the part that can rot under refactors); the actual |
| 9 | +# `redis` PUBLISH call is a single line covered by the shard's own |
| 10 | +# spec suite. |
| 11 | +module PlaceOS::Auth |
| 12 | + describe LoginEvents do |
| 13 | + # Save/restore the publisher on every example so a failure half-way |
| 14 | + # through doesn't leak a recording stub into the next test. |
| 15 | + original_publisher = LoginEvents.publisher |
| 16 | + ::Spec.before_each { LoginEvents.publisher = original_publisher } |
| 17 | + ::Spec.after_each { LoginEvents.publisher = original_publisher } |
| 18 | + |
| 19 | + describe "publish" do |
| 20 | + it "fires `{user_id, internal}` after a successful POST /auth/signin" do |
| 21 | + calls = [] of Tuple(String, String) |
| 22 | + LoginEvents.publisher = ->(uid : String, provider : String) { |
| 23 | + calls << {uid, provider} |
| 24 | + nil |
| 25 | + } |
| 26 | + |
| 27 | + authority = ::PlaceOS::Model::Authority.find_by_domain("localhost").not_nil! |
| 28 | + password = "ok-password-1234" |
| 29 | + user = ::PlaceOS::Model::Generator.user(authority) |
| 30 | + user.password = password |
| 31 | + user.save! |
| 32 | + |
| 33 | + body = {email: user.email.to_s, password: password}.to_json |
| 34 | + headers = HTTP::Headers{ |
| 35 | + "Host" => "localhost", |
| 36 | + "Content-Type" => "application/json", |
| 37 | + } |
| 38 | + result = client.post("/auth/signin", headers: headers, body: body) |
| 39 | + result.status_code.should eq 202 |
| 40 | + |
| 41 | + calls.size.should eq 1 |
| 42 | + calls.first[0].should eq user.id |
| 43 | + calls.first[1].should eq "internal" |
| 44 | + ensure |
| 45 | + user.try &.destroy |
| 46 | + end |
| 47 | + |
| 48 | + it "fires `{user_id, oauth2}` after a successful OAuth callback" do |
| 49 | + calls = [] of Tuple(String, String) |
| 50 | + LoginEvents.publisher = ->(uid : String, provider : String) { |
| 51 | + calls << {uid, provider} |
| 52 | + nil |
| 53 | + } |
| 54 | + |
| 55 | + authority = ::PlaceOS::Model::Authority.find_by_domain("localhost").not_nil! |
| 56 | + strat = ::PlaceOS::Model::OAuthAuthentication.new |
| 57 | + strat.name = "login-event-#{Random.rand(99999)}" |
| 58 | + strat.client_id = "test" |
| 59 | + strat.client_secret = "test" |
| 60 | + strat.site = "https://idp.example.test" |
| 61 | + strat.authorize_url = "/authorize" |
| 62 | + strat.token_url = "/token" |
| 63 | + strat.auth_scheme = "request_body" |
| 64 | + strat.token_method = "post" |
| 65 | + strat.scope = "openid email" |
| 66 | + strat.raw_info_url = "https://idp.example.test/userinfo" |
| 67 | + strat.info_mappings = {"uid" => "id", "email" => "email", "name" => "name"} |
| 68 | + strat.authority_id = authority.id |
| 69 | + strat.save! |
| 70 | + |
| 71 | + WebMock.reset |
| 72 | + WebMock.allow_net_connect = false |
| 73 | + WebMock.stub(:post, "https://idp.example.test/token").to_return( |
| 74 | + status: 200, |
| 75 | + headers: HTTP::Headers{"Content-Type" => "application/json"}, |
| 76 | + body: {access_token: "x", token_type: "Bearer", expires_in: 3600}.to_json, |
| 77 | + ) |
| 78 | + WebMock.stub(:get, "https://idp.example.test/userinfo").to_return( |
| 79 | + status: 200, |
| 80 | + headers: HTTP::Headers{"Content-Type" => "application/json"}, |
| 81 | + body: {id: "uid-#{Random.rand(99999)}", email: "bob-#{Random.rand(99999)}@localhost", name: "Bob"}.to_json, |
| 82 | + ) |
| 83 | + |
| 84 | + kickoff = client.get( |
| 85 | + "/auth/oauth2?id=#{URI.encode_www_form(strat.id.as(String))}", |
| 86 | + headers: HTTP::Headers{"Host" => "localhost"}, |
| 87 | + ) |
| 88 | + kickoff.status_code.should eq 303 |
| 89 | + cookie = kickoff.headers["Set-Cookie"].split(';', 2).first.strip |
| 90 | + state = URI::Params.parse(kickoff.headers["Location"].split('?', 2).last)["state"] |
| 91 | + |
| 92 | + callback = client.get( |
| 93 | + "/auth/oauth2/callback?id=#{URI.encode_www_form(strat.id.as(String))}&code=test-code&state=#{state}", |
| 94 | + headers: HTTP::Headers{"Host" => "localhost", "Cookie" => cookie}, |
| 95 | + ) |
| 96 | + callback.status_code.should eq 303 |
| 97 | + |
| 98 | + calls.size.should eq 1 |
| 99 | + calls.first[1].should eq "oauth2" |
| 100 | + ensure |
| 101 | + strat.try &.destroy |
| 102 | + WebMock.reset |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | +end |
0 commit comments