Skip to content

Commit

Permalink
settings: Move "download personal API key" from "your-bots" to "your …
Browse files Browse the repository at this point in the history
…account"

Fixes: zulip#5355.
  • Loading branch information
vabs22 authored and showell committed Jun 20, 2017
1 parent aee130b commit d9bc393
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 102 deletions.
10 changes: 7 additions & 3 deletions frontend_tests/casper_tests/06-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ casper.then(function () {
casper.then(function () {
casper.waitUntilVisible('#account-settings-status', function () {
casper.test.assertSelectorHasText('#account-settings-status', 'Updated settings!');

casper.click('[data-section="your-bots"]');
casper.click('#api_key_button');
});
});

Expand Down Expand Up @@ -101,6 +98,13 @@ casper.then(function () {
});
});

casper.then(function () {
casper.waitUntilVisible('#account-settings-status', function () {
casper.click('[data-section="your-bots"]');
casper.click('#api_key_button');
});
});

casper.then(function create_bot() {
casper.test.info('Filling out the create bot form');

Expand Down
55 changes: 55 additions & 0 deletions static/js/settings_account.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,61 @@ function settings_change_success(message) {

exports.set_up = function () {
$("#account-settings-status").hide();
$("#api_key_value").text("");
$("#get_api_key_box").hide();
$("#show_api_key_box").hide();
$("#api_key_button_box").show();

$('#api_key_button').click(function () {
if (page_params.realm_password_auth_enabled !== false) {
$("#get_api_key_box").show();
} else {
// Skip the password prompt step
$("#get_api_key_box form").submit();
}
$("#api_key_button_box").hide();
});

$("#get_api_key_box").hide();
$("#show_api_key_box").hide();
$("#get_api_key_box form").ajaxForm({
dataType: 'json', // This seems to be ignored. We still get back an xhr.
success: function (resp, statusText, xhr) {
var result = JSON.parse(xhr.responseText);
var settings_status = $('#account-settings-status').expectOne();

$("#get_api_key_password").val("");
$("#api_key_value").text(result.api_key);
$("#show_api_key_box").show();
$("#get_api_key_box").hide();
settings_status.hide();
},
error: function (xhr) {
ui_report.error(i18n.t("Error getting API key"), xhr, $('#account-settings-status').expectOne());
$("#show_api_key_box").hide();
$("#get_api_key_box").show();
},
});

$("#show_api_key_box").on("click", "button.regenerate_api_key", function () {
channel.post({
url: '/json/users/me/api_key/regenerate',
idempotent: true,
success: function (data) {
$('#api_key_value').text(data.api_key);
},
error: function (xhr) {
$('#user_api_key_error').text(JSON.parse(xhr.responseText).msg).show();
},
});
});

$("#download_zuliprc").on("click", function () {
$(this).attr("href", settings_bots.generate_zuliprc_uri(
people.my_current_email(),
$("#api_key_value").text()
));
});

function clear_password_change() {
// Clear the password boxes so that passwords don't linger in the DOM
Expand Down
55 changes: 0 additions & 55 deletions static/js/settings_bots.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,9 @@ exports.generate_flaskbotrc_content = function (email, api_key) {
};

exports.set_up = function () {
$("#api_key_value").text("");
$("#get_api_key_box").hide();
$("#show_api_key_box").hide();
$("#api_key_button_box").show();
$('#payload_url_inputbox').hide();
$('#create_payload_url').val('');

$('#api_key_button').click(function () {
if (page_params.realm_password_auth_enabled !== false) {
$("#get_api_key_box").show();
} else {
// Skip the password prompt step
$("#get_api_key_box form").submit();
}
$("#api_key_button_box").hide();
});

$('#download_flaskbotrc').click(function () {
var OUTGOING_WEBHOOK_BOT_TYPE_INT = 3;
var content = "";
Expand All @@ -116,27 +102,6 @@ exports.set_up = function () {
$(this).attr("href", "data:application/octet-stream;charset=utf-8," + encodeURIComponent(content));
});

$("#get_api_key_box").hide();
$("#show_api_key_box").hide();
$("#get_api_key_box form").ajaxForm({
dataType: 'json', // This seems to be ignored. We still get back an xhr.
success: function (resp, statusText, xhr) {
var result = JSON.parse(xhr.responseText);
var settings_status = $('#account-settings-status').expectOne();

$("#get_api_key_password").val("");
$("#api_key_value").text(result.api_key);
$("#show_api_key_box").show();
$("#get_api_key_box").hide();
settings_status.hide();
},
error: function (xhr) {
ui_report.error(i18n.t("Error getting API key"), xhr, $('#account-settings-status').expectOne());
$("#show_api_key_box").hide();
$("#get_api_key_box").show();
},
});

// TODO: render bots xxxx
render_bots();
$(document).on('zulip.bot_data_changed', render_bots);
Expand Down Expand Up @@ -366,26 +331,6 @@ exports.set_up = function () {
));
});

$("#download_zuliprc").on("click", function () {
$(this).attr("href", exports.generate_zuliprc_uri(
people.my_current_email(),
$("#api_key_value").text()
));
});

$("#show_api_key_box").on("click", "button.regenerate_api_key", function () {
channel.post({
url: '/json/users/me/api_key/regenerate',
idempotent: true,
success: function (data) {
$('#api_key_value').text(data.api_key);
},
error: function (xhr) {
$('#user_api_key_error').text(JSON.parse(xhr.responseText).msg).show();
},
});
});

$("#bots_lists_navbar .active-bots-tab").click(function (e) {
e.preventDefault();
e.stopPropagation();
Expand Down
42 changes: 42 additions & 0 deletions static/templates/settings/account-settings.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,47 @@
</div>
</div>

<hr class="settings_separator" />

<div class="form-horizontal" id="api_key_button_box">
<div class="input-group side-padded-container">
<p class="api-key-note">
{{#tr this}}
Creating a bot, and using the bot's account / API key, is the recommended way to use
the Zulip API. However, you can also use your personal API key if you need to a way
to automatically send and edit messages from your own account.
{{/tr}}
</p>
<button class="button rounded" id="api_key_button">{{t "Show/change your API key" }}</button>
</div>
</div>
<div id="get_api_key_box" class="side-padded-container">
<p>{{t "Please re-enter your password to confirm your identity." }}
<a href="/accounts/password/reset/" target="_blank">{{t "Forgotten it?" }}</a></p>
<form action="/json/fetch_api_key" method="post" class="form-horizontal">
<div class="control-group">
<label for="password" class="control-label">{{t "Current password" }}</label>
<input type="password" autocomplete="off"
name="password" id="get_api_key_password" value="" />
</div>
<div class="control-group">
<input type="submit" name="view_api_key" value="{{t 'Get API key' }}"
class="btn btn-primary" />
</div>
</form>
</div>
<div id="show_api_key_box" class="side-padded-container">
<p>{{t "Your API key:" }}</p>
<p><b><span id="api_key_value"></span></b></p>
<button type="submit" class="btn btn-primary regenerate_api_key">
{{t "Generate new API key" }}
</button>
<a id="download_zuliprc" download="{{zuliprc}}" class="btn btn-success">
{{t "Download .zuliprc" }}
</a>
<div id="user_api_key_error text-error">
</div>
</div>

</div>
</div>
43 changes: 0 additions & 43 deletions static/templates/settings/bot-settings.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -80,48 +80,5 @@
</form>
</div>

<hr class="settings_separator" />

<div class="form-horizontal" id="api_key_button_box">
<div class="input-group side-padded-container">
<p class="api-key-note">
{{#tr this}}
For most bots using the API, you'll want to give each bot
its own name and API key using the above section. But if you
want to write a bot that can access your own private
messages, you should use your personal API key.
{{/tr}}
</p>
<button class="button rounded" id="api_key_button">{{t "Show/change your API key" }}</button>
</div>
</div>
<div id="get_api_key_box" class="side-padded-container">
<p>{{t "Please re-enter your password to confirm your identity." }}
<a href="/accounts/password/reset/" target="_blank">{{t "Forgotten it?" }}</a></p>
<form action="/json/fetch_api_key" method="post" class="form-horizontal">
<div class="control-group">
<label for="password" class="control-label">{{t "Current password" }}</label>
<input type="password" autocomplete="off"
name="password" id="get_api_key_password" value="" />
</div>
<div class="control-group">
<input type="submit" name="view_api_key" value="{{t 'Get API key' }}"
class="btn btn-primary" />
</div>
</form>
</div>
<div id="show_api_key_box" class="side-padded-container">
<p>{{t "Your API key:" }}</p>
<p><b><span id="api_key_value"></span></b></p>
<button type="submit" class="btn btn-primary regenerate_api_key">
{{t "Generate new API key" }}
</button>
<a id="download_zuliprc" download="{{zuliprc}}" class="btn btn-success">
{{t "Download .zuliprc" }}
</a>
<div id="user_api_key_error text-error">
</div>
</div>

</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
1. Generate a Slack API token using Slack's [test token generator](https://api.slack.com/docs/oauth-test-tokens)
to import all of the necessary data.

{!go-to-the.md!} [Your bots](/#settings/your-bots)
{!go-to-the.md!} [Your Account](/#settings/your-account)
{!settings.md!}

3. Click on the **Show/change your API key** button.
Expand Down

0 comments on commit d9bc393

Please sign in to comment.