|
| 1 | +/* |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +contributor license agreements. See the NOTICE file distributed with |
| 4 | +this work for additional information regarding copyright ownership. |
| 5 | +The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +(the "License"); you may not use this file except in compliance with |
| 7 | +the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package api |
| 19 | + |
| 20 | +import ( |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + |
| 25 | + "github.com/apache/incubator-devlake/plugins/claude_code/models" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + testOrganization = "anthropic-labs" |
| 30 | + testToken = "sk-ant-example" |
| 31 | +) |
| 32 | + |
| 33 | +func TestValidateConnectionSuccess(t *testing.T) { |
| 34 | + connection := &models.ClaudeCodeConnection{ |
| 35 | + ClaudeCodeConn: models.ClaudeCodeConn{ |
| 36 | + Organization: testOrganization, |
| 37 | + Token: testToken, |
| 38 | + }, |
| 39 | + } |
| 40 | + connection.Normalize() |
| 41 | + |
| 42 | + err := validateConnection(connection) |
| 43 | + assert.NoError(t, err) |
| 44 | +} |
| 45 | + |
| 46 | +func TestValidateConnectionNil(t *testing.T) { |
| 47 | + err := validateConnection(nil) |
| 48 | + assert.Error(t, err) |
| 49 | + assert.Contains(t, err.Error(), "connection is required") |
| 50 | +} |
| 51 | + |
| 52 | +func TestValidateConnectionMissingOrganization(t *testing.T) { |
| 53 | + connection := &models.ClaudeCodeConnection{ |
| 54 | + ClaudeCodeConn: models.ClaudeCodeConn{ |
| 55 | + Token: testToken, |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + err := validateConnection(connection) |
| 60 | + assert.Error(t, err) |
| 61 | + assert.Contains(t, err.Error(), "organization is required") |
| 62 | +} |
| 63 | + |
| 64 | +func TestValidateConnectionMissingToken(t *testing.T) { |
| 65 | + connection := &models.ClaudeCodeConnection{ |
| 66 | + ClaudeCodeConn: models.ClaudeCodeConn{ |
| 67 | + Organization: testOrganization, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + err := validateConnection(connection) |
| 72 | + assert.Error(t, err) |
| 73 | + assert.Contains(t, err.Error(), "either token or at least one custom header is required") |
| 74 | +} |
| 75 | + |
| 76 | +func TestValidateConnectionCustomHeadersWithoutToken(t *testing.T) { |
| 77 | + connection := &models.ClaudeCodeConnection{ |
| 78 | + ClaudeCodeConn: models.ClaudeCodeConn{ |
| 79 | + Organization: testOrganization, |
| 80 | + CustomHeaders: []models.CustomHeader{ |
| 81 | + {Key: "Ocp-Apim-Subscription-Key", Value: "secret-key"}, |
| 82 | + }, |
| 83 | + }, |
| 84 | + } |
| 85 | + connection.Normalize() |
| 86 | + |
| 87 | + err := validateConnection(connection) |
| 88 | + assert.NoError(t, err) |
| 89 | +} |
| 90 | + |
| 91 | +func TestValidateConnectionRejectsBlankCustomHeaders(t *testing.T) { |
| 92 | + connection := &models.ClaudeCodeConnection{ |
| 93 | + ClaudeCodeConn: models.ClaudeCodeConn{ |
| 94 | + Organization: testOrganization, |
| 95 | + CustomHeaders: []models.CustomHeader{ |
| 96 | + {Key: "", Value: ""}, |
| 97 | + }, |
| 98 | + }, |
| 99 | + } |
| 100 | + connection.Normalize() |
| 101 | + |
| 102 | + err := validateConnection(connection) |
| 103 | + assert.Error(t, err) |
| 104 | + assert.Contains(t, err.Error(), "either token or at least one custom header is required") |
| 105 | +} |
| 106 | + |
| 107 | +func TestValidateConnectionRejectsIncompleteCustomHeaders(t *testing.T) { |
| 108 | + connection := &models.ClaudeCodeConnection{ |
| 109 | + ClaudeCodeConn: models.ClaudeCodeConn{ |
| 110 | + Organization: testOrganization, |
| 111 | + CustomHeaders: []models.CustomHeader{ |
| 112 | + {Key: "Ocp-Apim-Subscription-Key", Value: ""}, |
| 113 | + }, |
| 114 | + }, |
| 115 | + } |
| 116 | + connection.Normalize() |
| 117 | + |
| 118 | + err := validateConnection(connection) |
| 119 | + assert.Error(t, err) |
| 120 | + assert.Contains(t, err.Error(), "custom headers must include both key and value") |
| 121 | +} |
| 122 | + |
| 123 | +func TestValidateConnectionInvalidRateLimit(t *testing.T) { |
| 124 | + connection := &models.ClaudeCodeConnection{ |
| 125 | + ClaudeCodeConn: models.ClaudeCodeConn{ |
| 126 | + Organization: testOrganization, |
| 127 | + Token: testToken, |
| 128 | + }, |
| 129 | + } |
| 130 | + connection.RateLimitPerHour = -1 |
| 131 | + |
| 132 | + err := validateConnection(connection) |
| 133 | + assert.Error(t, err) |
| 134 | + assert.Contains(t, err.Error(), "rateLimitPerHour must be non-negative") |
| 135 | +} |
0 commit comments