Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
DEV: Fix build.
  • Loading branch information
udan11 committed Sep 16, 2019
1 parent 6ff3873 commit 9de1ecb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 45 deletions.
2 changes: 1 addition & 1 deletion assets/javascripts/lib/database.js.es6
Expand Up @@ -82,7 +82,7 @@ export function saveDbIdentity(identity) {

if (isSafari) {
return exportIdentity(identity).then(exported =>
window.localStorage.setItem(DB_NAME, exported)
window.localStorage.setItem(DB_NAME, exported.private)
);
}

Expand Down
2 changes: 1 addition & 1 deletion assets/stylesheets/common/encrypt.scss
Expand Up @@ -80,4 +80,4 @@ table.paperkeys {
color: $danger;
margin: 0 0 0 0.5em;
}
}
}
2 changes: 1 addition & 1 deletion spec/plugin_spec.rb
Expand Up @@ -5,7 +5,7 @@
describe ::DiscourseEncrypt do

it 'registers current user fields' do
expect(DiscoursePluginRegistry.serialized_current_user_fields).to include('encrypt_public_key', 'encrypt_private_key', 'encrypt_salt')
expect(DiscoursePluginRegistry.serialized_current_user_fields).to include('encrypt_public', 'encrypt_private')
end

it 'registers preloaded custom fields' do
Expand Down
69 changes: 28 additions & 41 deletions spec/requests/encrypt_controller_spec.rb
Expand Up @@ -31,9 +31,8 @@
context '#update_keys' do
it 'does not work when not logged in' do
put '/encrypt/keys', params: {
public_key: '-- the public key --',
private_key: '-- the private key --',
salt: '-- the salt --'
public: '-- the public key --',
private: '-- the private key --'
}

expect(response.status).to eq(403)
Expand All @@ -47,18 +46,16 @@
sign_in(user)

put '/encrypt/keys', params: {
public_key: '-- the public key --',
private_key: '-- the private key --',
salt: '-- the salt --'
public: '-- the public key --',
private: '-- the private key --'
}
expect(response.status).to eq(403)

Fabricate(:group_user, group: group, user: user)

put '/encrypt/keys', params: {
public_key: '-- the public key --',
private_key: '-- the private key --',
salt: '-- the salt --'
public: '-- the public key --',
private: '-- the private key --'
}
expect(response.status).to eq(200)
end
Expand All @@ -67,51 +64,44 @@
sign_in(user3)

put '/encrypt/keys', params: {
public_key: '-- the public key --',
private_key: '-- the private key --',
salt: '-- the salt --'
public: '-- the public key --',
private: '-- the private key --'
}

expect(response.status).to eq(200)
expect(user3.custom_fields['encrypt_public_key']).to eq('-- the public key --')
expect(user3.custom_fields['encrypt_private_key']).to eq('-- the private key --')
expect(user3.custom_fields['encrypt_salt']).to eq('-- the salt --')
expect(user3.custom_fields['encrypt_public']).to eq('-- the public key --')
expect(user3.custom_fields['encrypt_private']).to eq('-- the private key --')
end

it 'updates user keys' do
sign_in(user)

put '/encrypt/keys', params: {
public_key: '-- the public key --',
private_key: '-- the new private key --',
salt: '-- the new salt --'
public: '-- the public key --',
private: '-- the new private key --'
}

user.reload

expect(response.status).to eq(200)
expect(user.custom_fields['encrypt_public_key']).to eq('-- the public key --')
expect(user.custom_fields['encrypt_private_key']).to eq('-- the new private key --')
expect(user.custom_fields['encrypt_salt']).to eq('-- the new salt --')
expect(user.custom_fields['encrypt_public']).to eq('-- the public key --')
expect(user.custom_fields['encrypt_private']).to eq('-- the new private key --')
end

it 'does not allow updating if wrong public key' do
user.custom_fields['encrypt_public_key'] = '-- the public key --'
user.custom_fields['encrypt_private_key'] = '-- the private key --'
user.custom_fields['encrypt_salt'] = '-- the salt --'
user.custom_fields['encrypt_public'] = '-- the public key --'
user.custom_fields['encrypt_private'] = '-- the private key --'
user.save!
sign_in(user)

put '/encrypt/keys', params: {
public_key: '-- a wrong public key --',
private_key: '-- the new private key --',
salt: '-- the new salt --'
public: '-- a wrong public key --',
private: '-- the new private key --'
}

expect(response.status).to eq(409)
expect(user.custom_fields['encrypt_public_key']).to eq('-- the public key --')
expect(user.custom_fields['encrypt_private_key']).to eq('-- the private key --')
expect(user.custom_fields['encrypt_salt']).to eq('-- the salt --')
expect(user.custom_fields['encrypt_public']).to eq('-- the public key --')
expect(user.custom_fields['encrypt_private']).to eq('-- the private key --')
end
end

Expand All @@ -122,9 +112,9 @@
end

it 'gets the right user keys' do
user.custom_fields['encrypt_public_key'] = '-- the public key --'
user.custom_fields['encrypt_public'] = '-- the public key --'
user.save!
user2.custom_fields['encrypt_public_key'] = '-- another public key --'
user2.custom_fields['encrypt_public'] = '-- another public key --'
user2.save
sign_in(user)

Expand All @@ -141,9 +131,8 @@

context '#reset_user' do
before do
user.custom_fields['encrypt_public_key'] = '-- the public key --'
user.custom_fields['encrypt_private_key'] = '-- the private key --'
user.custom_fields['encrypt_salt'] = '-- the salt --'
user.custom_fields['encrypt_public'] = '-- the public key --'
user.custom_fields['encrypt_private'] = '-- the private key --'
user.save_custom_fields

store.set("key_#{topic.id}_#{user.id}", '-- user key --')
Expand All @@ -152,23 +141,21 @@
end

it 'resets everything' do
user.grant_admin!

expect { post '/encrypt/reset', params: { user_id: user.id, everything: true } }
.to change { TopicAllowedUser.count }.by(-1)
.and change { PluginStoreRow.count }.by(-1)
.and change { UserCustomField.count }.by(-3)
.and change { UserCustomField.count }.by(-2)

expect(response.status).to eq(200)
end

it 'allows only staff members' do
it 'resets only keys' do
expect { post '/encrypt/reset', params: { user_id: user.id } }
.to change { TopicAllowedUser.count }.by(0)
.and change { PluginStoreRow.count }.by(0)
.and change { UserCustomField.count }.by(0)
.and change { UserCustomField.count }.by(-2)

expect(response.status).to eq(403)
expect(response.status).to eq(200)
end
end
end
7 changes: 6 additions & 1 deletion test/javascripts/lib/protocol_v1-test.js.es6
Expand Up @@ -24,11 +24,16 @@ test("exportIdentity & importIdentity", async assert => {
const identity = await generateIdentity();

let exported = await exportIdentity(identity);
let imported = await importIdentity(exported);
let imported = await importIdentity(exported.private);
assert.ok(imported.encryptPublic instanceof CryptoKey);
assert.ok(imported.encryptPrivate instanceof CryptoKey);
assert.ok(imported.signPublic instanceof CryptoKey);
assert.ok(imported.signPrivate instanceof CryptoKey);
imported = await importIdentity(exported.public);
assert.ok(imported.encryptPublic instanceof CryptoKey);
assert.equal(imported.encryptPrivate, null);
assert.ok(imported.signPublic instanceof CryptoKey);
assert.equal(imported.signPrivate, null);

exported = await exportIdentity(identity, "test");
imported = await importIdentity(exported.private, "test");
Expand Down

0 comments on commit 9de1ecb

Please sign in to comment.