Skip to content

Commit 344206c

Browse files
ZachEddyEvgeniy-L
authored andcommitted
Move from toolkit (#1)
* Move protoc-gen-perm files from toolkit without editing * Update imports from toolkit to protoc-gen-perm * Update protoc package from atlas.api to perm * Fix broken unit tests * Fix grammar errors * Add dep for dependency management * Create a .gitignore for the repository * Add Travis CI * Update package name in test directory * Make note about adding protoc-gen-perm to gentool * Update reference to vendor folder * Skip test that uses protoc command
1 parent e79d110 commit 344206c

17 files changed

+1226
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
vendor
2+
debug
3+
*.vscode
4+
*.idea
5+
*.DS_Store
6+
*.bak
7+
*.log
8+
*.report

.travis.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
language: go
2+
3+
go:
4+
- "1.10"
5+
6+
env:
7+
- DEP_VERSION="0.4.1"
8+
9+
before_install:
10+
# Download the binary to bin folder in $GOPATH
11+
- curl -L -s https://github.com/golang/dep/releases/download/v${DEP_VERSION}/dep-linux-amd64 -o $GOPATH/bin/dep
12+
# Make the binary executable
13+
- chmod +x $GOPATH/bin/dep
14+
15+
script:
16+
- make vendor
17+
- make install
18+
# NOTE: We will be able to run protoc commands from Travis once
19+
# protoc-gen-perm is added to the atlas gentool. For now, we can just run
20+
# tests to ensure that everything works as expected.
21+
- go test -v ./...

Gopkg.lock

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Gopkg.toml example
2+
#
3+
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
4+
# for detailed Gopkg.toml documentation.
5+
#
6+
# required = ["github.com/user/thing/cmd/thing"]
7+
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
8+
#
9+
# [[constraint]]
10+
# name = "github.com/user/project"
11+
# version = "1.0.0"
12+
#
13+
# [[constraint]]
14+
# name = "github.com/user/project2"
15+
# branch = "dev"
16+
# source = "github.com/myfork/project2"
17+
#
18+
# [[override]]
19+
# name = "github.com/x/y"
20+
# version = "2.4.0"
21+
#
22+
# [prune]
23+
# non-go = false
24+
# go-tests = true
25+
# unused-packages = true
26+
27+
28+
[[constraint]]
29+
name = "github.com/gogo/protobuf"
30+
version = "1.1.1"
31+
32+
[[constraint]]
33+
name = "github.com/infobloxopen/atlas-app-toolkit"
34+
version = "0.10.0"
35+
36+
[prune]
37+
go-tests = true
38+
unused-packages = true

Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
GOPATH ?= $(HOME)/go
2+
SRCPATH := $(patsubst %/,%,$(GOPATH))/src
3+
4+
default: options install
5+
6+
7+
.PHONY: options
8+
options:
9+
protoc -I. -I$(SRCPATH) -I./vendor \
10+
--gogo_out="Mgoogle/protobuf/descriptor.proto=github.com/gogo/protobuf/protoc-gen-gogo/descriptor:$(SRCPATH)" \
11+
options/collection_permissions.proto
12+
13+
.PHONY: install
14+
install:
15+
go install
16+
17+
.PHONY: perm-test
18+
perm-test: ./example/* ./test/*
19+
protoc -I. -I${SRCPATH} -I./vendor --perm_out="./" example/example.proto
20+
go test ./...
21+
22+
.PHONY: vendor
23+
vendor:
24+
dep ensure -vendor-only

example/example.pb.perm.go

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/example.proto

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
syntax = "proto3";
3+
package example;
4+
5+
import "github.com/infobloxopen/protoc-gen-perm/options/collection_permissions.proto";
6+
import "github.com/infobloxopen/atlas-app-toolkit/query/collection_operators.proto";
7+
8+
message User {
9+
string first_name = 2 [(perm.permissions).filters.allow = "MATCH"];
10+
string middle_name = 3 [(perm.permissions).disable_sorting = true];
11+
string last_name = 4 [(perm.permissions) = {filters: {allow: "EQ"}, disable_sorting: false}];
12+
string age = 7 [(perm.permissions).filters.allow = "MATCH"];;
13+
int64 height = 15 [(perm.permissions).filters.allow = "GE"];
14+
float weight = 16 [(perm.permissions).filters.deny = "LE"];
15+
bool OnVacation = 17 [(perm.permissions).disable_sorting = true];
16+
}
17+
18+
19+
message ListRequest {
20+
infoblox.api.Filtering filter = 1;
21+
infoblox.api.Sorting order_by = 2;
22+
infoblox.api.FieldSelection fields = 3;
23+
infoblox.api.Pagination paging = 4;
24+
}
25+
26+
message ReadRequest {
27+
infoblox.api.Sorting order_by = 1;
28+
infoblox.api.FieldSelection fields = 2;
29+
infoblox.api.Pagination paging = 3;
30+
}
31+
32+
message UserResponse {
33+
repeated User data = 1;
34+
}
35+
36+
service TestService {
37+
rpc List (ListRequest) returns (UserResponse) {
38+
}
39+
40+
rpc Read (ReadRequest) returns (UserResponse) {
41+
}
42+
}

example/example_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package example
2+
3+
import (
4+
"testing"
5+
6+
"github.com/infobloxopen/atlas-app-toolkit/query"
7+
)
8+
9+
func TestFilteringPermissionsValidation(t *testing.T) {
10+
data := map[string]string{
11+
"first_name==\"Jan\"": "Operation EQ is not allowed for 'first_name'",
12+
"User.first_name!=\"Sam\"": "Operation EQ is not allowed for 'User.first_name'",
13+
"first_name~\"Sam.*\"": "",
14+
15+
"middle_name==\"Jan\"": "",
16+
"middle_name!=\"Sam\"": "",
17+
"User.middle_name~\"Sam.*\"": "",
18+
19+
"last_name~\"Jan\"": "Operation MATCH is not allowed for 'last_name'",
20+
"User.last_name!~\"Sam\"": "Operation MATCH is not allowed for 'User.last_name'",
21+
"last_name==\"Sam.*\"": "",
22+
"User.last_name!=\"Sam.*\"": "",
23+
24+
"age==18": "Operation EQ is not allowed for 'age'",
25+
"age>=18": "Operation GE is not allowed for 'age'",
26+
"User.age<18": "Operation LT is not allowed for 'User.age'",
27+
28+
"height>=180": "",
29+
"height>180": "Operation GT is not allowed for 'height'",
30+
"User.height<180": "Operation LT is not allowed for 'User.height'",
31+
}
32+
33+
for param, expected := range data {
34+
response := ""
35+
f, err := query.ParseFiltering(param)
36+
if err != nil {
37+
t.Fatalf("Invalid filtering data '%s'", param)
38+
}
39+
resp := Validate(f, nil, "/example.TestService/List")
40+
if resp != nil {
41+
response = resp.Error()
42+
}
43+
if expected != response {
44+
t.Errorf("Error, for filtering data '%s' expected '%s', got '%s'", param, expected, response)
45+
}
46+
47+
}
48+
}
49+
50+
func TestFilteringPermissionsValidationForRead(t *testing.T) {
51+
data := []string{
52+
"first_name==\"Jan\"",
53+
"last_name~\"Jan\"",
54+
"age==18",
55+
"height>=180",
56+
"height<180",
57+
}
58+
59+
for _, param := range data {
60+
f, err := query.ParseFiltering(param)
61+
if err != nil {
62+
t.Fatalf("Invalid filtering data '%s'", param)
63+
}
64+
resp := Validate(f, nil, "/example.TestService/Read")
65+
if resp != nil {
66+
t.Errorf("Error, for filtering data '%s' got '%s'", param, resp.Error())
67+
}
68+
69+
}
70+
}
71+
72+
func TestSortingPermissionsValidation(t *testing.T) {
73+
data := map[string]string{
74+
"first_name, height, middle_name": "pagination is not allowed for 'middle_name'",
75+
"User.first_name, User.height, User.middle_name": "pagination is not allowed for 'User.middle_name'",
76+
}
77+
78+
for param, expected := range data {
79+
response := ""
80+
p, err := query.ParseSorting(param)
81+
if err != nil {
82+
t.Fatalf("Invalid paging data '%s'", param)
83+
}
84+
resp := Validate(nil, p, "/example.TestService/List")
85+
if resp != nil {
86+
response = resp.Error()
87+
}
88+
if expected != response {
89+
t.Errorf("Error, for paging data '%s' expected '%s', got '%s'", param, expected, response)
90+
}
91+
}
92+
}

main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import (
4+
"github.com/gogo/protobuf/vanity/command"
5+
"github.com/infobloxopen/protoc-gen-perm/plugin"
6+
)
7+
8+
func main() {
9+
plugin := &plugin.PermPlugin{}
10+
response := command.GeneratePlugin(command.Read(), plugin, ".pb.perm.go")
11+
plugin.CleanFiles(response)
12+
command.Write(response)
13+
}

0 commit comments

Comments
 (0)