Stop user from adding a db or owner with the same name (#1893)

This commit is contained in:
Charis Kyriakou
2022-12-21 10:51:24 +00:00
committed by GitHub
parent 55869e8033
commit dbdb4ba57a
3 changed files with 94 additions and 4 deletions

View File

@@ -99,6 +99,12 @@ export class DbConfigStore extends DisposableObject {
throw Error("Cannot add remote repo if config is not loaded");
}
if (this.doesRemoteDbExist(repoNwo)) {
throw Error(
`A remote repository with the name '${repoNwo}' already exists`,
);
}
const config: DbConfig = cloneDbConfig(this.config);
config.databases.remote.repositories.push(repoNwo);
@@ -110,6 +116,10 @@ export class DbConfigStore extends DisposableObject {
throw Error("Cannot add remote owner if config is not loaded");
}
if (this.doesRemoteOwnerExist(owner)) {
throw Error(`A remote owner with the name '${owner}' already exists`);
}
const config: DbConfig = cloneDbConfig(this.config);
config.databases.remote.owners.push(owner);
@@ -144,6 +154,32 @@ export class DbConfigStore extends DisposableObject {
);
}
public doesRemoteDbExist(dbName: string, listName?: string): boolean {
if (!this.config) {
throw Error(
"Cannot check remote database existence if config is not loaded",
);
}
if (listName) {
return this.config.databases.remote.repositoryLists.some(
(l) => l.name === listName && l.repositories.includes(dbName),
);
}
return this.config.databases.remote.repositories.includes(dbName);
}
public doesRemoteOwnerExist(owner: string): boolean {
if (!this.config) {
throw Error(
"Cannot check remote onwer existence if config is not loaded",
);
}
return this.config.databases.remote.owners.includes(owner);
}
private async writeConfig(config: DbConfig): Promise<void> {
await outputJSON(this.configPath, config, {
spaces: 2,

View File

@@ -76,10 +76,24 @@ export class DbManager {
}
public async addNewRemoteRepo(nwo: string): Promise<void> {
if (nwo === "") {
throw new Error("Repository name cannot be empty");
}
if (this.dbConfigStore.doesRemoteDbExist(nwo)) {
throw new Error(`The repository '${nwo}' already exists`);
}
await this.dbConfigStore.addRemoteRepo(nwo);
}
public async addNewRemoteOwner(owner: string): Promise<void> {
if (owner === "") {
throw Error("Owner name cannot be empty");
}
if (this.dbConfigStore.doesRemoteOwnerExist(owner)) {
throw Error(`The owner '${owner}' already exists`);
}
await this.dbConfigStore.addRemoteOwner(owner);
}
@@ -97,8 +111,4 @@ export class DbManager {
throw Error("Cannot add a local list");
}
}
public doesRemoteListExist(listName: string): boolean {
return this.dbConfigStore.doesRemoteListExist(listName);
}
}

View File

@@ -589,6 +589,50 @@ describe("db panel", () => {
new Error("A list with the name 'my-list-1' already exists"),
);
});
it("should not allow adding a new remote db with empty name", async () => {
const dbConfig = createDbConfig();
await saveDbConfig(dbConfig);
await expect(dbManager.addNewRemoteRepo("")).rejects.toThrow(
new Error("Repository name cannot be empty"),
);
});
it("should not allow adding a remote db with duplicate name", async () => {
const dbConfig = createDbConfig({
remoteRepos: ["owner1/repo1"],
});
await saveDbConfig(dbConfig);
await expect(dbManager.addNewRemoteRepo("owner1/repo1")).rejects.toThrow(
new Error("The repository 'owner1/repo1' already exists"),
);
});
it("should not allow adding a new remote owner with empty name", async () => {
const dbConfig = createDbConfig();
await saveDbConfig(dbConfig);
await expect(dbManager.addNewRemoteOwner("")).rejects.toThrow(
new Error("Owner name cannot be empty"),
);
});
it("should not allow adding a remote owner with duplicate name", async () => {
const dbConfig = createDbConfig({
remoteOwners: ["owner1"],
});
await saveDbConfig(dbConfig);
await expect(dbManager.addNewRemoteOwner("owner1")).rejects.toThrow(
new Error("The owner 'owner1' already exists"),
);
});
});
async function saveDbConfig(dbConfig: DbConfig): Promise<void> {