Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add testing configuration for MOOC #721

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"type": "pickString",
"id": "zBackend",
"description": "Select backend mode",
"options": ["mockBackend", "localTMCServer", "production"],
"options": ["mockTmcLocalMooc", "mockBackend", "production"],
"default": "production"
}
]
Expand Down
11 changes: 10 additions & 1 deletion backend/controllers/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,20 @@ oauthRouter.post("/token", (req, res: Response<Token>) => {
const { username, password } = req.body;
console.log("Username:" + username, "Password:", password);

if (username === password) {
return res.json({
access_token: username,
token_type: "bearer",
scope: "public",
created_at: 1234567890,
});
}

const isTestUser = username === USER.username && password === USER.password;
const isStudent = username === "student" && password === "student";
if (isTestUser || isStudent) {
return res.json({
access_token: "1234",
access_token: "token",
token_type: "bearer",
scope: "public",
created_at: 1234567890,
Expand Down
18 changes: 11 additions & 7 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@ const path = require("path");

const TMC_LANGS_RUST_VERSION = "0.36.1";

const localTMCServer = {
__TMC_BACKEND__URL__: JSON.stringify("http://localhost:3000"),
__TMC_LANGS_CONFIG_DIR__: JSON.stringify(null),
__TMC_LANGS_DL_URL__: JSON.stringify("https://download.mooc.fi/tmc-langs-rust/"),
const mockTmcLocalMooc = {
__TMC_BACKEND_URL__: JSON.stringify("http://localhost:4001"),
__TMC_LANGS_CONFIG_DIR__: JSON.stringify(path.join(__dirname, "backend", "cli")),
__TMC_LANGS_DL_URL__: JSON.stringify("http://localhost:4001/langs/"),
__TMC_LANGS_VERSION__: JSON.stringify(TMC_LANGS_RUST_VERSION),
__MOOC_BACKEND_URL__: JSON.stringify("http://project-331.local"),
};

const mockBackend = {
__TMC_BACKEND__URL__: JSON.stringify("http://localhost:4001"),
__TMC_BACKEND_URL__: JSON.stringify("http://localhost:4001"),
__TMC_LANGS_CONFIG_DIR__: JSON.stringify(path.join(__dirname, "backend", "cli")),
__TMC_LANGS_DL_URL__: JSON.stringify("http://localhost:4001/langs/"),
__TMC_LANGS_VERSION__: JSON.stringify(TMC_LANGS_RUST_VERSION),
// no mock mooc backend yet
__MOOC_BACKEND_URL__: JSON.stringify("https://courses.mooc.fi"),
};

const productionApi = {
__TMC_BACKEND__URL__: JSON.stringify("https://tmc.mooc.fi"),
__TMC_BACKEND_URL__: JSON.stringify("https://tmc.mooc.fi"),
__TMC_LANGS_CONFIG_DIR__: JSON.stringify(null),
__TMC_LANGS_DL_URL__: JSON.stringify("https://download.mooc.fi/tmc-langs-rust/"),
__TMC_LANGS_VERSION__: JSON.stringify(TMC_LANGS_RUST_VERSION),
__MOOC_BACKEND_URL__: JSON.stringify("https://courses.mooc.fi"),
};

module.exports = { mockBackend, localTMCServer, productionApi };
module.exports = { mockTmcLocalMooc, mockBackend, productionApi };
10 changes: 7 additions & 3 deletions src/api/tmc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
API_CACHE_LIFETIME,
CLI_PROCESS_TIMEOUT,
MINIMUM_SUBMISSION_INTERVAL,
MOOC_BACKEND_URL,
TMC_BACKEND_URL,
} from "../config/constants";
import {
Expand Down Expand Up @@ -1012,11 +1013,13 @@ export default class TMC {
.join(" ");

// override settings with environment variables, mainly for testing
const tmcLangsBackendUrl = process.env.TMC_LANGS_TMC_ROOT_URL ?? TMC_BACKEND_URL;
const tmcBackendUrl = process.env.TMC_LANGS_TMC_ROOT_URL ?? TMC_BACKEND_URL;
const moocBackendUrl = process.env.TMC_LANGS_MOOC_ROOT_URL ?? MOOC_BACKEND_URL;
const tmcLangsConfigDir = process.env.TMC_LANGS_CONFIG_DIR ?? this._options.cliConfigDir;

Logger.info(`Running ${loggableCommand}`);
Logger.debug(`Backend at ${tmcLangsBackendUrl}`);
Logger.debug(`TMC backend at ${tmcBackendUrl}`);
Logger.debug(`MOOC backend at ${moocBackendUrl}`);
Logger.debug(`Config dir at ${tmcLangsConfigDir}`);

let active = true;
Expand All @@ -1026,7 +1029,8 @@ export default class TMC {
...process.env,
...env,
RUST_LOG: "debug,rustls=warn,reqwest=warn",
TMC_LANGS_TMC_ROOT_URL: tmcLangsBackendUrl,
TMC_LANGS_TMC_ROOT_URL: tmcBackendUrl,
TMC_LANGS_MOOC_ROOT_URL: moocBackendUrl,
TMC_LANGS_CONFIG_DIR: tmcLangsConfigDir,
},
});
Expand Down
6 changes: 4 additions & 2 deletions src/config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
// Build time only globals defined in webpack configuration. These values are inlined when
// compiling.
declare const __DEBUG_MODE__: boolean;
declare const __TMC_BACKEND__URL__: string;
declare const __TMC_BACKEND_URL__: string;
declare const __TMC_LANGS_CONFIG_DIR__: string | null;
declare const __TMC_LANGS_DL_URL__: string;
declare const __TMC_LANGS_VERSION__: string;
declare const __MOOC_BACKEND_URL__: string;

// @ts-ignore "No module found" error even though the file exists
import FAQ from "../../docs/FAQ.md";
import { TestResultData } from "../shared/shared";

export const DEBUG_MODE = __DEBUG_MODE__;
export const TMC_BACKEND_URL = __TMC_BACKEND__URL__;
export const TMC_BACKEND_URL = __TMC_BACKEND_URL__;
export const TMC_LANGS_CONFIG_DIR = __TMC_LANGS_CONFIG_DIR__ || undefined;
export const TMC_LANGS_DL_URL = __TMC_LANGS_DL_URL__;
export const TMC_LANGS_VERSION = __TMC_LANGS_VERSION__;
export const MOOC_BACKEND_URL = __MOOC_BACKEND_URL__;

export const CLIENT_NAME = "vscode_plugin";
export const EXTENSION_ID = "moocfi.test-my-code";
Expand Down
9 changes: 5 additions & 4 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ const TerserPlugin = require("terser-webpack-plugin");
const webpack = require("webpack");
const merge = require("webpack-merge").merge;

const { mockBackend, localTMCServer, productionApi } = require("./config");
const { mockTmcLocalMooc, mockBackend, productionApi } = require("./config");

/**@type {() => import("webpack").Configuration} */
const config = () => {
const isDevelopmentMode = process.env.NODE_ENV && process.env.NODE_ENV === "development";

const apiConfig = (() => {
switch (process.env.BACKEND) {
case "mockTmcLocalMooc":
return mockTmcLocalMooc;
case "mockBackend":
return mockBackend;
case "localTMCServer":
return localTMCServer;
default:
return productionApi;
}
Expand Down Expand Up @@ -162,7 +162,8 @@ const config = () => {
console.log(
`Webpack building in ${isDevelopmentMode ? "development" : "production"} configuration.`,
);
console.log(`Configured backend: ${apiConfig.__TMC_BACKEND__URL__}`);
console.log(`Configured TMC backend: ${apiConfig.__TMC_BACKEND_URL__}`);
console.log(`Configured MOOC backend: ${apiConfig.__MOOC_BACKEND_URL__}`);

return merge(commonConfig, isDevelopmentMode ? devConfig() : prodConfig());
};
Expand Down
Loading