|
| 1 | +// Copyright 2019 Google Inc. All Rights Reserved. |
| 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 auth |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "net/http" |
| 20 | + "reflect" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | +) |
| 24 | + |
| 25 | +const samlConfigResponse = `{ |
| 26 | + "name":"projects/mock-project-id/inboundSamlConfigs/saml.provider", |
| 27 | + "idpConfig": { |
| 28 | + "idpEntityId": "IDP_ENTITY_ID", |
| 29 | + "ssoUrl": "https://example.com/login", |
| 30 | + "signRequest": true, |
| 31 | + "idpCertificates": [ |
| 32 | + {"x509Certificate": "CERT1"}, |
| 33 | + {"x509Certificate": "CERT2"} |
| 34 | + ] |
| 35 | + }, |
| 36 | + "spConfig": { |
| 37 | + "spEntityId": "RP_ENTITY_ID", |
| 38 | + "callbackUri": "https://projectId.firebaseapp.com/__/auth/handler" |
| 39 | + }, |
| 40 | + "displayName": "samlProviderName", |
| 41 | + "enabled": true |
| 42 | +}` |
| 43 | +const notFoundResponse = `{ |
| 44 | + "error": { |
| 45 | + "message": "CONFIGURATION_NOT_FOUND" |
| 46 | + } |
| 47 | +}` |
| 48 | + |
| 49 | +var samlProviderConfig = &SAMLProviderConfig{ |
| 50 | + ID: "saml.provider", |
| 51 | + DisplayName: "samlProviderName", |
| 52 | + Enabled: true, |
| 53 | + IDPEntityID: "IDP_ENTITY_ID", |
| 54 | + SSOURL: "https://example.com/login", |
| 55 | + RequestSigningEnabled: true, |
| 56 | + X509Certificates: []string{"CERT1", "CERT2"}, |
| 57 | + RPEntityID: "RP_ENTITY_ID", |
| 58 | + CallbackURL: "https://projectId.firebaseapp.com/__/auth/handler", |
| 59 | +} |
| 60 | + |
| 61 | +var invalidSAMLConfigIDs = []string{ |
| 62 | + "", |
| 63 | + "invalid.id", |
| 64 | + "oidc.config", |
| 65 | +} |
| 66 | + |
| 67 | +func TestSAMLProviderConfig(t *testing.T) { |
| 68 | + s := echoServer([]byte(samlConfigResponse), t) |
| 69 | + defer s.Close() |
| 70 | + |
| 71 | + client := s.Client.pcc |
| 72 | + saml, err := client.SAMLProviderConfig(context.Background(), "saml.provider") |
| 73 | + if err != nil { |
| 74 | + t.Fatal(err) |
| 75 | + } |
| 76 | + |
| 77 | + if !reflect.DeepEqual(saml, samlProviderConfig) { |
| 78 | + t.Errorf("SAMLProviderConfig() = %#v; want = %#v", saml, samlProviderConfig) |
| 79 | + } |
| 80 | + |
| 81 | + req := s.Req[0] |
| 82 | + if req.Method != http.MethodGet { |
| 83 | + t.Errorf("SAMLProviderConfig() Method = %q; want = %q", req.Method, http.MethodGet) |
| 84 | + } |
| 85 | + |
| 86 | + wantURL := "/projects/mock-project-id/inboundSamlConfigs/saml.provider" |
| 87 | + if req.URL.Path != wantURL { |
| 88 | + t.Errorf("SAMLProviderConfig() URL = %q; want = %q", req.URL.Path, wantURL) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestSAMLProviderConfigInvalidID(t *testing.T) { |
| 93 | + client := &providerConfigClient{} |
| 94 | + wantErr := "invalid SAML provider id: " |
| 95 | + |
| 96 | + for _, id := range invalidSAMLConfigIDs { |
| 97 | + saml, err := client.SAMLProviderConfig(context.Background(), id) |
| 98 | + if saml != nil || err == nil || !strings.HasPrefix(err.Error(), wantErr) { |
| 99 | + t.Errorf("SAMLProviderConfig(%q) = (%v, %v); want = (nil, %q)", id, saml, err, wantErr) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestSAMLProviderConfigError(t *testing.T) { |
| 105 | + s := echoServer([]byte(notFoundResponse), t) |
| 106 | + defer s.Close() |
| 107 | + s.Status = http.StatusNotFound |
| 108 | + |
| 109 | + client := s.Client.pcc |
| 110 | + saml, err := client.SAMLProviderConfig(context.Background(), "saml.provider") |
| 111 | + if saml != nil || err == nil || !IsConfigurationNotFound(err) { |
| 112 | + t.Errorf("SAMLProviderConfig() = (%v, %v); want = (nil, ConfigurationNotFound)", saml, err) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestDeleteSAMLProviderConfig(t *testing.T) { |
| 117 | + s := echoServer([]byte("{}"), t) |
| 118 | + defer s.Close() |
| 119 | + |
| 120 | + client := s.Client.pcc |
| 121 | + if err := client.DeleteSAMLProviderConfig(context.Background(), "saml.provider"); err != nil { |
| 122 | + t.Fatal(err) |
| 123 | + } |
| 124 | + |
| 125 | + req := s.Req[0] |
| 126 | + if req.Method != http.MethodDelete { |
| 127 | + t.Errorf("DeleteSAMLProviderConfig() Method = %q; want = %q", req.Method, http.MethodDelete) |
| 128 | + } |
| 129 | + |
| 130 | + wantURL := "/projects/mock-project-id/inboundSamlConfigs/saml.provider" |
| 131 | + if req.URL.Path != wantURL { |
| 132 | + t.Errorf("DeleteSAMLProviderConfig() URL = %q; want = %q", req.URL.Path, wantURL) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func TestDeleteSAMLProviderConfigInvalidID(t *testing.T) { |
| 137 | + client := &providerConfigClient{} |
| 138 | + wantErr := "invalid SAML provider id: " |
| 139 | + |
| 140 | + for _, id := range invalidSAMLConfigIDs { |
| 141 | + err := client.DeleteSAMLProviderConfig(context.Background(), id) |
| 142 | + if err == nil || !strings.HasPrefix(err.Error(), wantErr) { |
| 143 | + t.Errorf("DeleteSAMLProviderConfig(%q) = %v; want = %q", id, err, wantErr) |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func TestDeleteSAMLProviderConfigError(t *testing.T) { |
| 149 | + s := echoServer([]byte(notFoundResponse), t) |
| 150 | + defer s.Close() |
| 151 | + s.Status = http.StatusNotFound |
| 152 | + |
| 153 | + client := s.Client.pcc |
| 154 | + err := client.DeleteSAMLProviderConfig(context.Background(), "saml.provider") |
| 155 | + if err == nil || !IsConfigurationNotFound(err) { |
| 156 | + t.Errorf("DeleteSAMLProviderConfig() = %v; want = ConfigurationNotFound", err) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +func TestSAMLProviderConfigNoProjectID(t *testing.T) { |
| 161 | + client := &providerConfigClient{} |
| 162 | + want := "project id not available" |
| 163 | + if _, err := client.SAMLProviderConfig(context.Background(), "saml.provider"); err == nil || err.Error() != want { |
| 164 | + t.Errorf("SAMLProviderConfig() = %v; want = %q", err, want) |
| 165 | + } |
| 166 | +} |
0 commit comments