Exemplo n.º 1
0
def main(arguments):

    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('--list-servers', help="Put this flag to simply list the available servers. This overrides other flags.",
                        action='store_true', default=False)
    parser.add_argument('-r', '--refresh_data', help="Put this flag to refresh the resources classes list. This overrides other flags.", action='store_true', default=False)
    parser.add_argument('-v', '--view_classes', help="Read the classes list.", action='store_true', default=False)
    parser.add_argument('-u', '--url', help="This value can override the default url (SWGAide) not recommended since this tool is designed to work with SWGAide.",
                        type=str, default='https://swgaide.com/')
    parser.add_argument(
        '-s', '--server', help="The SWG server number. Use flag '--list-servers' to find your server.", type=int, required=True)
    # TODO: add the possibility to override the output file.
    # parser.add_argument('-o', '--outfile', help="The output file to generate the resources.", 
    #                    default='resources.pickle', type=argparse.FileType('w'))

    args = parser.parse_args(arguments)

    if args.list_servers:
        print_server_list(args.url)
    elif args.view_classes:
        print_classes()
    elif args.refresh_data or not os.path.isfile('swg-grm-extractor/data/classes.pkl'): 
        classes = get_resources_classes('http://www.swgcraft.org/')
        mapping = get_resources_mapping('http://www.swgcraft.org/')
        aggregate_classes(classes, mapping) 
    else:
        get_resources(args.url, args.server)
Exemplo n.º 2
0
    def _get_pods_for_service(self, service_name, namespace):
        pod_list = []
        platf = platform.system()
        kubeplus_home = os.getenv('KUBEPLUS_HOME', '/')
        cmd = ''
        if platf == "Darwin":
            cmd = kubeplus_home + '/plugins/kubediscovery-macos connections Service ' + service_name + ' ' + namespace + ' -o json'
        if platf == "Linux":
            cmd = kubeplus_home + '/plugins/kubediscovery-linux connections Service ' + service_name + ' ' + namespace + ' -o json'

        if cmd:
            output = ''
            try:
                output = subprocess.Popen(cmd,
                                          stdout=subprocess.PIPE,
                                          stderr=subprocess.PIPE,
                                          shell=True).communicate()[0]
                output = output.decode('utf-8')
            except Exception as e:
                print(e)

            if output:
                try:
                    json_output = json.loads(output)
                except Exception as e:
                    print(e)

                pod_list = utils.get_pods(json_output)
                res_list = utils.get_resources(json_output)

            #pod_list = self._parse_pods_from_connections_op(output)
        return pod_list, res_list
Exemplo n.º 3
0
def test_message_records_completed(client, mocker):
    """Test message records processing with handling the complition of the task."""
    admin = client.data["admin"]
    user = OrcidToken.select().join(User).where(
        User.orcid.is_null(False)).first().user
    org = user.organisation
    mocker.patch(
        "orcid_hub.orcid_client.MemberAPIV3.get_resources",
        return_value=get_resources(org=user.organisation, user=user),
    )
    send = mocker.patch("emails.message.MessageSendMixin.send")
    execute = mocker.patch(
        "orcid_hub.orcid_client.MemberAPIV3.execute",
        return_value=Mock(headers={"Location": f"{user.orcid}/ABC/123456"},
                          json=dict(visibility="public")),
    )
    task = Task.create(
        is_active=True,
        created_by=admin,
        filename="messages.json",
        task_type=TaskType.RESOURCE,
        is_raw=True,
        org=org,
    )
    record = MessageRecord.create(task=task, message="{}", is_active=True)
    invitee = Invitee.create(email=user.email, orcid=user.orcid)
    record.invitees.add(invitee)
    record.save()
    utils.process_message_records(record_id=record.id)
    send.assert_called_once()

    MessageRecord.update(processed_at=None).execute()
    Invitee.update(processed_at=None).execute()
    send_email = mocker.patch("orcid_hub.utils.send_email")
    utils.process_message_records(record_id=record.id)
    send_email.assert_called_once()
    args, _ = send_email.call_args
    assert args[0] == "email/task_completed.html"
    assert Task.get(task.id).completed_at is not None
Exemplo n.º 4
0
def test_message_records(client, mocker):
    """Test message records processing."""
    user = client.data["admin"]
    client.login(user, follow_redirects=True)

    raw_data0 = readup_test_data("resources.json")
    resp = client.post(
        "/load/task/RESOURCE",
        data={"file_": (BytesIO(raw_data0), "resources.json")},
        follow_redirects=True,
    )
    assert resp.status_code == 200
    assert b"SYN-18-UOA-333" in resp.data
    assert b"SYN-18-UOA-222" in resp.data
    # assert b"*****@*****.**" in resp.data
    # assert b"*****@*****.**" in resp.data
    task = Task.get(filename="resources.json")
    assert task.records.count() == 2

    send = mocker.patch("emails.message.MessageSendMixin.send")
    get_resources_mock = mocker.patch(
        "orcid_hub.orcid_client.MemberAPIV3.get_resources",
        return_value=get_resources(org=user.organisation, user=user),
    )
    execute = mocker.patch("orcid_hub.orcid_client.MemberAPIV3.execute")

    resp = client.post("/activate_all/?url=/resources",
                       data=dict(task_id=task.id))
    assert resp.status_code == 302
    assert resp.location.endswith("/resources")

    get_resources_mock.assert_called()
    execute.assert_called()

    send.assert_called()
    assert UserInvitation.select().count() == 3

    # via task view - activate all:
    UserInvitation.delete().execute()
    MessageRecord.update(is_active=False).execute()

    resp = client.post("/admin/task/action/",
                       data=dict(action="activate", rowid=[task.id]))
    assert resp.status_code == 302
    assert resp.location.endswith("/admin/task/")
    assert UserInvitation.select().count() == 3

    # via task view - reset all:
    UserInvitation.delete().execute()
    resp = client.post("/admin/task/action/",
                       data=dict(action="reset", rowid=[task.id]))
    assert resp.status_code == 302
    assert resp.location.endswith("/admin/task/")
    assert UserInvitation.select().count() == 3

    # Edit invitees
    for r in task.records:
        for i in r.invitees:
            resp = client.get(f"/admin/invitee/details/?id={i.id}")
            resp = client.post(
                f"/admin/invitee/edit/?id={i.id}&url=/admin/invitee/%2F%3Frecord_id={r.id}",
                data=dict(
                    identifier=f"ID-{i.id}",
                    # email="*****@*****.**",
                    first_name=i.first_name or "FN",
                    last_name=i.first_name or "LN",
                    visibility="limited",
                ),
                follow_redirects=True,
            )
            resp = client.get(f"/admin/invitee/details/?id={i.id}")
            invitee = Invitee.get(i.id)
            assert invitee.identifier == f"ID-{i.id}"

        invitee_count = r.invitees.count()
        resp = client.post(
            f"/admin/invitee/new/?url=/admin/invitee/%2F%3Frecord_id={r.id}",
            data=dict(
                email="*****@*****.**",
                first_name="FN",
                last_name="LN",
                visibility="limited",
            ),
        )
        assert r.invitees.count() == invitee_count + 1

    # Export and re-import
    for export_type in ["json", "yaml"]:
        resp = client.get(
            f"/admin/messagerecord/export/{export_type}/?task_id={task.id}")
        data = json.loads(resp.data) if export_type == "json" else yaml.load(
            resp.data)
        data0 = data.copy()
        del data0["id"]

        file_name = f"resources_reimport.{export_type}"
        resp = client.post(
            "/load/task/RESOURCE",
            data={
                "file_": (
                    BytesIO((json.dumps(data0) if export_type == "json" else
                             yaml.dump(data0)).encode()),
                    file_name,
                )
            },
            follow_redirects=True,
        )
        assert resp.status_code == 200
        assert b"SYN-18-UOA-333" in resp.data
        assert b"SYN-18-UOA-222" in resp.data

        t = Task.get(filename=file_name)
        assert t.records.count() == 2
        assert t.is_raw

        # with the the existing ID
        resp = client.post(
            "/load/task/RESOURCE",
            data={
                "file_": (
                    BytesIO((json.dumps(data) if export_type == "json" else
                             yaml.dump(data)).encode()),
                    file_name,
                )
            },
            follow_redirects=True,
        )
        assert resp.status_code == 200
        assert b"SYN-18-UOA-333" in resp.data
        assert b"SYN-18-UOA-222" in resp.data

        t = Task.get(filename=file_name)
        assert t.records.count() == 2
        assert t.is_raw