From 3bc6af9840648a6b531a548aee281ec4f46bffe0 Mon Sep 17 00:00:00 2001
From: Dominik Seeger <dominik.seeger@gmx.net>
Date: Sun, 6 Jan 2019 21:28:26 +0100
Subject: [PATCH] added tests for studentList deobfuscation logic

---
 .../tests/unit/components/DataExport.spec.ts  | 14 +++++
 .../unit/components/InstanceExport.spec.ts    | 61 +++++++++++++++++++
 frontend/tests/utils/testUtils.ts             | 23 +++++--
 3 files changed, 94 insertions(+), 4 deletions(-)
 create mode 100644 frontend/tests/unit/components/InstanceExport.spec.ts

diff --git a/frontend/tests/unit/components/DataExport.spec.ts b/frontend/tests/unit/components/DataExport.spec.ts
index f41c8bf8..617670fb 100644
--- a/frontend/tests/unit/components/DataExport.spec.ts
+++ b/frontend/tests/unit/components/DataExport.spec.ts
@@ -94,4 +94,18 @@ describe('DataExport Component Unit Tests', () => {
     await wrapper.vm.getExportFile('data')
     spy.calledWithExactly(testUtils.studentExports).should.equal(true)
   })
+  it('should correctly apply the student-map file', () => {
+    let wrapper = mount(DataExport, { localVue: localVue, store })
+    let mapStub = sinon.stub().returns(testUtils.studentMap)
+    let spy = sinon.spy()
+    // @ts-ignore
+    sinon.replaceGetter(wrapper.vm, 'studentMap', mapStub)
+    // @ts-ignore
+    sinon.replaceGetter(wrapper.vm, 'mapFileLoaded', sinon.stub().returns(true))
+    // @ts-ignore
+    sinon.replace(wrapper.vm, 'optionalConvertAndCreatePopup', spy)
+    // @ts-ignore
+    wrapper.vm.getMappedExportFile(testUtils.studentExportsObfuscated)
+    spy.firstCall.args[0][0].Matrikel.should.equal(testUtils.studentMap['username'].matrikelNo)
+  })
 })
diff --git a/frontend/tests/unit/components/InstanceExport.spec.ts b/frontend/tests/unit/components/InstanceExport.spec.ts
new file mode 100644
index 00000000..9f9a6de9
--- /dev/null
+++ b/frontend/tests/unit/components/InstanceExport.spec.ts
@@ -0,0 +1,61 @@
+import Vuex from 'vuex'
+import { mount, createLocalVue, createWrapper } from '@vue/test-utils'
+import InstanceExport from '@/components/export/InstanceExport.vue'
+import sinon from 'sinon'
+import chai from 'chai'
+import VueRouter from 'vue-router'
+import testUtils from '@/../tests/utils/testUtils'
+
+import * as api from '@/api'
+chai.should()
+
+let localVue = createLocalVue()
+localVue.use(Vuex)
+localVue.use(VueRouter)
+let router = new VueRouter()
+
+// @ts-ignore
+global.requestAnimationFrame = cb => cb()
+
+describe('InstanceExport Component Unit Tests', () => {
+  let store: any = null
+  let consoleTemp = {
+    warn: console.warn,
+    error: console.error
+  }
+
+  before(function () {
+    console.warn = function () {}
+    console.error = function () {}
+  })
+
+  after(function () {
+    console.warn = consoleTemp.warn
+    console.error = consoleTemp.error
+  })
+
+  beforeEach(() => {
+    store = new Vuex.Store({
+      state: {}
+    })
+  })
+
+  afterEach(() => {
+    sinon.restore()
+  })
+
+  it('should correctly apply the student-map file', () => {
+    let wrp = mount(InstanceExport, { localVue: localVue, store })
+    let mapStub = sinon.stub().returns(testUtils.studentMap)
+    let spy = sinon.spy()
+    // @ts-ignore
+    sinon.replaceGetter(wrp.vm, 'studentMap', mapStub)
+    // @ts-ignore
+    sinon.replaceGetter(wrp.vm, 'mapFileLoaded', sinon.stub().returns(true))
+    // @ts-ignore
+    sinon.replace(wrp.vm, 'optionalConvertAndCreatePopup', spy)
+    // @ts-ignore
+    wrp.vm.getMappedExportFile(testUtils.instanceExportsObfuscated)
+    spy.firstCall.args[0].students[0].matrikelNo.should.equal(testUtils.studentMap['username'].matrikelNo)
+  })
+})
\ No newline at end of file
diff --git a/frontend/tests/utils/testUtils.ts b/frontend/tests/utils/testUtils.ts
index 599cb750..7009a255 100644
--- a/frontend/tests/utils/testUtils.ts
+++ b/frontend/tests/utils/testUtils.ts
@@ -19,12 +19,27 @@ export default {
     Password: 'pwd',
     Scores: [{ type: 'test01', score: 100 }]
   }],
+  studentExportsObfuscated: <StudentExportItem[]> [{
+    Matrikel: 'username',
+    Name: 'name',
+    Username: 'username',
+    Sum: 100,
+    Exam: 'exam',
+    Password: 'pwd',
+    Scores: [{ type: 'test01', score: 100 }]
+  }],
   instanceExports: <InstanceExportData> {
     students: [{
-      name: 'test',
+      name: 'name',
       matrikelNo: '1000000'
     }]
   },
+  instanceExportsObfuscated: <InstanceExportData> {
+    students: [{
+      name: 'name',
+      matrikelNo: 'username'
+    }]
+  },
   fakeFile: <File> {
     lastModified: 1,
     name: 'Grady testUtils fake file',
@@ -33,8 +48,8 @@ export default {
     type: 'fake file'
   },
   studentMap: {
-    '1000000': {
-      matrikelNo: '1000000', name: 'test'
+    'username': {
+      matrikelNo: '1000000', name: 'name'
     }
-  }
+  },
 }
-- 
GitLab