forked from OWASP/threat-dragon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Local session will now download the threat model file when saved.
- Loading branch information
Showing
4 changed files
with
142 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import env from './env.js'; | ||
|
||
/** | ||
* Saves the threat model using electron specific APIs | ||
* @param {Object} data | ||
* @param {string} fileName | ||
*/ | ||
const saveElectron = (data, fileName) => { | ||
console.log(data); | ||
console.log(fileName); | ||
console.warn('Not implemented - TODO'); | ||
}; | ||
|
||
/** | ||
* Saves a local copy from the browser. | ||
* This appears to the user as a download | ||
* @param {Object} data | ||
* @param {string} fileName | ||
*/ | ||
const saveLocalBrowser = (data, fileName) => { | ||
const contentType = 'application/json'; | ||
const jsonData = JSON.stringify(data, null, 2); | ||
const blob = new Blob([jsonData], { type: contentType }); | ||
var a = document.createElement('a'); | ||
a.href = window.URL.createObjectURL(blob); | ||
a.download = fileName; | ||
a.click(); | ||
a.remove(); | ||
}; | ||
|
||
/** | ||
* Helper that figures out how to save the model. | ||
* There are different flows for electron, vs browser local vs github | ||
* @param {Vuex.Store} store | ||
* @param {Object} data | ||
* @param {string} fileName | ||
*/ | ||
const local = (data, fileName) => { | ||
if (env.isElectron()) { | ||
saveElectron(data, fileName); | ||
} else { | ||
saveLocalBrowser(data, fileName); | ||
} | ||
}; | ||
|
||
export default { | ||
local | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import env from '@/service/env.js'; | ||
import save from '@/service/save.js'; | ||
|
||
describe('service/save.js', () => { | ||
const data = { foo: 'bar' }; | ||
const name = 'test.json'; | ||
|
||
describe('browser', () => { | ||
let mockAnchor; | ||
beforeEach(() => { | ||
mockAnchor = { | ||
click: jest.fn(), | ||
remove: jest.fn() | ||
}; | ||
env.isElectron = jest.fn().mockReturnValue(false); | ||
window.URL = { | ||
createObjectURL: jest.fn() | ||
}; | ||
document.createElement = jest.fn().mockReturnValue(mockAnchor); | ||
save.local(data, name); | ||
}); | ||
|
||
it('creates the object url', () => { | ||
expect(window.URL.createObjectURL).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('clicks the link', () => { | ||
expect(mockAnchor.click).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('removes the anchor', () => { | ||
expect(mockAnchor.remove).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe('electron', () => { | ||
beforeEach(() => { | ||
env.isElectron = jest.fn().mockReturnValue(true); | ||
}); | ||
|
||
// TODO | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters