|  | /* | 
|  | Copyright 2017 Google Inc. | 
|  |  | 
|  | Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | you may not use this file except in compliance with the License. | 
|  | You may obtain a copy of the License at | 
|  |  | 
|  | http://www.apache.org/licenses/LICENSE-2.0 | 
|  |  | 
|  | Unless required by applicable law or agreed to in writing, software | 
|  | distributed under the License is distributed on an "AS IS" BASIS, | 
|  | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | See the License for the specific language governing permissions and | 
|  | limitations under the License. | 
|  | */ | 
|  |  | 
|  | package adapter | 
|  |  | 
|  | import ( | 
|  | "testing" | 
|  |  | 
|  | "github.com/apid/istioApigeeAdapter/adapter/config" | 
|  | "github.com/apid/istioApigeeAdapter/mock" | 
|  | ) | 
|  |  | 
|  | func TestMissingKey(t *testing.T) { | 
|  | cfg := &config.VerifyKeyParams{ | 
|  | Organization:    "foo", | 
|  | Environment:     "test", | 
|  | VerificationURL: "http://" + mockServer.Address(), | 
|  | } | 
|  |  | 
|  | builder := newKeyAttrsBuilder() | 
|  | ce := builder.ValidateConfig(cfg) | 
|  | if ce != nil { | 
|  | t.Fatalf("Error validating config: %s", ce) | 
|  | } | 
|  |  | 
|  | ag, err := builder.BuildAttributesGenerator(mockEnv, cfg) | 
|  | if err != nil { | 
|  | t.Fatalf("Error creating aspect: %s", err) | 
|  | } | 
|  | defer ag.Close() | 
|  |  | 
|  | in := make(map[string]interface{}) | 
|  |  | 
|  | out, err := ag.Generate(in) | 
|  | if err != nil { | 
|  | t.Fatalf("Error checking parameters: %s", err) | 
|  | } | 
|  | if out[successParam] != false { | 
|  | t.Fatal("Expected success to be false") | 
|  | } | 
|  | if out[successStringParam] != "false" { | 
|  | t.Fatal("Expected success string to be false") | 
|  | } | 
|  | } | 
|  |  | 
|  | func TestEmptyKey(t *testing.T) { | 
|  | cfg := &config.VerifyKeyParams{ | 
|  | Organization:    "foo", | 
|  | Environment:     "test", | 
|  | VerificationURL: "http://" + mockServer.Address(), | 
|  | } | 
|  |  | 
|  | builder := newKeyAttrsBuilder() | 
|  | ce := builder.ValidateConfig(cfg) | 
|  | if ce != nil { | 
|  | t.Fatalf("Error validating config: %s", ce) | 
|  | } | 
|  |  | 
|  | ag, err := builder.BuildAttributesGenerator(mockEnv, cfg) | 
|  | if err != nil { | 
|  | t.Fatalf("Error creating aspect: %s", err) | 
|  | } | 
|  | defer ag.Close() | 
|  |  | 
|  | in := make(map[string]interface{}) | 
|  | in[keyParam] = "99999" | 
|  | in[pathParam] = "/" | 
|  |  | 
|  | out, err := ag.Generate(in) | 
|  | if err != nil { | 
|  | t.Fatalf("Error checking parameters: %s", err) | 
|  | } | 
|  | if out[successParam] != false { | 
|  | t.Fatal("Expected success to be false") | 
|  | } | 
|  | if out[successStringParam] != "false" { | 
|  | t.Fatal("Expected success string to be false") | 
|  | } | 
|  | } | 
|  |  | 
|  | func TestInvalidKeyAttributes(t *testing.T) { | 
|  | cfg := &config.VerifyKeyParams{ | 
|  | Organization:    "foo", | 
|  | Environment:     "test", | 
|  | VerificationURL: "http://" + mockServer.Address(), | 
|  | } | 
|  |  | 
|  | builder := newKeyAttrsBuilder() | 
|  | ce := builder.ValidateConfig(cfg) | 
|  | if ce != nil { | 
|  | t.Fatalf("Error validating config: %s", ce) | 
|  | } | 
|  |  | 
|  | ag, err := builder.BuildAttributesGenerator(mockEnv, cfg) | 
|  | if err != nil { | 
|  | t.Fatalf("Error creating aspect: %s", err) | 
|  | } | 
|  | defer ag.Close() | 
|  |  | 
|  | in := make(map[string]interface{}) | 
|  | in[keyParam] = "99999" | 
|  | in[pathParam] = "/" | 
|  |  | 
|  | out, err := ag.Generate(in) | 
|  | if err != nil { | 
|  | t.Fatalf("Error checking parameters: %s", err) | 
|  | } | 
|  | if out[successParam] != false { | 
|  | t.Fatal("Expected success to be false") | 
|  | } | 
|  | if out[successStringParam] != "false" { | 
|  | t.Fatal("Expected success string to be false") | 
|  | } | 
|  | } | 
|  |  | 
|  | func TestValidKeyAttributes(t *testing.T) { | 
|  | cfg := &config.VerifyKeyParams{ | 
|  | Organization:    "foo", | 
|  | Environment:     "test", | 
|  | VerificationURL: "http://" + mockServer.Address(), | 
|  | } | 
|  |  | 
|  | builder := newKeyAttrsBuilder() | 
|  | ce := builder.ValidateConfig(cfg) | 
|  | if ce != nil { | 
|  | t.Fatalf("Error validating config: %s", ce) | 
|  | } | 
|  |  | 
|  | ag, err := builder.BuildAttributesGenerator(mockEnv, cfg) | 
|  | if err != nil { | 
|  | t.Fatalf("Error creating aspect: %s", err) | 
|  | } | 
|  | defer ag.Close() | 
|  |  | 
|  | in := make(map[string]interface{}) | 
|  | in[keyParam] = mock.ValidAPIKey1 | 
|  | in[pathParam] = "/" | 
|  |  | 
|  | out, err := ag.Generate(in) | 
|  | if err != nil { | 
|  | t.Fatalf("Error checking parameters: %s", err) | 
|  | } | 
|  | if out[successParam] != true { | 
|  | t.Fatal("Expected success to be true") | 
|  | } | 
|  | if out[successStringParam] != "true" { | 
|  | t.Fatal("Expected success string to be true") | 
|  | } | 
|  | if out[clientIDParam] != mock.ValidAPIKey1 { | 
|  | t.Fatalf("Invalid client ID %s", out[clientIDParam]) | 
|  | } | 
|  | if out[appNameParam] != "TestApp1" { | 
|  | t.Fatalf("Invalid application name %s", out[appNameParam]) | 
|  | } | 
|  | if out[productNameParam] != "TestProduct1" { | 
|  | t.Fatalf("Invalid API product name %s", out[productNameParam]) | 
|  | } | 
|  | } | 
|  |  | 
|  | func TestValidKeyNoProducts(t *testing.T) { | 
|  | cfg := &config.VerifyKeyParams{ | 
|  | Organization:    "foo", | 
|  | Environment:     "test", | 
|  | VerificationURL: "http://" + mockServer.Address(), | 
|  | } | 
|  |  | 
|  | builder := newKeyAttrsBuilder() | 
|  | ce := builder.ValidateConfig(cfg) | 
|  | if ce != nil { | 
|  | t.Fatalf("Error validating config: %s", ce) | 
|  | } | 
|  |  | 
|  | ag, err := builder.BuildAttributesGenerator(mockEnv, cfg) | 
|  | if err != nil { | 
|  | t.Fatalf("Error creating aspect: %s", err) | 
|  | } | 
|  | defer ag.Close() | 
|  |  | 
|  | in := make(map[string]interface{}) | 
|  | in[keyParam] = mock.ValidAPIKeyNoProducts | 
|  | in[pathParam] = "/" | 
|  |  | 
|  | out, err := ag.Generate(in) | 
|  | if err != nil { | 
|  | t.Fatalf("Error checking parameters: %s", err) | 
|  | } | 
|  | if out[successParam] != false { | 
|  | t.Fatal("Expected success to be false") | 
|  | } | 
|  | if out[successStringParam] != "false" { | 
|  | t.Fatal("Expected success string to be false") | 
|  | } | 
|  | if out[clientIDParam] != mock.ValidAPIKeyNoProducts { | 
|  | t.Fatalf("Invalid client ID %s", out[clientIDParam]) | 
|  | } | 
|  | if out[appNameParam] != "TestApp2" { | 
|  | t.Fatalf("Invalid application name %s", out[appNameParam]) | 
|  | } | 
|  | } |