-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add AllowList to config * Rename isPackageExcluded to be generic * Not ! in list * Add tests and fix issues * Use allow package in server * Not rename method * Remove lineshift * Add allow list in config example * Fix typo server_handler.go * Fix typo
- Loading branch information
1 parent
8b0300b
commit 4c8e077
Showing
4 changed files
with
243 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,168 @@ import ( | |
"testing" | ||
) | ||
|
||
func TestAllowListAndBanList_IsPackageNotAllowedOrBanned(t *testing.T) { | ||
type args struct { | ||
fullName string | ||
} | ||
tests := []struct { | ||
name string | ||
allowList AllowList | ||
banList BanList | ||
args args | ||
want bool | ||
}{ | ||
{ | ||
name: "NoAllowOrBanListAllowAnything", | ||
allowList: AllowList{}, | ||
banList: BanList{}, | ||
args: args{fullName: "[email protected]"}, | ||
want: false, | ||
}, | ||
{ | ||
name: "AllowedScopeBannedScope", | ||
allowList: AllowList{ | ||
Scopes: []AllowScope{{ | ||
Name: "@github", | ||
}}, | ||
}, | ||
banList: BanList{ | ||
Scopes: []BanScope{{ | ||
Name: "@github", | ||
}}, | ||
}, | ||
args: args{fullName: "@github/faker"}, | ||
want: true, | ||
}, | ||
{ | ||
name: "AllowedScopeBannedPackage", | ||
allowList: AllowList{ | ||
Scopes: []AllowScope{{ | ||
Name: "@github", | ||
}}, | ||
}, | ||
banList: BanList{ | ||
Packages: []string{"@github/faker"}, | ||
}, | ||
args: args{fullName: "@github/faker"}, | ||
want: true, | ||
}, | ||
{ | ||
name: "AllowedPackageBannedPackage", | ||
allowList: AllowList{ | ||
Packages: []string{"@github/faker"}, | ||
}, | ||
banList: BanList{ | ||
Packages: []string{"faker"}, | ||
}, | ||
args: args{fullName: "faker"}, | ||
want: true, | ||
}, | ||
{ | ||
name: "AllowedPackageBannedScope", | ||
allowList: AllowList{ | ||
Packages: []string{"faker"}, | ||
}, | ||
banList: BanList{ | ||
Scopes: []BanScope{{ | ||
Name: "@github", | ||
}}, | ||
}, | ||
args: args{fullName: "@github/faker"}, | ||
want: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// to simulate: | ||
// if !pkgAllowed || pkgBanned { | ||
// return rex.Status(403, "forbidden") | ||
// } | ||
packageName := tt.args.fullName | ||
|
||
isAllowed := tt.allowList.IsPackageAllowed(packageName) | ||
isBanned := tt.banList.IsPackageBanned(packageName) | ||
|
||
if got := !isAllowed || isBanned; got != tt.want { | ||
t.Errorf("isPackageNotAllowedOrBanned() = %v, want %v. %v isAllowed %v, %v isBanned %v", got, tt.want, packageName, isAllowed, packageName, isBanned) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
|
||
func TestAllowList_IsPackageAllowed(t *testing.T) { | ||
type args struct { | ||
fullName string | ||
} | ||
tests := []struct { | ||
name string | ||
allowList AllowList | ||
args args | ||
want bool | ||
}{ | ||
{ | ||
name: "NoAllowListAllowAnything", | ||
allowList: AllowList{}, | ||
args: args{fullName: "[email protected]"}, | ||
want: true, | ||
}, | ||
{ | ||
name: "AllowedByPackages", | ||
allowList: AllowList{ | ||
Packages: []string{"faker"}, | ||
}, | ||
args: args{fullName: "faker"}, | ||
want: true, | ||
}, | ||
{ | ||
name: "NotAllowedByPackages", | ||
allowList: AllowList{ | ||
Packages: []string{"allowedPackageName"}, | ||
}, | ||
args: args{fullName: "faker"}, | ||
want: false, | ||
}, | ||
{ | ||
name: "AllowedByScope", | ||
allowList: AllowList{ | ||
Scopes: []AllowScope{{ | ||
Name: "@github", | ||
}}, | ||
}, | ||
args: args{fullName: "@github/perfect"}, | ||
want: true, | ||
}, | ||
{ | ||
name: "NotAllowedByScope", | ||
allowList: AllowList{ | ||
Scopes: []AllowScope{{ | ||
Name: "@github", | ||
}}, | ||
}, | ||
args: args{fullName: "@faker/perfect"}, | ||
want: false, | ||
}, | ||
{ | ||
name: "NotAllowedByScope", | ||
allowList: AllowList{ | ||
Scopes: []AllowScope{{ | ||
Name: "@github", | ||
}}, | ||
}, | ||
args: args{fullName: "@faker/perfect"}, | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := tt.allowList.IsPackageAllowed(tt.args.fullName); got != tt.want { | ||
t.Errorf("IsPackageAllowed() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestBanList_IsPackageBanned(t *testing.T) { | ||
type args struct { | ||
fullName string | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters