github.com/minio/console@v1.4.1/web-app/tests/utils/functions.ts (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2022 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  import * as roles from "./roles";
    18  import * as elements from "./elements";
    19  import * as constants from "./constants";
    20  import { Selector } from "testcafe";
    21  
    22  import * as Minio from "minio";
    23  
    24  export const setUpBucket = (t, modifier) => {
    25    return setUpNamedBucket(t, `${constants.TEST_BUCKET_NAME}-${modifier}`);
    26  };
    27  
    28  export const setUpNamedBucket = (t, name) => {
    29    const minioClient = new Minio.Client({
    30      endPoint: "localhost",
    31      port: 9000,
    32      useSSL: false,
    33      accessKey: "minioadmin",
    34      secretKey: "minioadmin",
    35    });
    36    return minioClient.makeBucket(name, "us-east-1").catch((err) => {
    37      console.log(err);
    38    });
    39  };
    40  
    41  export const uploadObjectToBucket = (t, modifier, objectName, objectPath) => {
    42    const bucketName = `${constants.TEST_BUCKET_NAME}-${modifier}`;
    43    return uploadNamedObjectToBucket(t, bucketName, objectName, objectPath);
    44  };
    45  
    46  export const uploadNamedObjectToBucket = async (
    47    t,
    48    modifier,
    49    objectName,
    50    objectPath,
    51  ) => {
    52    const bucketName = modifier;
    53    const minioClient = new Minio.Client({
    54      endPoint: "localhost",
    55      port: 9000,
    56      useSSL: false,
    57      accessKey: "minioadmin",
    58      secretKey: "minioadmin",
    59    });
    60    return minioClient
    61      .fPutObject(bucketName, objectName, objectPath, {})
    62      .catch((err) => {
    63        console.log(err);
    64      });
    65  };
    66  
    67  export const setVersioned = (t, modifier) => {
    68    return setVersionedBucket(t, `${constants.TEST_BUCKET_NAME}-${modifier}`);
    69  };
    70  
    71  export const setVersionedBucket = (t, name) => {
    72    const minioClient = new Minio.Client({
    73      endPoint: "localhost",
    74      port: 9000,
    75      useSSL: false,
    76      accessKey: "minioadmin",
    77      secretKey: "minioadmin",
    78    });
    79  
    80    return new Promise((resolve, reject) => {
    81      minioClient
    82        .setBucketVersioning(name, { Status: "Enabled" })
    83        .then(resolve)
    84        .catch(resolve);
    85    });
    86  };
    87  
    88  export const namedManageButtonFor = (name) => {
    89    return Selector("div").withAttribute("id", `manageBucket-${name}`);
    90  };
    91  
    92  export const manageButtonFor = (modifier) => {
    93    return namedManageButtonFor(`${constants.TEST_BUCKET_NAME}-${modifier}`);
    94  };
    95  
    96  export const cleanUpNamedBucket = (t, name) => {
    97    const minioClient = new Minio.Client({
    98      endPoint: "localhost",
    99      port: 9000,
   100      useSSL: false,
   101      accessKey: "minioadmin",
   102      secretKey: "minioadmin",
   103    });
   104  
   105    return minioClient.removeBucket(name);
   106  };
   107  
   108  export const cleanUpBucket = (t, modifier) => {
   109    return cleanUpNamedBucket(t, `${constants.TEST_BUCKET_NAME}-${modifier}`);
   110  };
   111  
   112  export const namedTestBucketBrowseButtonFor = (name) => {
   113    return Selector("span").withAttribute("id", `browse-${name}`);
   114  };
   115  
   116  export const testBucketBrowseButtonFor = (modifier) => {
   117    return namedTestBucketBrowseButtonFor(
   118      `${constants.TEST_BUCKET_NAME}-${modifier}`,
   119    );
   120  };
   121  
   122  export const cleanUpNamedBucketAndUploads = (t, bucket) => {
   123    return new Promise((resolve, reject) => {
   124      const minioClient = new Minio.Client({
   125        endPoint: "localhost",
   126        port: 9000,
   127        useSSL: false,
   128        accessKey: "minioadmin",
   129        secretKey: "minioadmin",
   130      });
   131  
   132      var stream = minioClient.listObjects(bucket, "", true);
   133  
   134      let proms: any[] = [];
   135      stream.on("data", function (obj) {
   136        if (obj.name) {
   137          proms.push(minioClient.removeObject(bucket, obj.name));
   138        }
   139      });
   140  
   141      stream.on("end", () => {
   142        Promise.all(proms).then(() => {
   143          minioClient.removeBucket(bucket).then(resolve).catch(resolve);
   144        });
   145      });
   146    });
   147  };
   148  
   149  export const cleanUpBucketAndUploads = (t, modifier) => {
   150    const bucket = `${constants.TEST_BUCKET_NAME}-${modifier}`;
   151    return cleanUpNamedBucketAndUploads(t, bucket);
   152  };
   153  
   154  export const createUser = (t) => {
   155    return t
   156      .useRole(roles.admin)
   157      .navigateTo(`http://localhost:9090/identity/users/add-user`)
   158      .typeText(elements.usersAccessKeyInput, constants.TEST_USER_NAME)
   159      .typeText(elements.usersSecretKeyInput, constants.TEST_PASSWORD)
   160      .click(elements.saveButton);
   161  };
   162  
   163  export const cleanUpUser = (t) => {
   164    const userListItem = Selector(".ReactVirtualized__Table__rowColumn").withText(
   165      constants.TEST_USER_NAME,
   166    );
   167  
   168    const userDeleteIconButton = userListItem
   169      .nextSibling()
   170      .child("button")
   171      .withAttribute("aria-label", "delete");
   172  
   173    return t
   174      .useRole(roles.admin)
   175      .navigateTo("http://localhost:9090/identity/users")
   176      .click(userDeleteIconButton)
   177      .click(elements.deleteButton);
   178  };