|
| 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 models |
| 19 | + |
| 20 | +import ( |
| 21 | + "testing" |
| 22 | + |
| 23 | + helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api" |
| 24 | +) |
| 25 | + |
| 26 | +func TestValidateUserTokenPrefix(t *testing.T) { |
| 27 | + t.Parallel() |
| 28 | + |
| 29 | + serverEndpoint := "https://rad-sonar.example.com/api/" |
| 30 | + cloudEndpoint := "https://sonarcloud.io/api/" |
| 31 | + |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + conn SonarqubeConn |
| 35 | + wantErr bool |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "user token on server", |
| 39 | + conn: SonarqubeConn{ |
| 40 | + RestConnection: helper.RestConnection{Endpoint: serverEndpoint}, |
| 41 | + SonarqubeAccessToken: SonarqubeAccessToken{Token: "squ_abc123"}, |
| 42 | + }, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "global analysis token on server", |
| 46 | + conn: SonarqubeConn{ |
| 47 | + RestConnection: helper.RestConnection{Endpoint: serverEndpoint}, |
| 48 | + SonarqubeAccessToken: SonarqubeAccessToken{Token: "sqa_abc123"}, |
| 49 | + }, |
| 50 | + wantErr: true, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "project analysis token on server", |
| 54 | + conn: SonarqubeConn{ |
| 55 | + RestConnection: helper.RestConnection{Endpoint: serverEndpoint}, |
| 56 | + SonarqubeAccessToken: SonarqubeAccessToken{Token: "sqp_abc123"}, |
| 57 | + }, |
| 58 | + wantErr: true, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "unknown prefix on server", |
| 62 | + conn: SonarqubeConn{ |
| 63 | + RestConnection: helper.RestConnection{Endpoint: serverEndpoint}, |
| 64 | + SonarqubeAccessToken: SonarqubeAccessToken{Token: "legacy-token"}, |
| 65 | + }, |
| 66 | + wantErr: true, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "sonarcloud skips prefix check", |
| 70 | + conn: SonarqubeConn{ |
| 71 | + RestConnection: helper.RestConnection{Endpoint: cloudEndpoint}, |
| 72 | + SonarqubeAccessToken: SonarqubeAccessToken{Token: "sqa_abc123"}, |
| 73 | + }, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + for _, tt := range tests { |
| 78 | + t.Run(tt.name, func(t *testing.T) { |
| 79 | + t.Parallel() |
| 80 | + err := tt.conn.ValidateUserTokenPrefix() |
| 81 | + if tt.wantErr { |
| 82 | + if err == nil { |
| 83 | + t.Fatal("expected error, got nil") |
| 84 | + } |
| 85 | + return |
| 86 | + } |
| 87 | + if err != nil { |
| 88 | + t.Fatalf("unexpected error: %v", err) |
| 89 | + } |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
0 commit comments