|
1 | 1 | defmodule Assent.Strategy.GitlabTest do |
2 | | - use Assent.Test.OAuth2TestCase |
| 2 | + use Assent.Test.OIDCTestCase |
3 | 3 |
|
4 | 4 | alias Assent.Strategy.Gitlab |
5 | 5 |
|
6 | | - # From https://docs.gitlab.com/ee/api/users.html#list-current-user-for-normal-users |
7 | | - @user_response %{ |
8 | | - "id" => 1, |
9 | | - "username" => "john_smith", |
10 | | - |
11 | | - "name" => "John Smith", |
12 | | - "state" => "active", |
13 | | - "avatar_url" => "http://localhost:3000/uploads/user/avatar/1/index.jpg", |
14 | | - "web_url" => "http://localhost:3000/john_smith", |
15 | | - "created_at" => "2012-05-23T08:00:58Z", |
16 | | - "bio" => nil, |
17 | | - "location" => nil, |
18 | | - "public_email" => "[email protected]", |
19 | | - "skype" => "", |
20 | | - "linkedin" => "", |
21 | | - "twitter" => "", |
22 | | - "website_url" => "", |
23 | | - "organization" => "", |
24 | | - "last_sign_in_at" => "2012-06-01T11:41:01Z", |
25 | | - "confirmed_at" => "2012-05-23T09:05:22Z", |
26 | | - "theme_id" => 1, |
27 | | - "last_activity_on" => "2012-05-23", |
28 | | - "color_scheme_id" => 2, |
29 | | - "projects_limit" => 100, |
30 | | - "current_sign_in_at" => "2012-06-02T06:36:55Z", |
31 | | - "identities" => [ |
32 | | - %{"provider" => "github", "extern_uid" => "2435223452345"}, |
33 | | - %{"provider" => "bitbucket", "extern_uid" => "john_smith"}, |
34 | | - %{"provider" => "google_oauth2", "extern_uid" => "8776128412476123468721346"} |
35 | | - ], |
36 | | - "can_create_group" => true, |
37 | | - "can_create_project" => true, |
38 | | - "two_factor_enabled" => true, |
39 | | - "external" => false, |
40 | | - "private_profile" => false |
| 6 | + # From running GitLab in local |
| 7 | + @id_token_claims %{ |
| 8 | + "iss" => "http://localhost", |
| 9 | + "sub" => "1", |
| 10 | + "aud" => "4843ae8973e91d7f63baf626a88e221648d8839d0edee5878c9f1535f6930a1a", |
| 11 | + "exp" => :os.system_time(:second) + 60, |
| 12 | + "iat" => :os.system_time(:second), |
| 13 | + "auth_time" => :os.system_time(:second), |
| 14 | + "sub_legacy" => "71404f201852be9e557f9a3d85724711a2a6a09959beaf1450cc4f548a8182bc", |
| 15 | + "name" => "Administrator", |
| 16 | + "nickname" => "root", |
| 17 | + "preferred_username" => "root", |
| 18 | + |
| 19 | + "email_verified" => true, |
| 20 | + "profile" => "http://localhost/root", |
| 21 | + "picture" => |
| 22 | + "https://www.gravatar.com/avatar/261647effda628b0ddac771c741d5165af4590157d740ff427ca89bd2a11b82c?s=80&d=identicon", |
| 23 | + "groups_direct" => [] |
41 | 24 | } |
42 | 25 | @user %{ |
43 | | - |
| 26 | + "name" => "Administrator", |
| 27 | + "preferred_username" => "root", |
| 28 | + "sub" => "1", |
| 29 | + |
44 | 30 | "email_verified" => true, |
45 | | - "name" => "John Smith", |
46 | | - "picture" => "http://localhost:3000/uploads/user/avatar/1/index.jpg", |
47 | | - "preferred_username" => "john_smith", |
48 | | - "sub" => 1 |
| 31 | + "groups_direct" => [], |
| 32 | + "nickname" => "root", |
| 33 | + "picture" => |
| 34 | + "https://www.gravatar.com/avatar/261647effda628b0ddac771c741d5165af4590157d740ff427ca89bd2a11b82c?s=80&d=identicon", |
| 35 | + "profile" => "http://localhost/root", |
| 36 | + "sub_legacy" => "71404f201852be9e557f9a3d85724711a2a6a09959beaf1450cc4f548a8182bc" |
49 | 37 | } |
50 | 38 |
|
51 | 39 | test "authorize_url/2", %{config: config} do |
52 | 40 | assert {:ok, %{url: url}} = Gitlab.authorize_url(config) |
53 | | - assert url =~ "/oauth/authorize?client_id=" |
| 41 | + assert url =~ "/oauth/authorize?client_id=id" |
| 42 | + assert url =~ "scope=openid+email+profile" |
54 | 43 | end |
55 | 44 |
|
56 | 45 | test "callback/2", %{config: config, callback_params: params} do |
57 | | - expect_oauth2_access_token_request([uri: "/oauth/token"], fn _conn, params -> |
58 | | - assert params["client_secret"] == config[:client_secret] |
59 | | - end) |
| 46 | + openid_config = |
| 47 | + Map.put(config[:openid_configuration], "token_endpoint_auth_methods_supported", [ |
| 48 | + "client_secret_post" |
| 49 | + ]) |
| 50 | + |
| 51 | + config = |
| 52 | + Keyword.merge(config, |
| 53 | + openid_configuration: openid_config, |
| 54 | + client_id: "4843ae8973e91d7f63baf626a88e221648d8839d0edee5878c9f1535f6930a1a" |
| 55 | + ) |
60 | 56 |
|
61 | | - expect_oauth2_user_request(@user_response, uri: "/api/v4/user") |
| 57 | + [key | _rest] = expect_oidc_jwks_uri_request() |
| 58 | + expect_oidc_access_token_request(id_token_opts: [claims: @id_token_claims, kid: key["kid"]]) |
62 | 59 |
|
63 | 60 | assert {:ok, %{user: user}} = Gitlab.callback(config, params) |
64 | 61 | assert user == @user |
|
0 commit comments