Commit 1cc04183 authored by danfengliu's avatar danfengliu
Browse files

add test case for edit project creation;


fixed some space and import issues coddcy;
remove a lib;
Signed-off-by: default avatardanfengliu <danfengl@vmware.com>
parent b7c56edf
# -*- coding: utf-8 -*-
import base
import swagger_client
class Configurations(base.Base):
def get_configurations(self, item_name = None, expect_status_code = 200, **kwargs):
client = self._get_client(**kwargs)
data, status_code, _ = client.configurations_get_with_http_info()
base._assert_status_code(expect_status_code, status_code)
if item_name is not None:
return {
'project_creation_restriction': data.project_creation_restriction.value,
}.get(item_name,'error')
return data
def set_configurations_of_project_creation_restriction_success(self, project_creation_restriction, expect_status_code = 200, **kwargs):
client = self._get_client(**kwargs)
conf = swagger_client.Configurations(project_creation_restriction = project_creation_restriction)
_, status_code, _ = client.configurations_put_with_http_info(conf)
base._assert_status_code(200, status_code)
item_value = self.get_configurations(item_name = "project_creation_restriction", **kwargs)
if item_value != project_creation_restriction:
raise Exception("Failed to set system configuration item {} to value {},\
actual value is {}".format("project_creation_restriction", project_creation_restriction, item_value))
# -*- coding: utf-8 -*-
import sys
import base
import swagger_client
from swagger_client.rest import ApiException
class Project(base.Base):
def create_project(self, name=None, metadata=None, **kwargs):
def create_project(self, name=None, metadata=None, expect_status_code = 201, expect_response_body = None, **kwargs):
if name is None:
name = base._random_name("project")
if metadata is None:
metadata = {}
client = self._get_client(**kwargs)
_, status_code, header = client.projects_post_with_http_info(
swagger_client.ProjectReq(name, metadata))
base._assert_status_code(201, status_code)
project_id = base._get_id_from_header(header)
return project_id, name
try:
_, status_code, header = client.projects_post_with_http_info(swagger_client.ProjectReq(name, metadata))
except ApiException as e:
if e.status == expect_status_code:
if expect_response_body is not None and e.body.strip() != expect_response_body.strip():
raise Exception(r"Create project result body is not as expected {} actual status is {}.".format(expect_response_body.strip(), e.body.strip()))
else:
return e.reason, e.body
else:
raise Exception(r"Create project result is not as expected {} actual status is {}.".format(expect_status_code, e.status))
base._assert_status_code(expect_status_code, status_code)
return base._get_id_from_header(header), name
def get_projects(self, params, **kwargs):
client = self._get_client(**kwargs)
......@@ -40,7 +49,6 @@ class Project(base.Base):
404: False,
}.get(status_code,'error')
def get_project(self, project_id, **kwargs):
client = self._get_client(**kwargs)
data, status_code, _ = client.projects_project_id_get_with_http_info(project_id)
......
from __future__ import absolute_import
import unittest
from testutils import CLIENT
from testutils import TEARDOWN
from library.project import Project
from library.user import User
from library.configurations import Configurations
class TestProjects(unittest.TestCase):
@classmethod
def setUp(self):
conf = Configurations()
self.conf= conf
project = Project()
self.project= project
user = User()
self.user= user
@classmethod
def tearDown(self):
print "Case completed"
@unittest.skipIf(TEARDOWN == False, "Test data should be remain in the harbor.")
def test_ClearData(self):
print "Clear trace"
#1. Delete project(PA);
self.project.delete_project(TestProjects.project_edit_project_creation_id, **TestProjects.USER_edit_project_creation_CLIENT)
#2. Delete user(UA);
self.user.delete_user(TestProjects.user_edit_project_creation_id, **TestProjects.ADMIN_CLIENT)
def testEditProjectCreation(self):
"""
Test case:
Edit Project Creation
Test step & Expectation:
1. Create a new user(UA);
2. Set project creation to "admin only";
3. Create a new project(PA) by user(UA), and fail to create a new project;
4. Set project creation to "everyone";
5. Create a new project(PA) by user(UA), success to create a project.
Tear down:
1. Delete project(PA);
2. Delete user(UA);
"""
admin_user = "admin"
admin_pwd = "Harbor12345"
url = CLIENT["endpoint"]
user_edit_project_creation_password = "Aa123456"
TestProjects.ADMIN_CLIENT=dict(endpoint = url, username = admin_user, password = admin_pwd)
#1. Create a new user(UA);
TestProjects.user_edit_project_creation_id, user_edit_project_creation_name = self.user.create_user_success(user_password = user_edit_project_creation_password, **TestProjects.ADMIN_CLIENT)
TestProjects.USER_edit_project_creation_CLIENT=dict(endpoint = url, username = user_edit_project_creation_name, password = user_edit_project_creation_password)
#2. Set project creation to "admin only";
self.conf.set_configurations_of_project_creation_restriction_success("adminonly", **TestProjects.ADMIN_CLIENT)
#3. Create a new project(PA) by user(UA), and fail to create a new project;
self.project.create_project(metadata = {"public": "false"}, expect_status_code = 403,
expect_response_body = "Only system admin can create project", **TestProjects.USER_edit_project_creation_CLIENT)
#4. Set project creation to "everyone";
self.conf.set_configurations_of_project_creation_restriction_success("everyone", **TestProjects.ADMIN_CLIENT)
#5. Create a new project(PA) by user(UA), success to create a project.
TestProjects.project_edit_project_creation_id, _ = self.project.create_project(metadata = {"public": "false"}, **TestProjects.USER_edit_project_creation_CLIENT)
if __name__ == '__main__':
unittest.main()
\ No newline at end of file
......@@ -25,3 +25,5 @@ Test Case - Add a System Global Label to a Certain Tag
Harbor API Test ./tests/apitests/python/test_add_sys_label_to_tag.py
Test Case - Add Replication Rule
Harbor API Test ./tests/apitests/python/test_add_replication_rule.py
Test Case - Edit Project Creation
Harbor API Test ./tests/apitests/python/test_edit_project_creation.py
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment