-
Notifications
You must be signed in to change notification settings - Fork 567
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
NET-260 REST API tests #2351
Closed
Closed
NET-260 REST API tests #2351
Changes from 13 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
fe4cd83
return 401 instead of 403
TobiaszCudnik e816a98
fixed http.StatusForbidden
TobiaszCudnik e9e2021
Tagged build version (temp)
TobiaszCudnik d1ed188
Unauthorized_Err when applicable
TobiaszCudnik d062bc2
Merge branch 'develop' into NET-163/403-insteadof-401
TobiaszCudnik abdd31b
untagged version
TobiaszCudnik dab361d
Merge branch 'develop' into NET-163/403-insteadof-401
TobiaszCudnik 47543fd
fixed PUT /api/users/networks/user1
TobiaszCudnik 381bb1d
- expired token redirs to login
TobiaszCudnik cec227a
Merge branch 'develop' into NET-152/enrollment-keys-for-non-admins
TobiaszCudnik 577c696
handle user perms in `/hosts`
TobiaszCudnik 79810a5
Merge branch 'develop' into NET-152/enrollment-keys-for-non-admins
TobiaszCudnik c619373
api test for enrollment keys
TobiaszCudnik dc5b373
removed debug
TobiaszCudnik ad4efdf
misc
TobiaszCudnik 8953f41
- support masteradmin
TobiaszCudnik 7551454
Merge branch 'NET-152/enrollment-keys-for-non-admins' into NET-260/re…
TobiaszCudnik 4c5845b
- added masteradmin case
TobiaszCudnik 06ad728
added `ismaster` to middleware
TobiaszCudnik cd662a6
Merge branch 'develop' into NET-152/enrollment-keys-for-non-admins
9096623
Merge branch 'NET-152/enrollment-keys-for-non-admins' into NET-260/re…
TobiaszCudnik 908e253
inactive test for 403
TobiaszCudnik 490902d
added comments
TobiaszCudnik 14f1f54
Merge branch 'develop' into NET-260/rest-api-tests
513445e
Merge branch 'develop' into NET-260/rest-api-tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package test | ||
|
||
import ( | ||
"context" | ||
"github.com/gravitl/netmaker/cli/config" | ||
"github.com/gravitl/netmaker/cli/functions" | ||
controller "github.com/gravitl/netmaker/controllers" | ||
"github.com/gravitl/netmaker/database" | ||
"github.com/gravitl/netmaker/logic" | ||
"github.com/gravitl/netmaker/models" | ||
"github.com/gravitl/netmaker/servercfg" | ||
"github.com/stretchr/testify/assert" | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func DBInit() { | ||
database.InitializeDatabase() | ||
database.DeleteAllRecords(database.USERS_TABLE_NAME) | ||
database.DeleteAllRecords(database.NETWORKS_TABLE_NAME) | ||
database.DeleteAllRecords(database.NETWORK_USER_TABLE_NAME) | ||
database.DeleteAllRecords(database.ENROLLMENT_KEYS_TABLE_NAME) | ||
// TODO rest | ||
} | ||
|
||
func TestHasNetworksAccessAPI(t *testing.T) { | ||
// setup / teardown (TODO extract) | ||
DBInit() | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
wg := sync.WaitGroup{} | ||
wg.Add(1) | ||
go func() { | ||
wg.Wait() | ||
defer database.CloseDB() | ||
}() | ||
var err error | ||
port := servercfg.GetAPIPort() | ||
userPass := "bar123" | ||
user := &models.User{ | ||
UserName: "foo", | ||
Password: userPass, | ||
// TODO should be handled in fixtures? | ||
Networks: []string{"network-1"}, | ||
IsAdmin: false, | ||
Groups: nil, | ||
} | ||
err = logic.CreateUser(user) | ||
if err != nil { | ||
t.Error("Error creating a user ", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could use |
||
} | ||
configCtx := config.Context{ | ||
Endpoint: "http://localhost:" + port, | ||
Username: user.UserName, | ||
Password: userPass, | ||
} | ||
config.SetContext("test-ctx-1", configCtx) | ||
config.SetCurrentContext("test-ctx-1") | ||
|
||
// fixtures | ||
n1 := models.Network{ | ||
AddressRange: "10.101.0.0/16", | ||
NetID: "network-1", | ||
NodesLastModified: 1685013908, | ||
NetworkLastModified: 1684474527, | ||
DefaultInterface: "nm-netmaker", | ||
DefaultListenPort: 51821, | ||
NodeLimit: 999999999, | ||
DefaultPostDown: "", | ||
DefaultKeepalive: 20, | ||
AllowManualSignUp: "no", | ||
IsIPv4: "yes", | ||
IsIPv6: "no", | ||
DefaultUDPHolePunch: "no", | ||
DefaultMTU: 1280, | ||
DefaultACL: "yes", | ||
ProSettings: nil, | ||
} | ||
_, err = logic.CreateNetwork(n1) | ||
if err != nil { | ||
t.Error("Error creating a network ", err) | ||
} | ||
// copy | ||
n2 := n1 | ||
n2.NetID = "network-2" | ||
_, err = logic.CreateNetwork(n2) | ||
if err != nil { | ||
t.Error("Error creating a network ", err) | ||
} | ||
k1, _ := logic.CreateEnrollmentKey(0, time.Time{}, []string{n1.NetID}, nil, true) | ||
if err = logic.Tokenize(k1, servercfg.GetAPIHost()); err != nil { | ||
t.Error("failed to get token values for keys:", err) | ||
} | ||
_, _ = logic.CreateEnrollmentKey(0, time.Time{}, []string{n2.NetID}, nil, true) | ||
_, _ = logic.CreateEnrollmentKey(0, time.Time{}, []string{n1.NetID, n2.NetID}, nil, true) | ||
|
||
go controller.HandleRESTRequests(&wg, ctx) | ||
// TODO make sure that HTTP is up | ||
time.Sleep(1 * time.Second) | ||
keys := *functions.GetEnrollmentKeys() | ||
|
||
assert.Len(t, keys, 1, "1 key expected") | ||
assert.Len(t, keys[0].Networks, 1, "Key with 1 network expected") | ||
assert.Equal(t, keys[0].Networks[0], n1.NetID, "Network ID matches") | ||
assert.Equal(t, keys[0].Token, k1.Token, "Token matches") | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did you run go fmt and/or go import. stdlib imports should be listed first followed by external lib imports
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
goimports
should be added to precommit.gofmt
orders it differently.