Skip to content

Commit

Permalink
tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Moneexa committed Nov 30, 2023
1 parent 918b1e6 commit be65909
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
28 changes: 14 additions & 14 deletions src/plugins/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { Repository } from "./classes.js";
import axios from "axios"
import { BASE_URL_REPO, BASE_URL_USER, HEADERS } from "./apiConfig/config.js"
import { BASE_URL_REPO, BASE_URL_USER, getHeaders } from "./apiConfig/config.js"

let repoOwner = "";
let repoName = "";
Expand All @@ -21,7 +21,7 @@ export function setInfosForApi(currRepoOwner, currRepoName, currBranch) {
* @returns {Promise<object[]>} the array of emails with attributes 'email', 'primary', 'verified', 'visibility'
*/
export async function getUserEmail() {
const headers = HEADERS;
const headers = getHeaders();
return axios
.get(`${BASE_URL_USER}/public_emails`, { headers })
.then((response) => response.data)
Expand All @@ -37,7 +37,7 @@ export async function getUserEmail() {
* @returns {Promise<object[]>} the array of informations with attributes 'login', 'name', etc.
*/
export async function getUserName() {
const headers = HEADERS
const headers = getHeaders()
return axios.get(BASE_URL_USER, { headers })
.then((response) => response.data)
.catch((err) => {
Expand All @@ -52,7 +52,7 @@ export async function getUserName() {
* @returns {Promise<object[]>} informations about the branch with attributes 'commit: { sha }', etc.
*/
export async function getCommitSha() {
const headers = HEADERS
const headers = getHeaders()
return axios.get(`${BASE_URL_REPO}/${repoOwner}/${repoName}/branches/${branch}`, { headers })
.then((response) => response.data)
.catch((err) => {
Expand All @@ -68,7 +68,7 @@ export async function getCommitSha() {
* @returns {Promise<object[]>} informations about the newly created file with attributes 'sha', etc.
*/
export async function createBlobs(file) {
const headers = HEADERS;
const headers = getHeaders();
const copyHeader = { ...headers }; // Spread syntax to create a shallow copy
copyHeader["Accept"] = "application/vnd.github+json";
return axios.post(`${BASE_URL_REPO}/${repoOwner}/${repoName}/git/blobs`,
Expand All @@ -92,7 +92,7 @@ export async function createBlobs(file) {
* @returns {Promise<object[]>} informations about the newly created tree with attributes 'sha', etc.
*/
export async function createFileTree(lastCommitSha, folderTree) {
const headers = HEADERS
const headers = getHeaders()
return axios.post(`${BASE_URL_REPO}/${repoOwner}/${repoName}/git/trees`, {
base_tree: lastCommitSha,
tree: folderTree
Expand All @@ -116,7 +116,7 @@ export async function createFileTree(lastCommitSha, folderTree) {
* @returns {Promise<object[]>} informations about the created commit with attributes 'sha', etc.
*/
export async function createCommit(commitMessage, authorInfos, lastCommitSha, treeSha) {
const headers = HEADERS
const headers = getHeaders()
return axios.post(`${BASE_URL_REPO}/${repoOwner}/${repoName}/git/commits`, {
message: commitMessage,
author: authorInfos,
Expand All @@ -139,7 +139,7 @@ export async function createCommit(commitMessage, authorInfos, lastCommitSha, tr
* @returns {Promise<object[]>} informations about the reference.
*/
export async function pushToGitHub(newCommitSha) {
const headers = HEADERS
const headers = getHeaders()
return axios.post(`${BASE_URL_REPO}/${repoOwner}/${repoName}/git/refs/heads/${branch}`, {
ref: "refs/heads/" + branch,
sha: newCommitSha
Expand All @@ -164,10 +164,8 @@ export async function pushToGitHub(newCommitSha) {
*/

export async function loadRepositoryList(searchText = "", page = 1, perPage = 5) {
const token = localStorage.getItem("authId");

try {
const headers = HEADERS
const userId = localStorage.getItem('user');

const body = {
Expand All @@ -188,7 +186,9 @@ export async function loadRepositoryList(searchText = "", page = 1, perPage = 5)
}`
};

const response = await axios.post('https://api.github.com/graphql', body, { headers });
const response = await axios.post('https://api.github.com/graphql', body, {
headers: getHeaders()
});

if (!response?.data) {
return [];
Expand Down Expand Up @@ -260,7 +260,7 @@ export async function searchRepositoryList(searchString, maxResults = 2, searchR
*/

export async function loadFileTreeOfRepository(repoFullName, branch) {
const headers = HEADERS
const headers = getHeaders()

return axios.get(`${BASE_URL_REPO}/${repoFullName}/git/trees/${branch}?recursive=1`, { headers })
.then((response) => response.data)
Expand All @@ -279,7 +279,7 @@ export async function loadFileTreeOfRepository(repoFullName, branch) {
* @returns {Promise<{ name : string }[]>} the fetched array of branches
*/
export async function loadBranchesName(repoName, username) {
const headers = HEADERS
const headers = getHeaders()
return axios
.get(`${BASE_URL_REPO}/${username}/${repoName}/branches?per_page=999`, { headers })
.then((response) => response.data)
Expand All @@ -299,7 +299,7 @@ export async function loadBranchesName(repoName, username) {
* @returns {Promise<string>} a promise with the raw content of the specified file
*/
export async function loadRawFile(repoFullName, branch, filePath) {
const headers = HEADERS
const headers = getHeaders()

if (typeof branch !== "string" || typeof branch != "string") {
console.log(
Expand Down
10 changes: 6 additions & 4 deletions src/plugins/apiConfig/config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const BASE_URL_USER = "https://api.github.com/user"
const BASE_URL_REPO = "https://api.github.com/repos"
const HEADERS = {
Authorization: `Bearer ${localStorage.getItem("authId")
}`

const getHeaders = () => {
return {
Authorization: `Bearer ${localStorage?.getItem("authId")}`
}
}


export { BASE_URL_REPO, BASE_URL_USER, HEADERS }
export { BASE_URL_REPO, BASE_URL_USER, getHeaders }
2 changes: 2 additions & 0 deletions tests/api-searchRepositoryList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ global.localStorage = {
return "abc....";
}
};
console.log("###### print local storage #######", localStorage.getItem("authId"))
import { searchRepositoryList } from "../src/plugins/api.js";

import { searchTermRepoPairs } from "./constants.js";
Expand All @@ -21,6 +22,7 @@ for (let i = 0; i < searchTermRepoPairs.length; i++) {
let expectedResult = searchTermRepoPairs[i].results;

test("Test Searching Repos with list in parameter. Searching for " + searchTerm, async () => {
console.log(localStorage.getItem("authId"))
let list = [];
axios.post.mockResolvedValue({

Expand Down

0 comments on commit be65909

Please sign in to comment.