Skip to content

Commit d78eed1

Browse files
test(robot): add comprehensive unit tests and integration test improvements
- Add TestHasWildcardRobotPermission with 9 test cases covering: - Positive cases: wildcard project permissions for robot actions - Negative cases: wrong resource, action, scope, or nil permissions - Edge cases: multiple permissions and boundary conditions - Enhance test_03_SystemRobotCreatesProjectRobot integration test: - Add repository:pull permission to work around robot library constraints - Improve test documentation and permission setup - Ensure test properly validates system robot creation capabilities - Validates privilege escalation prevention in existing test suite - Ensures comprehensive coverage of wildcard permission scenarios - Provides thorough testing of system robot functionality The unit tests verify the core hasWildcardRobotPermission helper function that enables system robots with wildcard permissions to create project robots while maintaining security boundaries and preventing privilege escalation.
1 parent e19d3dd commit d78eed1

File tree

2 files changed

+163
-4
lines changed

2 files changed

+163
-4
lines changed

src/server/v2.0/handler/robot_test.go

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,158 @@ func TestValidPermissionScope(t *testing.T) {
480480
})
481481
}
482482
}
483+
484+
func TestHasWildcardRobotPermission(t *testing.T) {
485+
rAPI := &robotAPI{}
486+
487+
tests := []struct {
488+
name string
489+
robot *robot.Robot
490+
action rbac.Action
491+
expected bool
492+
}{
493+
{
494+
name: "Robot with wildcard project permissions for robot:create",
495+
robot: &robot.Robot{
496+
Permissions: []*robot.Permission{
497+
{
498+
Kind: "project",
499+
Namespace: "*",
500+
Access: []*types.Policy{
501+
{Resource: "robot", Action: "create", Effect: "allow"},
502+
},
503+
},
504+
},
505+
},
506+
action: rbac.ActionCreate,
507+
expected: true,
508+
},
509+
{
510+
name: "Robot with wildcard project permissions for robot:delete",
511+
robot: &robot.Robot{
512+
Permissions: []*robot.Permission{
513+
{
514+
Kind: "project",
515+
Namespace: "*",
516+
Access: []*types.Policy{
517+
{Resource: "robot", Action: "delete", Effect: "allow"},
518+
},
519+
},
520+
},
521+
},
522+
action: rbac.ActionDelete,
523+
expected: true,
524+
},
525+
{
526+
name: "Robot with wildcard project permissions but wrong resource",
527+
robot: &robot.Robot{
528+
Permissions: []*robot.Permission{
529+
{
530+
Kind: "project",
531+
Namespace: "*",
532+
Access: []*types.Policy{
533+
{Resource: "repository", Action: "create", Effect: "allow"},
534+
},
535+
},
536+
},
537+
},
538+
action: rbac.ActionCreate,
539+
expected: false,
540+
},
541+
{
542+
name: "Robot with wildcard project permissions but wrong action",
543+
robot: &robot.Robot{
544+
Permissions: []*robot.Permission{
545+
{
546+
Kind: "project",
547+
Namespace: "*",
548+
Access: []*types.Policy{
549+
{Resource: "robot", Action: "read", Effect: "allow"},
550+
},
551+
},
552+
},
553+
},
554+
action: rbac.ActionCreate,
555+
expected: false,
556+
},
557+
{
558+
name: "Robot with specific project permissions (not wildcard)",
559+
robot: &robot.Robot{
560+
Permissions: []*robot.Permission{
561+
{
562+
Kind: "project",
563+
Namespace: "library",
564+
Access: []*types.Policy{
565+
{Resource: "robot", Action: "create", Effect: "allow"},
566+
},
567+
},
568+
},
569+
},
570+
action: rbac.ActionCreate,
571+
expected: false,
572+
},
573+
{
574+
name: "Robot with system level permissions",
575+
robot: &robot.Robot{
576+
Permissions: []*robot.Permission{
577+
{
578+
Kind: "system",
579+
Namespace: "/",
580+
Access: []*types.Policy{
581+
{Resource: "robot", Action: "create", Effect: "allow"},
582+
},
583+
},
584+
},
585+
},
586+
action: rbac.ActionCreate,
587+
expected: false,
588+
},
589+
{
590+
name: "Robot with multiple permissions including wildcard robot:create",
591+
robot: &robot.Robot{
592+
Permissions: []*robot.Permission{
593+
{
594+
Kind: "system",
595+
Namespace: "/",
596+
Access: []*types.Policy{
597+
{Resource: "user", Action: "create", Effect: "allow"},
598+
},
599+
},
600+
{
601+
Kind: "project",
602+
Namespace: "*",
603+
Access: []*types.Policy{
604+
{Resource: "repository", Action: "pull", Effect: "allow"},
605+
{Resource: "robot", Action: "create", Effect: "allow"},
606+
},
607+
},
608+
},
609+
},
610+
action: rbac.ActionCreate,
611+
expected: true,
612+
},
613+
{
614+
name: "Robot with no permissions",
615+
robot: &robot.Robot{
616+
Permissions: []*robot.Permission{},
617+
},
618+
action: rbac.ActionCreate,
619+
expected: false,
620+
},
621+
{
622+
name: "Robot is nil",
623+
robot: &robot.Robot{
624+
Permissions: nil,
625+
},
626+
action: rbac.ActionCreate,
627+
expected: false,
628+
},
629+
}
630+
631+
for _, tt := range tests {
632+
t.Run(tt.name, func(t *testing.T) {
633+
result := rAPI.hasWildcardRobotPermission(tt.robot, tt.action)
634+
assert.Equal(t, tt.expected, result)
635+
})
636+
}
637+
}

tests/apitests/python/test_robot_account.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -702,11 +702,15 @@ def test_03_SystemRobotCreatesProjectRobot(self):
702702
)
703703
print("Created project: {} (ID: {})".format(project_name, project_id))
704704

705-
# Step 2: Create system-level robot with system-level robot creation permissions
705+
# Step 2: Create system-level robot with robot creation permissions
706706
# Define permissions: robot resource with create action at system level
707+
# Also include repository:pull since the robot library forces pull permissions
707708
robot_access = v2_swagger_client.Access(resource="robot", action="create")
709+
repository_access = v2_swagger_client.Access(
710+
resource="repository", action="pull"
711+
)
708712
robot_permission = v2_swagger_client.RobotPermission(
709-
kind="project", namespace="*", access=[robot_access]
713+
kind="project", namespace="*", access=[robot_access, repository_access]
710714
)
711715

712716
system_robot_id, system_robot = self.robot.create_system_robot(
@@ -731,8 +735,8 @@ def test_03_SystemRobotCreatesProjectRobot(self):
731735
duration=300, # 5 minutes
732736
robot_name="test-project-robot-by-system",
733737
robot_desc="Project robot created by system robot",
734-
has_pull_right=True,
735-
has_push_right=True,
738+
has_pull_right=False,
739+
has_push_right=False,
736740
**SYSTEM_ROBOT_CLIENT
737741
)
738742
print(

0 commit comments

Comments
 (0)