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

Workflow #245

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
ec875ac
ESLint installed
alexanderdyb Jul 20, 2023
d53f13e
ESLint setup
alexanderdyb Jul 20, 2023
b2b8354
Added lint task to package file
alexanderdyb Jul 20, 2023
1d9be56
Prettier installed
alexanderdyb Jul 20, 2023
6faec64
Added prettier custom task to package file
alexanderdyb Jul 20, 2023
ab192a7
Created save hook
alexanderdyb Jul 20, 2023
c59e469
Pre-commit Hooks - Mrm
alexanderdyb Jul 21, 2023
a553765
Added commands under lint-staged
alexanderdyb Jul 21, 2023
3702287
Configured the project with GitHub Actions for build/deploy
alexanderdyb Jul 24, 2023
a4d7006
Fixed duplication
alexanderdyb Jul 24, 2023
6c8dfc2
Configured project for Jest
alexanderdyb Aug 6, 2023
0b442a3
Created Unit tests for login and logout function
alexanderdyb Aug 6, 2023
fef43ca
Configured project for Cypress
alexanderdyb Aug 6, 2023
85be207
Update
alexanderdyb Aug 6, 2023
60ef255
End to end test: The user can log in and access their profile
alexanderdyb Aug 8, 2023
ffd12c7
End-to-end test: login error and logout
alexanderdyb Aug 14, 2023
e40a09e
Create static.yml
alexanderdyb Aug 14, 2023
e57c1b7
Fixes
alexanderdyb Aug 14, 2023
60676d3
Update README.md
alexanderdyb Aug 14, 2023
bb13deb
Update README.md
alexanderdyb Aug 14, 2023
e511b61
Fixes
alexanderdyb Aug 14, 2023
99f0e57
Merge branch 'workflow' of https://github.com/alexanderdyb/social-med…
alexanderdyb Aug 14, 2023
7cbd70a
Update README.md
alexanderdyb Aug 19, 2023
4e8ba71
Update README.md
alexanderdyb Aug 19, 2023
f56ef29
Update README.md
alexanderdyb Aug 19, 2023
5405d1d
Changes based on feedback
alexanderdyb Aug 19, 2023
71a6517
Merge branch 'workflow' of https://github.com/alexanderdyb/social-med…
alexanderdyb Aug 19, 2023
36979bb
Fixes
alexanderdyb Sep 10, 2023
bd9be1e
Worflow changes
alexanderdyb Sep 10, 2023
cdaf0c9
Update README.md
alexanderdyb Sep 10, 2023
8ea205f
Update README.md
alexanderdyb Sep 10, 2023
979934d
Update README.md
alexanderdyb Sep 10, 2023
c1b424c
Update README.md
alexanderdyb Sep 10, 2023
4f0563e
Changes to gpt.yml
alexanderdyb Sep 10, 2023
d6df47b
Merge branch 'workflow' of https://github.com/alexanderdyb/social-med…
alexanderdyb Sep 10, 2023
31de88c
Fixes
alexanderdyb Sep 10, 2023
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
32 changes: 32 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"env": {
"browser": true,
"es2021": true,
"node": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"overrides": [
{
"files": ["**/*.test.js"],
"env": { "jest": true },
"plugins": ["jest"],
"extends": ["plugin:jest/recommended"],
"rules": { "jest/prefer-expect-assertions": "off" }
},
{
"files": ["**/*.cy.js"],
"env": { "cypress/globals": true },
"plugins": ["cypress"],
"extends": ["plugin:cypress/recommended"],
"rules": {
"cypress/no-unnecessary-waiting": "off",
"no-unused-vars": "off"
}
}
],
"rules": {}
}
31 changes: 30 additions & 1 deletion .github/workflows/gpt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ permissions:
pull-requests: write

on:
pull_request_target:
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]
workflow_dispatch:
Comment on lines +8 to +13

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The changes made to the triggers for the GitHub Actions workflow are good. The workflow will now run on push events to the master branch, pull request events, and manual dispatches. This provides more flexibility and control over when the workflow is executed.

-  pull_request_target:
-     types: [opened, synchronize, reopened]
+  push:
+    branches:
+      - master
+  pull_request:
+     types: [opened, synchronize, reopened]
+  workflow_dispatch:


jobs:
review:
Expand All @@ -16,9 +20,34 @@ jobs:
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}

- name: Log environment info
run: |
echo "OS: $(uname -a)"
echo "Shell: $SHELL"
echo "GitHub Runner: ${{ runner.os }}"

- name: Install dependencies
run: npm install

- name: Build project
run: npm run build

- name: Lint Code
uses: github/super-linter@v4
env:
DEFAULT_BRANCH: main
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VALIDATE_JAVASCRIPT_ES: true
ESLINT_CONFIG_FILE: .eslintrc.json

- uses: fluxninja/openai-pr-reviewer@main
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
with:
debug: false

- name: Catch errors
if: failure()
run: echo "An error occurred during the workflow execution."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The additional steps in the workflow are generally well structured. However, there are a few points to consider:

  1. The "Log environment info" step might expose sensitive information in the logs. Make sure that no confidential data is being printed.

  2. The "Install dependencies" step uses npm install. It would be better to use npm ci in a CI/CD context as it will respect the exact versions specified in your package-lock.json, ensuring more consistent builds.

  3. The "Catch errors" step will only echo an error message if a failure occurs. Consider adding more robust error handling here, such as sending notifications or failing the workflow explicitly.

        - name: Log environment info
          run: |
            echo "OS: $(uname -a)"
            echo "Shell: $SHELL"
            echo "GitHub Runner: ${{ runner.os }}"

        - name: Install dependencies
-         run: npm install
+         run: npm ci

        - name: Catch errors
          if: failure()
-         run: echo "An error occurred during the workflow execution."
+         run: |
+           echo "An error occurred during the workflow execution."
+           exit 1

4 changes: 2 additions & 2 deletions .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install Dependencies
- name: Install Dependencies
run: npm i
- name: Build SASS
run: npm run build
Expand All @@ -40,7 +40,7 @@ jobs:
uses: actions/upload-pages-artifact@v1
with:
# Upload entire repository
path: '.'
path: "."
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.validate": ["javascript"]
}
15 changes: 15 additions & 0 deletions 6
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

added 67 packages, and audited 386 packages in 6s

55 packages are looking for funding
run `npm fund` for details

4 high severity vulnerabilities

To address issues that do not require attention, run:
npm audit fix

To address all issues (including breaking changes), run:
npm audit fix --force

Run `npm audit` for details.
Comment on lines +1 to +15

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

This hunk seems to be a log output from an npm install or similar command, rather than actual code. It's generally not recommended to include such logs in the codebase as they don't contribute to the functionality of the application and can quickly become outdated. Instead, any necessary instructions related to package installation or vulnerability fixing should be included in the project's README or other documentation.

- 
- added 67 packages, and audited 386 packages in 6s
- 
- 55 packages are looking for funding
-   run `npm fund` for details
- 
- 4 high severity vulnerabilities
- 
- To address issues that do not require attention, run:
-   npm audit fix
- 
- To address all issues (including breaking changes), run:
-   npm audit fix --force
- 
- Run `npm audit` for details.

51 changes: 51 additions & 0 deletions README.md
Copy link
Contributor

@cnnrbrn cnnrbrn Aug 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's customary to add the npm installor npm i instruction in the readme, though it's pretty obvious that that is required in any project with a package.json.

Just missing the status badges here, instructions for them are at the end of this lesson: https://content.noroff.dev/workflow/automated-testing.html

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
## Badges

[![Deploy static content to Pages](https://github.com/alexanderdyb/social-media-client/actions/workflows/pages.yml/badge.svg)](https://github.com/alexanderdyb/social-media-client/actions/workflows/pages.yml)

[![Code Review](https://github.com/alexanderdyb/social-media-client/actions/workflows/gpt.yml/badge.svg)](https://github.com/alexanderdyb/social-media-client/actions/workflows/gpt.yml)

[![Automated Unit Testing](https://github.com/alexanderdyb/social-media-client/actions/workflows/unit-test.yml/badge.svg)](https://github.com/alexanderdyb/social-media-client/actions/workflows/unit-test.yml)

[![Automated E2E Testing](https://github.com/alexanderdyb/social-media-client/actions/workflows/e2e-test.yml/badge.svg)](https://github.com/alexanderdyb/social-media-client/actions/workflows/e2e-test.yml)

## Getting started

Before you start the project, make sure you have Node.js and npm installed on your machine.

Install the Dependencies:

npm i

## Configuration

- SASS for styling, along with a build script for compiling SASS to CSS.
- Live-reloading with `live-server` for immediate feedback during development.
- ESLint for linting JavaScript files.
- Prettier for code formatting.
- Husky and lint-staged for ensuring code quality through pre-commit hooks.
- Unit testing with Jest.
- End-to-end testing with Cypress.

## Dependencies

- Styling: sass, bootstrap-dark-5
- Development: live-server
- Testing: jest, cypress
- Linting & Formatting: eslint, prettier
- Others: husky, lint-staged

## Testing

Run unit tests:
npm run test-unit

Run end-to-end tests:
npm run test-e2e

## To lint JavaScript files:

npm run lint

## To format code:

npm run format
3 changes: 3 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [["@babel/preset-env", { "targets": { "node": "current" } }]]
}
10 changes: 10 additions & 0 deletions cypress.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { defineConfig } = require("cypress");

module.exports = defineConfig({
e2e: {
// eslint-disable-next-line no-unused-vars
setupNodeEvents(on, config) {
// implement node event listeners here
},
},
});
Comment on lines +1 to +10

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The new configuration for Cypress looks good. It's a good practice to use defineConfig for better IntelliSense and type checking. The setupNodeEvents function is currently empty, but it seems like it will be used in the future for setting up node event listeners. If it won't be used, consider removing it to avoid unnecessary code.

+ const { defineConfig } = require("cypress");
+ 
+ module.exports = defineConfig({
+   e2e: {
+     // eslint-disable-next-line no-unused-vars
+     setupNodeEvents(on, config) {
+       // implement node event listeners here
+     },
+   },
+ });

24 changes: 24 additions & 0 deletions cypress/e2e/login.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
describe("Login and Profile Access", () => {
const email = "testusername21212@noroff.no";
const password = "Banner1212";

const loginPageUrl = "http://127.0.0.1:5500/";
const profilePageUrl = "http://127.0.0.1:5500/?view=profile&name=John_Doe";

it("should log in and access profile", () => {
cy.visit(loginPageUrl);
cy.wait(500);

cy.get('#registerForm [data-auth="login"]').click();
cy.wait(500);

cy.get("#loginForm input[name=email]").type(email);
cy.get("#loginForm input[name=password]").type(password);

cy.get("#loginForm button[type=submit]").click();

cy.url().should("eq", profilePageUrl);

cy.contains("John_Doe").should("be.visible");
Comment on lines +20 to +22

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

It's good that you are checking the URL and visible content after login. However, it would be better to also check if the logout button or other user-specific elements are present on the page. This will ensure that the user is indeed logged in.

// Add after line 22
cy.get('#logoutButton').should('be.visible');

});
});
Comment on lines +1 to +24

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The test case seems to be well written, but it's not a good practice to hardcode sensitive data like email and password directly in the test cases. Consider using environment variables or a configuration file that is not committed to the repository to store these values. This will also make your tests more flexible as you can change the test user without modifying the test code.

- 2:   const email = "testusername21212@noroff.no";
- 3:   const password = "Banner1212";
+ 2:   const email = Cypress.env('TEST_USER_EMAIL');
+ 3:   const password = Cypress.env('TEST_USER_PASSWORD');

24 changes: 24 additions & 0 deletions cypress/e2e/loginerror.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
describe("Invalid Login Attempt", () => {
const invalidEmail = "invalid@noroff.no";
const invalidPassword = "wrongpassword";

const loginPageUrl = "http://127.0.0.1:5500/";

const errorMessageSelector = ".error-message";

it("should not allow login with invalid credentials and show an error", () => {
cy.visit(loginPageUrl);
cy.wait(500);

cy.get('#registerForm [data-auth="login"]').click();
cy.wait(500);

cy.get("#loginForm input[name=email]").type(invalidEmail);
cy.get("#loginForm input[name=password]").type(invalidPassword);
cy.get("#loginForm button[type=submit]").click();

cy.url().should("eq", loginPageUrl);

cy.get(errorMessageSelector).should("be.visible");
});
});
29 changes: 29 additions & 0 deletions cypress/e2e/logout.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
describe("Logout Functionality", () => {
const loginPageUrl = "http://127.0.0.1:5500/";
const profilePageUrl = "http://127.0.0.1:5500/?view=profile&name=John_Doe";

const email = "testusername21212@noroff.no";
const password = "Banner1212";

const logoutButtonSelector = '[data-auth="logout"]';

it("should log out the user when the logout button is clicked", () => {
cy.visit(loginPageUrl);
cy.wait(500);
cy.get('#registerForm [data-auth="login"]').click();
cy.wait(500);

cy.get("#loginForm input[name=email]").type(email);
cy.get("#loginForm input[name=password]").type(password);
cy.get("#loginForm button[type=submit]").click();

cy.url().should("eq", profilePageUrl);

cy.get(logoutButtonSelector).click();

cy.url().should("eq", loginPageUrl);

cy.window().its("localStorage").should("not.have.key", "token");
cy.window().its("localStorage").should("not.have.key", "profile");
});
});
5 changes: 5 additions & 0 deletions cypress/e2e/spec.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe("template spec", () => {
it("passes", () => {
cy.visit("https://example.cypress.io");
});
});
5 changes: 5 additions & 0 deletions cypress/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Using fixtures to represent data",
"email": "hello@cypress.io",
"body": "Fixtures are a great way to mock data for responses to routes"
}
25 changes: 25 additions & 0 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add('login', (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
20 changes: 20 additions & 0 deletions cypress/support/e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ***********************************************************
// This example support/e2e.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import "./commands";

// Alternatively you can use CommonJS syntax:
// require('./commands')
Loading
Loading