From f8795454a45e1887e7bbb1a04cfe8e165982832e Mon Sep 17 00:00:00 2001
From: Jayesh Velayudhan <jayesh@softwareheritage.org>
Date: Thu, 17 Mar 2022 15:56:34 +0100
Subject: [PATCH] Bug fixes in template and JS

Removed python form
Cypress tests
---
 .../bundles/add_forge/request-dashboard.js    |  5 +-
 cypress/fixtures/add-forge-now-request.json   | 23 +++++
 .../add-forge-now-request-dashboard.spec.js   | 88 +++++++++++++++++++
 swh/web/add_forge_now/views.py                | 14 +--
 .../add_forge_now/request-dashboard.html      | 15 ++--
 5 files changed, 123 insertions(+), 22 deletions(-)
 create mode 100644 cypress/fixtures/add-forge-now-request.json
 create mode 100644 cypress/integration/add-forge-now-request-dashboard.spec.js

diff --git a/assets/src/bundles/add_forge/request-dashboard.js b/assets/src/bundles/add_forge/request-dashboard.js
index bccdd0099..0f6761d4b 100644
--- a/assets/src/bundles/add_forge/request-dashboard.js
+++ b/assets/src/bundles/add_forge/request-dashboard.js
@@ -48,9 +48,8 @@ async function populateRequestDetails(requestId) {
     populateRequestHistory(data.history);
     populateDecisionSelectOption(data.request.status);
   } catch (response) {
-    alert(11);
-    $('#fetchError').removeClass('d-done');
-    $('#requestDetails').addClass('d-done');
+    $('#fetchError').removeClass('d-none');
+    $('#requestDetails').addClass('d-none');
   }
 }
 
diff --git a/cypress/fixtures/add-forge-now-request.json b/cypress/fixtures/add-forge-now-request.json
new file mode 100644
index 000000000..127f5e93a
--- /dev/null
+++ b/cypress/fixtures/add-forge-now-request.json
@@ -0,0 +1,23 @@
+{
+  "request":{
+    "id":1,
+    "status":"PENDING",
+    "submission_date":"2022-03-17T13:35:24.324848Z",
+    "submitter_name":"admin",
+    "submitter_email":"admin@swh-web.org",
+    "forge_type":"bitbucket",
+    "forge_url":"test.com",
+    "forge_contact_email":"test@example.com",
+    "forge_contact_name":"test user",
+    "forge_contact_comment":"test comment"
+  },"history":[
+    {
+      "id":1,
+      "text":"",
+      "actor":"admin",
+      "actor_role":"SUBMITTER",
+      "date":"2022-03-17T13:35:24.326190Z",
+      "new_status":"PENDING"
+    }
+  ]
+}
diff --git a/cypress/integration/add-forge-now-request-dashboard.spec.js b/cypress/integration/add-forge-now-request-dashboard.spec.js
new file mode 100644
index 000000000..998aae9d9
--- /dev/null
+++ b/cypress/integration/add-forge-now-request-dashboard.spec.js
@@ -0,0 +1,88 @@
+/**
+ * Copyright (C) 2022  The Software Heritage developers
+ * See the AUTHORS file at the top-level directory of this distribution
+ * License: GNU Affero General Public License version 3, or any later version
+ * See top-level LICENSE file for more information
+ */
+
+
+let forgeAddRequest;
+const requestId = 1;
+
+describe('Test add forge now request dashboard load', function() {
+
+  beforeEach(function() {
+    const url = this.Urls.request_dashboard_forge_add(requestId);
+    cy.moderatorLogin();
+    cy.intercept(`${this.Urls.api_1_add_forge_request_get(requestId)}**`,
+                 {fixture: 'add-forge-now-request'}).as('forgeAddRequest');
+    cy.visit(url);
+  });
+
+  // it('should not let non moderator access page', function() {
+  //   cy.userLogin();
+  //   cy.visit(this.Urls.request_dashboard_forge_add(requestId));
+  // });
+
+  it('should load add forge request details', function() {
+    cy.wait('@forgeAddRequest');
+    cy.get('#requestStatus')
+      .should('contain', 'PENDING');
+
+    cy.get('#requestType')
+      .should('contain', 'bitbucket');
+
+    cy.get('#requestURL')
+      .should('contain', 'test.com');
+
+    cy.get('#requestEmail')
+      .should('contain', 'test@example.com');
+  });
+
+  it('should not show any error message', function() {
+    cy.get('#fetchError')
+      .should('have.class', 'd-none');
+    cy.get('#requestDetails')
+      .should('not.have.class', 'd-none');
+  });
+
+  it('should show error message for an api error', function() {
+    const invalidRequestId = 2;
+    const url = this.Urls.request_dashboard_forge_add(invalidRequestId);
+    cy.visit(url);
+    cy.get('#fetchError')
+      .should('not.have.class', 'd-none');
+    cy.get('#requestDetails')
+      .should('have.class', 'd-none');
+  });
+
+  it('should load add forge request history', function() {
+    cy.get('#swh-request-history')
+      .children()
+      .should('have.length', 1);
+
+    cy.get('#swh-request-history')
+      .children()
+      .should('contain', 'New status: PENDING');
+  });
+
+  // it('should load right email template ', function() {
+  // });
+
+  // it('should open mailclient with right text', function() {
+  //   // make sure the email address is right
+  // });
+
+  it('should load possbile next status', function() {
+    // 3 possible status for a request in pending state
+    cy.get('#decisionOptions')
+      .children()
+      .should('have.length', 3);
+  });
+
+  it('should update the forge request', function() {
+  });
+
+  it('should update cahnge the request details', function() {
+  });
+});
diff --git a/swh/web/add_forge_now/views.py b/swh/web/add_forge_now/views.py
index cb9ca0cd7..dcee10ae2 100644
--- a/swh/web/add_forge_now/views.py
+++ b/swh/web/add_forge_now/views.py
@@ -99,17 +99,6 @@ FORGE_TYPES: List[str] = [
 ]
 
 
-class RequestUpdateForm(forms.Form):
-    """Form for moderators to update an 'add_forge_now' request
-
-    """
-
-    new_status = forms.ChoiceField(choices=[])
-    new_status.widget.attrs.update({"class": "form-control", "id": "decisionOptions"})
-    text = forms.CharField(label="Comment", widget=forms.Textarea)
-    text.widget.attrs.update({"class": "form-control", "rows": "3"})
-
-
 def create_request(request):
     """View to create a new 'add_forge_now' request.
 
@@ -152,9 +141,8 @@ def request_dashboard(request, request_id):
 
     """
 
-    request_update_form = RequestUpdateForm()
     return render(
         request,
         "add_forge_now/request-dashboard.html",
-        {"request_id": request_id, "request_update_form": request_update_form},
+        {"request_id": request_id},
     )
diff --git a/swh/web/templates/add_forge_now/request-dashboard.html b/swh/web/templates/add_forge_now/request-dashboard.html
index 22faa57c4..563c5dc88 100644
--- a/swh/web/templates/add_forge_now/request-dashboard.html
+++ b/swh/web/templates/add_forge_now/request-dashboard.html
@@ -25,7 +25,7 @@ See top-level LICENSE file for more information
 <div class="col-md-7 offset-md-2">
   <div class="container">
     <div id="fetchError" class="d-none">
-      Error fetching information about the request
+      <h3>Error fetching information about the request</h3>
     </div>
     <div id="requestDetails">
       <div class="row">
@@ -98,16 +98,19 @@ See top-level LICENSE file for more information
             {% csrf_token %}
             <div class="form-row">
               <div class="form-group col-md-6">
-                <h6><span class="text-muted">Choose your decision</span></h6>
-                {{request_update_form.new_status}}
+                <label for="decisionOptions">Choose your decision</label>
+                <select class="form-control" id="decisionOptions" name="new_status">
+                </select>
               </div>
             </div>
 
             <div class="form-row">
               <div class="form-group col-md-12">
-                <h6><span class="text-muted">Comment</span></h6>
-                {{request_update_form.text}}
-                <small id="emailHelp" class="form-text text-muted">Enter any comment related to your decision.</small>
+                <label for="swh-input-forge-comment">Comment</label>
+                <textarea class="form-control" name="text" rows="3"></textarea>
+                <small class="form-text text-muted">
+                  Enter any comment related to your decision.
+                </small>                
               </div>
             </div>
             <div class="form-group col-md-6">
-- 
GitLab