Skip to content

Commit

Permalink
Merge branch '187809206-automation-polygon-points' of https://github.…
Browse files Browse the repository at this point in the history
…com/concord-consortium/collaborative-learning into 187809206-automation-polygon-points
  • Loading branch information
bgoldowsky committed Sep 10, 2024
2 parents 6c39d67 + adad6ac commit 7316e78
Show file tree
Hide file tree
Showing 165 changed files with 19,293 additions and 1,360 deletions.
111 changes: 26 additions & 85 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,109 +59,49 @@ To deploy a production release:

## Developing/deploying cloud functions

CLUE uses several Google cloud functions to implement certain features that would be difficult (or impossible) to implement entirely client-side.
|Function|Purpose|
|--------|-------|
|_getImageData_|Retrieves image data that may reside in other classes and hence is not accessible client-side, e.g. for supports published to multiple classes or documents retrieved via the teacher network.|
|_getNetworkDocument_|Retrieves the contents of a document accessible to a teacher via the teacher network.|
|_getNetworkResources_|Retrieves the list of resources (documents) available to a teacher via the teacher network.|
|_postDocumentComment_|Posts a comment to a document in firestore, adding metadata for the document to firestore if necessary.|
|_publishSupport_|Publishes a document as a support that is accessible to all of a teacher's classes (including any referenced images).|
|_validateCommentableDocument_|Checks whether a specific commentable document exists in firestore and creates it if necessary.|

The code for the functions is in the `functions` directory. You should be able to cd into the
`functions` directory and perform basic development operations:
```
$ cd functions
$ npm install # install local dependencies
$ npm run lint # lint the functions code
$ npm run test # runs jest (unit) tests for the functions code
$ npm run build # build the functions code (transpile TypeScript)
```
### Note 1
There seems to be an uneasy relationship between the `node_modules` folder in the
`functions` directory and the one in the parent directory. I had to explicitly specify the
path to typescript in the `build` function. There's probably a better configuration available,
but in the meantime this seems to mostly work.

### Note 2
When running `npm run test` with node 16, the following error is shown
```
TypeError: Cannot read properties of undefined (reading 'INTERNAL')
```
This error is triggered by the following line in `test-utils.ts`
```
import { useEmulators } from "@firebase/rules-unit-testing";
```
The current work around is to use node 14 to run the tests.

See functions/dependency-notes.md for more on this.
CLUE uses several Google cloud functions to implement certain features that would be difficult (or impossible) to implement entirely client-side. There are two folders of functions `functions-v1` and `functions-v2`. We are trying to incrementally migrate the v1 functions into the v2 folder.

### Testing cloud functions
Each folder has its own readme:
- [functions-v2](functions-v2/README.md)
- [functions-v1](functions-v1/README.md)

Google recommends (requires?) that [firebase-tools](https://www.npmjs.com/package/firebase-tools) be installed globally:
```
$ npm install -g firebase-tools
```
This should be run periodically to make sure you're running the latest version of the tools.

#### Running tests locally (without running functions in the emulator)
```
$ npm run serve # build and then start the emulators
$ npm run test # run all tests in `functions` directory
$ npm run test -- some.test.ts # run a particular test
```
The existing tests currently work this way. They test the basic functionality of the cloud functions by importing and calling them directly from node.js test code. This is a simple and efficient way of testing the basic functionality without all the overhead of the functions emulator. The downside is that the node.js test environment is not the same as the hosted function environment. For instance, it's possible to return objects in node.js that can't be JSON-stringified which will throw an error when the function is hosted. That said, you can't beat the convenience of simply calling the functions directly.

#### Running local tests against functions hosted in the emulator
To run jest tests against functions running in the emulator requires [serving functions using a Cloud Functions Shell](https://firebase.google.com/docs/functions/local-shell#serve_functions_using_a_cloud_functions_shell). Currently, all of our functions are `HTTPS Callable` functions, which [can be called](https://firebase.google.com/docs/functions/local-shell#invoke_https_callable_functions) in this shell mode, but:
>Emulation of context.auth is currently unavailable.
## Testing/Deploying database rules

#### Running CLUE against functions running locally in the emulator:
```
$ npm run serve # build and then start the functions emulator
```
and launch CLUE with url parameter `functions=emulator`.
### Requirements

### To deploy firebase functions to production:
```
$ npm run deploy # deploy all functions
$ npm run deploy:getImageData # deploy individual function
$ npm run deploy:postDocumentComment # deploy individual function
```
- The tests currently only run with Node.js version 16.x
- You need the firebase CLI. Version 12 is compatible with Node 16: `npm install -g firebase-tools@12`
- You should be logged in to firebase: `firebase login`

By convention, our firebase functions have an internal version number that is returned with any results. This should be incremented appropriately when new versions are deployed. This will allow us to determine whether the current code in GitHub has been deployed or not, for instance. Also by convention, our firebase functions accept parameters of `{ warmUp: true }` which can be issued in advance of any actual call to mitigate the google cloud function cold-start issue.
Java is also required for running the emulators. There are various ways to install it; I did this:

### Serving CLUE from https://localhost
To test the deployed function(s) from your local development environment, you may need to run your local dev server with https to avoid CORS errors. To do so, [create a certificate](https://www.matthewhoelter.com/2019/10/21/how-to-setup-https-on-your-local-development-environment-localhost-in-minutes.html) in your `~/.localhost-ssl` directory and name the files `localhost.pem` and `localhost.key`. To use the certificate:
```shell
brew install java
echo 'export PATH="/opt/homebrew/opt/openjdk/bin:$PATH"' >> ~/.zshrc
```
$ npm run start:secure
```

## Testing/Deploying database rules

### Requirements:

* You should install the firebase CLI via: `npm install -g firebase-tools`
* You should be logged in to firebase: `firebase login`

Firestore security rules are unit tested and realtime database rules could be with some additional work.

### To test database rules

The emulator must be running when the test is invoked.

```shell
cd firebase-test
npm run start &
npm run test
```
$ cd firebase-test
$ npm run test
```

### To deploy database rules

You deploy firebase functions and rules directly from the working directory using
the `firebase deploy` command. You can see `firebase deploy help` for more info.

See which project you have access to and which you are currently using via: `firebase projects:list`

### To deploy database rules:
```
$ npm run deploy:firestore:rules # deploys firestore rules
$ npm run deploy:firebase:rules # deploys firebase (realtime database) rules
```shell
npm run deploy:firestore:rules # deploys firestore rules
npm run deploy:firebase:rules # deploys firebase (realtime database) rules
```

## Debugging
Expand All @@ -174,6 +114,7 @@ To enable per component debugging set the "debug" localstorage key with one or m
- `docList` - this will print a table of information about a list of documents
- `document` this will add the active document as `window.currentDocument`, you can use MST's hidden toJSON() like `currentDocument.toJSON()` to views its content.
- `drop` console log the dataTransfer object from drop events on the document.
- `firestore` turn on Firestore's internal debugging, this logs all queries to Firestore.
- `history` this will: print some info to the console as the history system records changes, print the full history as JSON each time it is loaded from Firestore, and provide a `window.historyDocument` so you can inspect the document while navigating the history.
- `images` this will set `window.imageMap` so you can look at the status and URLs of images that have been loaded.
- `listeners` console log the adding, removing, and firing of firebase listeners
Expand Down
1 change: 1 addition & 0 deletions cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default defineConfig({
qaMothPlotUnitStudent5: "/?appMode=qa&fakeClass=5&fakeUser=student:5&qaGroup=5&problem=1.1&unit=./demo/units/qa-moth-plot/content.json",
qaNoSectionProblemTabUnitStudent5: "/?appMode=qa&fakeClass=5&fakeUser=student:5&qaGroup=5&problem=1.1&unit=./demo/units/qa-no-section-problem-tab/content.json",
clueTestqaUnitStudent5: "/?appMode=demo&demoName=CLUE-Test&fakeClass=5&fakeUser=student:5&problem=1.1&unit=./demo/units/qa/content.json&noPersistentUI",
clueTestNoUnitStudent5: "/?appMode=demo&demoName=CLUE-Test&fakeClass=5&fakeUser=student:5&problem=1.1&noPersistentUI",
clueTestqaUnitTeacher6: "/?appMode=demo&demoName=CLUE-Test&fakeClass=5&fakeUser=teacher:6&problem=1.1&unit=./demo/units/qa/content.json&noPersistentUI",
clueTestqaConfigSubtabsUnitTeacher6: "/?appMode=demo&demoName=CLUE-Test&fakeClass=5&fakeUser=teacher:6&problem=1.1&unit=qa-config-subtabs&noPersistentUI",
e2e: {
Expand Down
35 changes: 35 additions & 0 deletions cypress/e2e/functional/document_tests/exemplar_test_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,41 @@ context('Exemplar Documents', function () {
clueCanvas.getStickyNotePopup().should("not.exist");
});

it('Exemplars show up in the correct place in the sort work view', function () {
beforeTest(queryParams2);
cy.openTopTab('sort-work');

// With no secondary sort, the full exemplar tile should show up in the right sections.
sortWork.openSortWorkSection("No Group");
sortWork.checkDocumentInGroup("No Group", exemplarName);

sortWork.getPrimarySortByMenu().click();
sortWork.getPrimarySortByNameOption().click();
sortWork.openSortWorkSection("Idea, Ivan");
sortWork.checkDocumentInGroup("Idea, Ivan", exemplarName);

sortWork.getPrimarySortByMenu().click();
sortWork.getPrimarySortByTagOption().click();
sortWork.openSortWorkSection("Varies Material/Surface");
sortWork.checkDocumentInGroup("Varies Material/Surface", exemplarName);

sortWork.getPrimarySortByMenu().click();
sortWork.getPrimarySortByBookmarkedOption().click();
sortWork.openSortWorkSection("Not Bookmarked");
sortWork.checkDocumentInGroup("Not Bookmarked", exemplarName);

sortWork.getPrimarySortByMenu().click();
sortWork.getPrimarySortByToolsOption().click();
sortWork.openSortWorkSection("Text");
sortWork.checkDocumentInGroup("Text", exemplarName);

// With a secondary sort, "simple documents" (little boxes) should show up for exemplars.

sortWork.getSecondarySortByMenu().click();
sortWork.getSecondarySortByNameOption().click();
sortWork.checkSimpleDocumentInSubgroup("Text", "Idea, Ivan", exemplarInfo);
});

it('Unit with exemplars hidden initially, revealed 3 drawings and 3 text tiles', function () {
beforeTest(queryParams1);
cy.openTopTab('sort-work');
Expand Down
29 changes: 26 additions & 3 deletions cypress/e2e/functional/document_tests/student_test_spec.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import Header from '../../../support/elements/common/Header';
import ClueHeader from '../../../support/elements/common/cHeader';
import SortedWork from "../../../support/elements/common/SortedWork";


const header = new Header;
const clueHeader = new ClueHeader;
const sortWork = new SortedWork;

let student = '5',
classroom = '5',
group = '5';

function beforeTest() {
const queryParams = `${Cypress.config("qaUnitStudent5")}`;
function beforeTest(queryParams) {
cy.clearQAData('all');
cy.visit(queryParams);
cy.waitForLoad();
}

context('Check header area for correctness', function () {
it('verify header area', function () {
beforeTest();
beforeTest(`${Cypress.config("qaUnitStudent5")}`);

cy.log('will verify if class name is correct');
header.getClassName().should('contain', 'Class ' + classroom);
Expand All @@ -43,3 +44,25 @@ context('Check header area for correctness', function () {
});
});

context("check public/private document access", function() {
it("marks private documents as private and only shows public documents as accessible", function() {
const queryParams = (`${Cypress.config("clueTestNoUnitStudent5")}`);
beforeTest(queryParams);

cy.openTopTab("sort-work");
cy.get(".section-header-arrow").click({multiple: true}); // Open all sections
cy.log("will verify if private documents are marked as private and are not accessible");
sortWork.checkGroupDocumentVisibility("No Group", true, true);
cy.log("will verify if user's own documents are not marked as private and are accessible");
sortWork.checkGroupDocumentVisibility("Group 2", false, true);

// Check the above for a view that contains compact document items
sortWork.getShowForMenu().click();
sortWork.getShowForInvestigationOption().click();
cy.get(".section-header-arrow").click({multiple: true}); // Open all sections
cy.log("will verify if private documents are marked as private and are not accessible in the compact view");
sortWork.checkGroupDocumentVisibility("No Group", true);
cy.log("will verify if user's own documents are not marked as private and are accessible in the compact view");
sortWork.checkGroupDocumentVisibility("Group 2", false);
});
});
6 changes: 4 additions & 2 deletions cypress/e2e/functional/teacher_tests/teacher_share_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ context('Teacher Sharing', function() {
verifySwitch('private');
});

it('does not allow student to access private teacher document', function() {
// TODO: Reinstate the tests below when all metadata documents have the new fields and are updated in real time.
it.skip('does not allow student to access private teacher document', function() {
cy.visit(studentQueryParams);
cy.waitForLoad();
verifyStudentSeesAsPrivate();
Expand All @@ -52,7 +53,8 @@ context('Teacher Sharing', function() {
verifySwitch('public');
});

it('allows student to access public teacher document', function() {
// TODO: Reinstate the tests below when all metadata documents have the new fields and are updated in real time.
it.skip('allows student to access public teacher document', function() {
cy.visit(studentQueryParams);
cy.waitForLoad();
verifyStudentSeesAsPublic();
Expand Down
Loading

0 comments on commit 7316e78

Please sign in to comment.