|
| 1 | +defmodule Guard.GitProviderCredentialsTest do |
| 2 | + use Guard.RepoCase, async: false |
| 3 | + alias Guard.InstanceConfig.Store |
| 4 | + |
| 5 | + setup do |
| 6 | + Application.put_env(:guard, :include_instance_config, true) |
| 7 | + Guard.Mocks.GithubAppApi.github_app_api() |
| 8 | + Guard.InstanceConfigRepo.delete_all(Guard.InstanceConfig.Models.Config) |
| 9 | + Cachex.clear(:config_cache) |
| 10 | + |
| 11 | + :ok |
| 12 | + end |
| 13 | + |
| 14 | + describe "get/1" do |
| 15 | + test "should retrieve valid credentials from each provider when instance config is configured" do |
| 16 | + setup_github_app_integration() |
| 17 | + setup_gitlab_app_integration() |
| 18 | + setup_bitbucket_app_integration() |
| 19 | + |
| 20 | + assert {:ok, {"client_id", "client_secret"}} == Guard.GitProviderCredentials.get(:github) |
| 21 | + assert {:ok, {"client_id", "client_secret"}} == Guard.GitProviderCredentials.get(:gitlab) |
| 22 | + assert {:ok, {"client_id", "client_secret"}} == Guard.GitProviderCredentials.get(:bitbucket) |
| 23 | + end |
| 24 | + |
| 25 | + test "should return error when provider is not configured" do |
| 26 | + assert {:error, :not_found} == Guard.GitProviderCredentials.get(:github) |
| 27 | + assert {:error, :not_found} == Guard.GitProviderCredentials.get(:gitlab) |
| 28 | + assert {:error, :not_found} == Guard.GitProviderCredentials.get(:bitbucket) |
| 29 | + end |
| 30 | + |
| 31 | + test "should refetch from instance config when cache is wrong" do |
| 32 | + setup_github_app_integration() |
| 33 | + setup_gitlab_app_integration() |
| 34 | + setup_bitbucket_app_integration() |
| 35 | + |
| 36 | + Cachex.put(:config_cache, "github_credentials", %{client_id: nil, client_secret: nil}) |
| 37 | + Cachex.put(:config_cache, "gitlab_credentials", %{client_id: nil, client_secret: nil}) |
| 38 | + Cachex.put(:config_cache, "bitbucket_credentials", %{client_id: nil, client_secret: nil}) |
| 39 | + |
| 40 | + assert {:ok, {"client_id", "client_secret"}} == Guard.GitProviderCredentials.get(:github) |
| 41 | + assert {:ok, {"client_id", "client_secret"}} == Guard.GitProviderCredentials.get(:gitlab) |
| 42 | + assert {:ok, {"client_id", "client_secret"}} == Guard.GitProviderCredentials.get(:bitbucket) |
| 43 | + end |
| 44 | + |
| 45 | + test "should get credentials from cache if it contains valid credentials" do |
| 46 | + Cachex.put(:config_cache, "github_credentials", %{ |
| 47 | + client_id: "cached_client_id", |
| 48 | + client_secret: "cached_secret" |
| 49 | + }) |
| 50 | + |
| 51 | + Cachex.put(:config_cache, "gitlab_credentials", %{ |
| 52 | + client_id: "cached_client_id", |
| 53 | + client_secret: "cached_secret" |
| 54 | + }) |
| 55 | + |
| 56 | + Cachex.put(:config_cache, "bitbucket_credentials", %{ |
| 57 | + client_id: "cached_client_id", |
| 58 | + client_secret: "cached_secret" |
| 59 | + }) |
| 60 | + |
| 61 | + assert {:ok, {"cached_client_id", "cached_secret"}} == |
| 62 | + Guard.GitProviderCredentials.get(:github) |
| 63 | + |
| 64 | + assert {:ok, {"cached_client_id", "cached_secret"}} == |
| 65 | + Guard.GitProviderCredentials.get(:gitlab) |
| 66 | + |
| 67 | + assert {:ok, {"cached_client_id", "cached_secret"}} == |
| 68 | + Guard.GitProviderCredentials.get(:bitbucket) |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + defp setup_github_app_integration do |
| 73 | + private_key = JOSE.JWK.generate_key({:rsa, 1024}) |
| 74 | + {_, pem_private_key} = JOSE.JWK.to_pem(private_key) |
| 75 | + |
| 76 | + Guard.InstanceConfig.Models.Config.changeset(%Guard.InstanceConfig.Models.Config{}, %{ |
| 77 | + name: :CONFIG_TYPE_GITHUB_APP |> Atom.to_string(), |
| 78 | + config: %{ |
| 79 | + app_id: "11111111111111111111111", |
| 80 | + slug: "slug", |
| 81 | + name: "name", |
| 82 | + client_id: "client_id", |
| 83 | + client_secret: "client_secret", |
| 84 | + pem: pem_private_key, |
| 85 | + html_url: "https://github.com", |
| 86 | + webhook_secret: "webhook_secret" |
| 87 | + } |
| 88 | + }) |
| 89 | + |> Store.set() |
| 90 | + end |
| 91 | + |
| 92 | + defp setup_gitlab_app_integration do |
| 93 | + Guard.InstanceConfig.Models.Config.changeset(%Guard.InstanceConfig.Models.Config{}, %{ |
| 94 | + name: :CONFIG_TYPE_GITLAB_APP |> Atom.to_string(), |
| 95 | + config: %{ |
| 96 | + client_id: "client_id", |
| 97 | + client_secret: "client_secret" |
| 98 | + } |
| 99 | + }) |
| 100 | + |> Store.set() |
| 101 | + end |
| 102 | + |
| 103 | + defp setup_bitbucket_app_integration do |
| 104 | + Guard.InstanceConfig.Models.Config.changeset(%Guard.InstanceConfig.Models.Config{}, %{ |
| 105 | + name: :CONFIG_TYPE_BITBUCKET_APP |> Atom.to_string(), |
| 106 | + config: %{ |
| 107 | + client_id: "client_id", |
| 108 | + client_secret: "client_secret" |
| 109 | + } |
| 110 | + }) |
| 111 | + |> Store.set() |
| 112 | + end |
| 113 | +end |
0 commit comments