Skip to content

Commit

Permalink
Add banner message API test case
Browse files Browse the repository at this point in the history
Fix goharbor#19271

Signed-off-by: Yang Jiao <jiaoya@vmware.com>
  • Loading branch information
Yang Jiao committed Oct 31, 2023
1 parent 064a109 commit f03fe69
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 27 deletions.
46 changes: 19 additions & 27 deletions tests/apitests/python/library/configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,17 @@
from v2_swagger_client.rest import ApiException

import base
import json


def set_configurations(client, expect_status_code = 200, expect_response_body = None, **config):
conf = {}

if "project_creation_restriction" in config and config.get("project_creation_restriction") is not None:
conf["project_creation_restriction"] = config.get("project_creation_restriction")
if "token_expiration" in config and config.get("token_expiration") is not None:
conf["token_expiration"] = config.get("token_expiration")
if "ldap_filter" in config and config.get("ldap_filter") is not None:
conf["ldap_filter"] = config.get("ldap_filter")
if "ldap_group_attribute_name" in config and config.get("ldap_group_attribute_name") is not None:
conf["ldap_group_attribute_name"] = config.get("ldap_group_attribute_name")
if "ldap_group_base_dn" in config:
conf["ldap_group_base_dn"] = config.get("ldap_group_base_dn")
if "ldap_group_search_filter" in config and config.get("ldap_group_search_filter") is not None:
conf["ldap_group_search_filter"] = config.get("ldap_group_search_filter")
if "ldap_group_search_scope" in config and config.get("ldap_group_search_scope") is not None:
conf["ldap_group_search_scope"] = config.get("ldap_group_search_scope")
if "ldap_group_admin_dn" in config and config.get("ldap_group_admin_dn") is not None:
conf["ldap_group_admin_dn"] = config.get("ldap_group_admin_dn")
if "audit_log_forward_endpoint" in config and config.get("audit_log_forward_endpoint") is not None:
conf["audit_log_forward_endpoint"] = config.get("audit_log_forward_endpoint")
if "skip_audit_log_database" in config and config.get("skip_audit_log_database") is not None:
conf["skip_audit_log_database"] = config.get("skip_audit_log_database")
if "scanner_skip_update_pulltime" in config and config.get("scanner_skip_update_pulltime") is not None:
conf["scanner_skip_update_pulltime"] = config.get("scanner_skip_update_pulltime")

try:
_, status_code, _ = client.update_configurations_with_http_info(conf)
_, status_code, _ = client.update_configurations_with_http_info(config)
except ApiException as e:
base._assert_status_code(expect_status_code, e.status)
if expect_response_body is not None:
base._assert_status_body(expect_response_body, e.body)
return

base._assert_status_code(expect_status_code, status_code)

class Configurations(base.Base, object):
Expand Down Expand Up @@ -95,3 +70,20 @@ def set_configurations_of_retain_image_last_pull_time(self, is_skip, expect_stat
client = self._get_client(**kwargs)
config=dict(scanner_skip_update_pulltime=is_skip)
set_configurations(client, expect_status_code = expect_status_code, **config)

def set_configurations_of_banner_message(self, message, message_type=None, closable=None, from_date=None, to_date=None, expect_status_code = 200, **kwargs):
client = self._get_client(**kwargs)
banner_message = None
if message == "":
banner_message = ""
else:
banner_message = {
"message": message,
"type": message_type,
"closable": closable,
"fromDate": from_date,
"toDate": to_date
}
banner_message = json.dumps(banner_message)
config=dict(banner_message=banner_message)
set_configurations(client, expect_status_code = expect_status_code, **config)
92 changes: 92 additions & 0 deletions tests/apitests/python/test_banner_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import
import unittest
import json

from testutils import suppress_urllib3_warning
from library.configurations import Configurations
from library.system_info import System_info


class TestReferrersApi(unittest.TestCase):


@suppress_urllib3_warning
def setUp(self):
self.configurations = Configurations()
self.system_info = System_info()
self.message = "This is a test message."
self.message_type = "info"
self.closable = True
self.from_date = "10/27/2023"
self.to_date_str = "10/31/2030"


def testBannerMessage(self):
"""
Test case:
Banner Message Api
Test step and expected result:
1. Setup banner message;
2. Get banner message by configurations api;
3. Check banner message by configurations api;
4. Get banner message by system info api;
5. Check banner message by system info api;
6. Reset banner message;
7. Get banner message by configurations api;
8. Check banner message by configurations api;
9. Get banner message by system info api;
10. Check banner message by system info api;
"""
# 1. Setup banner message
self.configurations.set_configurations_of_banner_message(message=self.message, message_type=self.message_type, closable=self.closable, from_date=self.from_date, to_date=self.to_date_str)

# 2. Get banner message by configurations api
configurations = self.configurations.get_configurations()

# 3. Check banner message by configurations api
config_banner_message = configurations.banner_message
config_banner_message_value = json.loads(config_banner_message.value)
self.assertEqual(config_banner_message.editable, True)
self.checkBannerMseeage(config_banner_message_value)

# 4. Get banner message by system info api
system_info = self.system_info.get_system_info()

# 5. Check banner message by system info api
system_info_banner_message = json.loads(system_info.banner_message)
self.checkBannerMseeage(system_info_banner_message)

# 6. Reset banner message
self.message = ""
self.configurations.set_configurations_of_banner_message(message=self.message)

# 7. Get banner message by configurations api
configurations = self.configurations.get_configurations()

# 8. Check banner message by configurations api
config_banner_message = configurations.banner_message
self.assertEqual(config_banner_message.editable, True)
self.checkBannerMseeage(config_banner_message.value)

# 9. Get banner message by system info api
system_info = self.system_info.get_system_info()

# 10. Check banner message by system info api
self.checkBannerMseeage(system_info.banner_message)


def checkBannerMseeage(self, banner_mseeage):
if self.message == "":
self.assertEqual(banner_mseeage, "")
else:
self.assertEqual(banner_mseeage["message"], self.message)
self.assertEqual(banner_mseeage["type"], self.message_type)
self.assertEqual(banner_mseeage["closable"], self.closable)
self.assertEqual(banner_mseeage["fromDate"], self.from_date)
self.assertEqual(banner_mseeage["toDate"], self.to_date_str)


if __name__ == '__main__':
unittest.main()
4 changes: 4 additions & 0 deletions tests/robot-cases/Group0-BAT/API_DB.robot
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,7 @@ Test Case - Podman Pull And Push To Harbor
Test Case - Security Hub
[Tags] security_hub
Harbor API Test ./tests/apitests/python/test_security_hub.py

Test Case - Banner Message
[Tags] banner_message
Harbor API Test ./tests/apitests/python/test_banner_message.py

0 comments on commit f03fe69

Please sign in to comment.