Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supplementary unit testing #43

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions config.example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
LFS_BUCKET: "***********"
CDN_DOMAIN: "***********"
OBS_REGION: "***********"
OBS_ACCESS_KEY_ID: "***********"
OBS_SECRET_ACCESS_KEY: "***********"
CLIENT_ID: "***********"
CLIENT_SECRET: "***********"
PATH_PREFIX: "***********"
DEFAULT_TOKEN: "***********"
VALIDATE_REGEXP:
OWNER_REGEXP: "^[a-zA-Z]([-_.]?[a-zA-Z0-9]+)*$"
REPONAME_REGEXP: "^[a-zA-Z0-9_.-]{1,189}[a-zA-Z0-9]$"
USERNAME_REGEXP: "^[a-zA-Z]([-_.]?[a-zA-Z0-9]+)*$"
PASSWORD_REGEXP: "^[a-zA-Z0-9!@_#$%^&*()\\-=+,?.,]*$"

16 changes: 0 additions & 16 deletions config.yml.example

This file was deleted.

51 changes: 51 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package config

import "testing"

func TestLoadConfig(t *testing.T) {
type args struct {
path string
cfg *Config
remove bool
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "TestLoadConfig success",
args: args{
path: "../config.example.yml",
cfg: &Config{},
remove: false,
},
wantErr: false,
},
{
name: "TestLoadConfig fail",
args: args{
path: "../config.yml.example",
cfg: &Config{},
remove: false,
},
wantErr: true,
},
{
name: "TestLoadConfig fail and remove LocalFile",
args: args{
path: "../config.yml.example",
cfg: &Config{},
remove: true,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := LoadConfig(tt.args.path, tt.args.cfg, tt.args.remove); (err != nil) != tt.wantErr {
t.Errorf("LoadConfig() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
66 changes: 66 additions & 0 deletions server/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package server

import (
"github.com/metalogical/BigFiles/config"
"testing"
)

func TestInit(t *testing.T) {
tests := []struct {
name string
args config.ValidateConfig
wantErr bool
}{
{
name: "compile owner regexp failed",
args: config.ValidateConfig{
OwnerRegexp: `^[\\-?]$`,
},
wantErr: true,
},
{
name: "compile repo regexp failed",
args: config.ValidateConfig{
OwnerRegexp: `^[a-zA-Z]([-_.]?[a-zA-Z0-9]+)*$`,
RepoNameRegexp: `^[\\-?]$`,
},
wantErr: true,
},
{
name: "compile username regexp failed",
args: config.ValidateConfig{
OwnerRegexp: `^[a-zA-Z]([-_.]?[a-zA-Z0-9]+)*$`,
RepoNameRegexp: `^[a-zA-Z0-9_.-]{1,189}[a-zA-Z0-9]$`,
UsernameRegexp: `^[\\-?]$`,
},
wantErr: true,
},
{
name: "compile password regexp failed",
args: config.ValidateConfig{
OwnerRegexp: `^[a-zA-Z]([-_.]?[a-zA-Z0-9]+)*$`,
RepoNameRegexp: `^[a-zA-Z0-9_.-]{1,189}[a-zA-Z0-9]$`,
UsernameRegexp: `^[a-zA-Z]([-_.]?[a-zA-Z0-9]+)*$`,
PasswordRegexp: `^[\\-?]$`,
},
wantErr: true,
},
{
name: "compile regexp success",
args: config.ValidateConfig{
OwnerRegexp: `^[a-zA-Z]([-_.]?[a-zA-Z0-9]+)*$`,
RepoNameRegexp: `^[a-zA-Z0-9_.-]{1,189}[a-zA-Z0-9]$`,
UsernameRegexp: `^[a-zA-Z]([-_.]?[a-zA-Z0-9]+)*$`,
PasswordRegexp: `^[a-zA-Z0-9!@_#$%^&*()-=+,?.,]*$`,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := Init(tt.args); (err != nil) != tt.wantErr {
t.Errorf("Init() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
57 changes: 57 additions & 0 deletions utils/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package utils

import (
"testing"
)

func TestLoadFromYaml(t *testing.T) {
type ValidateConfig struct {
OwnerRegexp string `json:"OWNER_REGEXP" required:"true"`
RepoNameRegexp string `json:"REPONAME_REGEXP" required:"true"`
UsernameRegexp string `json:"USERNAME_REGEXP" required:"true"`
PasswordRegexp string `json:"PASSWORD_REGEXP" required:"true"`
}
type Config struct {
Prefix string `json:"PATH_PREFIX"`
LfsBucket string `json:"LFS_BUCKET"`
ClientId string `json:"CLIENT_ID"`
ClientSecret string `json:"CLIENT_SECRET"`
CdnDomain string `json:"CDN_DOMAIN"`
ObsRegion string `json:"OBS_REGION"`
ObsAccessKeyId string `json:"OBS_ACCESS_KEY_ID"`
ObsSecretAccessKey string `json:"OBS_SECRET_ACCESS_KEY"`
ValidateConfig ValidateConfig `json:"VALIDATE_REGEXP"`
DefaultToken string `json:"DEFAULT_TOKEN"`
}
type args struct {
path string
cfg Config
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "TestLoadFromYaml success",
args: args{
path: "../config.example.yml",
},
wantErr: false,
},
{
name: "TestLoadFromYaml fail",
args: args{
path: "../config.yml.example",
Zherphy marked this conversation as resolved.
Show resolved Hide resolved
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := LoadFromYaml(tt.args.path, tt.args.cfg); (err != nil) != tt.wantErr {
t.Errorf("LoadFromYaml() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
Loading