|
| 1 | +// Copyright 2019 Palantir Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package githubapp |
| 16 | + |
| 17 | +import ( |
| 18 | + "os" |
| 19 | + "reflect" |
| 20 | + "testing" |
| 21 | +) |
| 22 | + |
| 23 | +func TestSetValuesFromEnv(t *testing.T) { |
| 24 | + tests := map[string]struct { |
| 25 | + Input func(*Config) |
| 26 | + Prefix string |
| 27 | + Variables map[string]string |
| 28 | + Output func(*Config) |
| 29 | + }{ |
| 30 | + "noVariables": { |
| 31 | + Input: func(c *Config) { |
| 32 | + c.WebURL = "https://github.com" |
| 33 | + c.App.WebhookSecret = "secrethookvalue" |
| 34 | + }, |
| 35 | + Output: func(c *Config) { |
| 36 | + c.WebURL = "https://github.com" |
| 37 | + c.App.WebhookSecret = "secrethookvalue" |
| 38 | + }, |
| 39 | + }, |
| 40 | + "overwriteExisting": { |
| 41 | + Input: func(c *Config) { |
| 42 | + c.WebURL = "https://github.com" |
| 43 | + }, |
| 44 | + Variables: map[string]string{ |
| 45 | + "GITHUB_WEB_URL": "https://github.company.domain", |
| 46 | + }, |
| 47 | + Output: func(c *Config) { |
| 48 | + c.WebURL = "https://github.company.domain" |
| 49 | + }, |
| 50 | + }, |
| 51 | + "allVariables": { |
| 52 | + Variables: map[string]string{ |
| 53 | + "GITHUB_WEB_URL": "https://github.company.domain", |
| 54 | + "GITHUB_V3_API_URL": "https://github.company.domain/api/v3", |
| 55 | + "GITHUB_V4_API_URL": "https://github.company.domain/api/graphql", |
| 56 | + "GITHUB_APP_INTEGRATION_ID": "4", |
| 57 | + "GITHUB_APP_WEBHOOK_SECRET": "secrethookvalue", |
| 58 | + "GITHUB_APP_PRIVATE_KEY": "-----BEGIN RSA PRIVATE KEY-----\nxxx\nxxx\nxxx\n-----END RSA PRIVATE KEY-----", |
| 59 | + "GITHUB_OAUTH_CLIENT_ID": "92faf4b9146f3278", |
| 60 | + "GITHUB_OAUTH_CLIENT_SECRET": "b00f7ea6d59dd5c9578c48f9391e71db", |
| 61 | + }, |
| 62 | + Output: func(c *Config) { |
| 63 | + c.WebURL = "https://github.company.domain" |
| 64 | + c.V3APIURL = "https://github.company.domain/api/v3" |
| 65 | + c.V4APIURL = "https://github.company.domain/api/graphql" |
| 66 | + c.App.IntegrationID = 4 |
| 67 | + c.App.WebhookSecret = "secrethookvalue" |
| 68 | + c.App.PrivateKey = "-----BEGIN RSA PRIVATE KEY-----\nxxx\nxxx\nxxx\n-----END RSA PRIVATE KEY-----" |
| 69 | + c.OAuth.ClientID = "92faf4b9146f3278" |
| 70 | + c.OAuth.ClientSecret = "b00f7ea6d59dd5c9578c48f9391e71db" |
| 71 | + }, |
| 72 | + }, |
| 73 | + "withPrefix": { |
| 74 | + Input: func(c *Config) { |
| 75 | + c.WebURL = "https://github.com" |
| 76 | + }, |
| 77 | + Prefix: "TEST_", |
| 78 | + Variables: map[string]string{ |
| 79 | + "TEST_GITHUB_WEB_URL": "https://github.company.domain", |
| 80 | + }, |
| 81 | + Output: func(c *Config) { |
| 82 | + c.WebURL = "https://github.company.domain" |
| 83 | + }, |
| 84 | + }, |
| 85 | + "emptyValues": { |
| 86 | + Input: func(c *Config) { |
| 87 | + c.WebURL = "https://github.com" |
| 88 | + }, |
| 89 | + Variables: map[string]string{ |
| 90 | + "GITHUB_WEB_URL": "", |
| 91 | + }, |
| 92 | + Output: func(c *Config) { |
| 93 | + c.WebURL = "" |
| 94 | + }, |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + for name, test := range tests { |
| 99 | + t.Run(name, func(t *testing.T) { |
| 100 | + for k, v := range test.Variables { |
| 101 | + if err := os.Setenv(k, v); err != nil { |
| 102 | + t.Fatalf("failed to set environment variable: %s: %v", k, err) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + defer func() { |
| 107 | + for k := range test.Variables { |
| 108 | + if err := os.Unsetenv(k); err != nil { |
| 109 | + t.Fatalf("failed to clear environment variable: %s: %v", k, err) |
| 110 | + } |
| 111 | + } |
| 112 | + }() |
| 113 | + |
| 114 | + var in Config |
| 115 | + if test.Input != nil { |
| 116 | + test.Input(&in) |
| 117 | + } |
| 118 | + |
| 119 | + var out Config |
| 120 | + if test.Output != nil { |
| 121 | + test.Output(&out) |
| 122 | + } |
| 123 | + |
| 124 | + in.SetValuesFromEnv(test.Prefix) |
| 125 | + |
| 126 | + if !reflect.DeepEqual(out, in) { |
| 127 | + t.Errorf("incorrect configuration\nexpected: %+v\n actual: %+v", out, in) |
| 128 | + } |
| 129 | + }) |
| 130 | + } |
| 131 | +} |
0 commit comments